forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed scheduler setTimeout fallback (facebook#14358)
* Fixed scheduler setTimeout fallback * Moved unit-test-specific setTimeout code into a new NPM package, jest-mock-scheduler.
- Loading branch information
Showing
11 changed files
with
156 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# `jest-mock-scheduler` | ||
|
||
Jest matchers and utilities for testing the `scheduler` package. | ||
|
||
This package is experimental. APIs may change between releases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export * from './src/JestMockScheduler'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/jest-mock-scheduler.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/jest-mock-scheduler.development.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "jest-mock-scheduler", | ||
"private": true, | ||
"version": "0.1.0", | ||
"description": "Jest matchers and utilities for testing the scheduler package.", | ||
"main": "index.js", | ||
"repository": "facebook/react", | ||
"keywords": [ | ||
"jest", | ||
"scheduler" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/facebook/react/issues" | ||
}, | ||
"homepage": "https://reactjs.org/", | ||
"peerDependencies": { | ||
"jest": "^23.0.1", | ||
"scheduler": "^0.11.0" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"build-info.json", | ||
"index.js", | ||
"cjs/" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// Max 31 bit integer. The max integer size in V8 for 32-bit systems. | ||
// Math.pow(2, 30) - 1 | ||
// 0b111111111111111111111111111111 | ||
const maxSigned31BitInt = 1073741823; | ||
|
||
export function mockRestore() { | ||
delete global._schedMock; | ||
} | ||
|
||
let callback = null; | ||
let currentTime = -1; | ||
|
||
function flushCallback(didTimeout, ms) { | ||
if (callback !== null) { | ||
let cb = callback; | ||
callback = null; | ||
try { | ||
currentTime = ms; | ||
cb(didTimeout); | ||
} finally { | ||
currentTime = -1; | ||
} | ||
} | ||
} | ||
|
||
function requestHostCallback(cb, ms) { | ||
if (currentTime !== -1) { | ||
// Protect against re-entrancy. | ||
setTimeout(requestHostCallback, 0, cb, ms); | ||
} else { | ||
callback = cb; | ||
setTimeout(flushCallback, ms, true, ms); | ||
setTimeout(flushCallback, maxSigned31BitInt, false, maxSigned31BitInt); | ||
} | ||
} | ||
|
||
function cancelHostCallback() { | ||
callback = null; | ||
} | ||
|
||
function shouldYieldToHost() { | ||
return false; | ||
} | ||
|
||
function getCurrentTime() { | ||
return currentTime === -1 ? 0 : currentTime; | ||
} | ||
|
||
global._schedMock = [ | ||
requestHostCallback, | ||
cancelHostCallback, | ||
shouldYieldToHost, | ||
getCurrentTime, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters