Skip to content

Commit 5ef6cb8

Browse files
committed
Working on ^16.9 and lower than 16.8
1 parent f9557bf commit 5ef6cb8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@reach/router": "^1.3.3",
5151
"@testing-library/jest-dom": "^5.10.1",
5252
"@types/react-dom": "^16.9.8",
53-
"@types/scheduler": "^0.16.1",
5453
"dotenv-cli": "^3.1.0",
5554
"dtslint": "3.6.12",
5655
"kcd-scripts": "^6.2.3",
@@ -62,8 +61,7 @@
6261
},
6362
"peerDependencies": {
6463
"react": "*",
65-
"react-dom": "*",
66-
"scheduler": "*"
64+
"react-dom": "*"
6765
},
6866
"eslintConfig": {
6967
"extends": "./node_modules/kcd-scripts/eslint.js",

src/flush-microtasks.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import {
2-
unstable_scheduleCallback as scheduleCallback,
3-
unstable_NormalPriority as normalPriority,
4-
} from 'scheduler'
51
/* istanbul ignore file */
62
// the part of this file that we need tested is definitely being run
73
// and the part that is not cannot easily have useful tests written
@@ -21,6 +17,10 @@ function getIsUsingFakeTimers() {
2117

2218
let didWarnAboutMessageChannel = false
2319
let enqueueTask
20+
const globalObj = typeof window === 'undefined' ? global : window
21+
22+
let Scheduler = globalObj.Scheduler
23+
2424
try {
2525
// read require off the module object to get around the bundlers.
2626
// we don't want them to detect a require and bundle a Node polyfill.
@@ -29,6 +29,8 @@ try {
2929
// assuming we're in node, let's try to get node's
3030
// version of setImmediate, bypassing fake timers if any.
3131
enqueueTask = nodeRequire.call(module, 'timers').setImmediate
32+
// import React's scheduler so we'll be able to schedule our tasks later on.
33+
Scheduler = nodeRequire.call(module, 'scheduler')
3234
} catch (_err) {
3335
// we're in a browser
3436
// we can't use regular timers because they may still be faked
@@ -53,6 +55,15 @@ try {
5355
}
5456
}
5557

58+
// in case this react version has a Scheduler implementation, we use it,
59+
// if not, we just create a function calling our callback
60+
const scheduleCallback = Scheduler
61+
? Scheduler.scheduleCallback || Scheduler.unstable_scheduleCallback
62+
: (_, cb) => cb()
63+
const NormalPriority = Scheduler
64+
? Scheduler.NormalPriority || Scheduler.unstable_NormalPriority
65+
: null
66+
5667
export default function flushMicroTasks() {
5768
return {
5869
then(resolve) {
@@ -63,7 +74,7 @@ export default function flushMicroTasks() {
6374
jest.advanceTimersByTime(0)
6475
resolve()
6576
} else {
66-
scheduleCallback(normalPriority, () => {
77+
scheduleCallback(NormalPriority, () => {
6778
enqueueTask(() => {
6879
resolve()
6980
})

0 commit comments

Comments
 (0)