Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Working on ^16.9 and lower than 16.8
  • Loading branch information
MatanBobi committed Jul 4, 2020
commit 5ef6cb8a813b505b0cd9c8018cfabe8b57b1ccfb
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@reach/router": "^1.3.3",
"@testing-library/jest-dom": "^5.10.1",
"@types/react-dom": "^16.9.8",
"@types/scheduler": "^0.16.1",
"dotenv-cli": "^3.1.0",
"dtslint": "3.6.12",
"kcd-scripts": "^6.2.3",
Expand All @@ -62,8 +61,7 @@
},
"peerDependencies": {
"react": "*",
"react-dom": "*",
"scheduler": "*"
"react-dom": "*"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
Expand Down
21 changes: 16 additions & 5 deletions src/flush-microtasks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
unstable_scheduleCallback as scheduleCallback,
unstable_NormalPriority as normalPriority,
} from 'scheduler'
/* 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 @@ -21,6 +17,10 @@ function getIsUsingFakeTimers() {

let didWarnAboutMessageChannel = false
let enqueueTask
const globalObj = typeof window === 'undefined' ? global : window

let Scheduler = globalObj.Scheduler

try {
// read require off the module object to get around the bundlers.
// we don't want them to detect a require and bundle a Node polyfill.
Expand All @@ -29,6 +29,8 @@ 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 @@ -53,6 +55,15 @@ try {
}
}

// in case this react version has a Scheduler implementation, we use it,
// if not, we just create a function calling our callback
const scheduleCallback = Scheduler
? Scheduler.scheduleCallback || Scheduler.unstable_scheduleCallback
: (_, cb) => cb()
const NormalPriority = Scheduler
? Scheduler.NormalPriority || Scheduler.unstable_NormalPriority
: null

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