Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cleanup): remove scheduler code from flush-microtasks #744

Merged
merged 4 commits into from Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/__tests__/cleanup.js
Expand Up @@ -27,11 +27,11 @@ test('cleanup does not error when an element is not a child', async () => {
await cleanup()
})

test('cleanup waits for queued microtasks during unmount sequence', async () => {
test('cleanup runs effect cleanup functions', async () => {
const spy = jest.fn()

const Test = () => {
React.useEffect(() => () => setImmediate(spy))
React.useEffect(() => spy)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All we can guarantee is that the cleanup function is called, so that's all we'll test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 The problem is that we can't know what kind of task is queued from the cleanup function. We only supported setImmediate but any longer running task wasn't caught as far as I can tell.


return null
}
Expand Down
29 changes: 1 addition & 28 deletions src/flush-microtasks.js
@@ -1,6 +1,3 @@
import React from 'react'
import satisfies from 'semver/functions/satisfies'

/* istanbul ignore file */
// the part of this file that we need tested is definitely being run
// and the part that is not cannot easily have useful tests written
Expand All @@ -18,9 +15,6 @@ function getIsUsingFakeTimers() {
)
}

const globalObj = typeof window === 'undefined' ? global : window
let Scheduler = globalObj.Scheduler

let didWarnAboutMessageChannel = false
let enqueueTask

Expand All @@ -32,8 +26,6 @@ try {
// assuming we're in node, let's try to get node's
// version of setImmediate, bypassing fake timers if any.
enqueueTask = nodeRequire.call(module, 'timers').setImmediate
// import React's scheduler so we'll be able to schedule our tasks later on.
Scheduler = nodeRequire.call(module, 'scheduler')
} catch (_err) {
// we're in a browser
// we can't use regular timers because they may still be faked
Expand All @@ -55,28 +47,9 @@ try {
'if you encounter this warning.',
)
}

}
}

const isModernScheduleCallbackSupported = Scheduler && satisfies(React.version, '>16.8.6', {
includePrerelease: true,
})

function scheduleCallback(cb) {
const NormalPriority = Scheduler
? Scheduler.NormalPriority || Scheduler.unstable_NormalPriority
: null

const scheduleFn = Scheduler
? Scheduler.scheduleCallback || Scheduler.unstable_scheduleCallback
: callback => callback()

return isModernScheduleCallbackSupported
? scheduleFn(NormalPriority, cb)
: scheduleFn(cb)
}

export default function flushMicroTasks() {
return {
then(resolve) {
Expand All @@ -87,7 +60,7 @@ export default function flushMicroTasks() {
jest.advanceTimersByTime(0)
resolve()
} else {
scheduleCallback(() => enqueueTask(resolve))
enqueueTask(resolve)
}
},
}
Expand Down