Skip to content

Commit

Permalink
fix(cleanup): remove scheduler code from flush-microtasks (#744)
Browse files Browse the repository at this point in the history
* test: reveal issue with the test generally

* test: fix the test to actually test what we can test for

* fix(cleanup): remove scheduler related code

* chore: remove semver dep
  • Loading branch information
kentcdodds committed Jul 13, 2020
1 parent 9e5cf59 commit 240900c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 32 deletions.
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -44,8 +44,7 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.10.3",
"@testing-library/dom": "^7.17.1",
"semver": "^7.3.2"
"@testing-library/dom": "^7.17.1"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.10.1",
Expand Down
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)

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

0 comments on commit 240900c

Please sign in to comment.