Skip to content

Commit ff724d3

Browse files
authored
[scheduler] 4/n Allow splitting out schedule in fb-www, prepare to fix polyfill issue internally (#12900)
* Use local references to global things inside 'scheduler' **what is the change?:** See title **why make this change?:** We want to avoid initially calling one version of an API and then later accessing a polyfilled version. **test plan:** Run existing tests. * Shim ReactScheduler for www **what is the change?:** In 'www' we want to reference the separate build of ReactScheduler, which allows treating it as a separate module internally. **why make this change?:** We need to require the ReactScheduler before our rAF polyfill activates, in order to customize which custom behaviors we want. This is also a step towards being able to experiment with using it outside of React. **test plan:** Ran tests, ran the build, and ran `test-build`. * Generate a bundle for fb-www **what is the change?:** See title **why make this change?:** Splitting out the 'schedule' module allows us to load it before polyfills kick in for rAF and other APIs. And long term we want to split this into a separate module anyway, this is a step towards that. **test plan:** I'll run the sync next week and verify that this all works. :) * ran prettier * fix rebase issues * Change names of variables used for holding globals
1 parent 001f9ef commit ff724d3

File tree

8 files changed

+64
-5
lines changed

8 files changed

+64
-5
lines changed

packages/react-art/src/ReactARTHostConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import * as ReactScheduler from 'react-scheduler';
8+
import * as ReactScheduler from 'shared/ReactScheduler';
99
import Transform from 'art/core/transform';
1010
import Mode from 'art/modes/current';
1111
import invariant from 'fbjs/lib/invariant';

packages/react-dom/src/client/ReactDOMHostConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import * as ReactScheduler from 'react-scheduler';
10+
import * as ReactScheduler from 'shared/ReactScheduler';
1111

1212
import * as ReactDOMComponentTree from './ReactDOMComponentTree';
1313
import * as ReactDOMFiberComponent from './ReactDOMFiberComponent';

packages/react-scheduler/src/ReactScheduler.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ export type CallbackIdType = CallbackConfigType;
4444
import requestAnimationFrameForReact from 'shared/requestAnimationFrameForReact';
4545
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
4646

47+
// We capture a local reference to any global, in case it gets polyfilled after
48+
// this module is initially evaluated.
49+
// We want to be using a consistent implementation.
50+
const Date = global.Date;
51+
const setTimeout = global.setTimeout;
52+
const clearTimeout = global.clearTimeout;
53+
4754
const hasNativePerformanceNow =
4855
typeof performance === 'object' && typeof performance.now === 'function';
4956

5057
let now;
5158
if (hasNativePerformanceNow) {
59+
const Performance = performance;
5260
now = function() {
53-
return performance.now();
61+
return Performance.now();
5462
};
5563
} else {
5664
now = function() {
@@ -224,7 +232,7 @@ if (!ExecutionEnvironment.canUseDOM) {
224232
};
225233
// Assumes that we have addEventListener in this environment. Might need
226234
// something better for old IE.
227-
window.addEventListener('message', idleTick, false);
235+
global.addEventListener('message', idleTick, false);
228236

229237
const animationTick = function(rafTime) {
230238
isAnimationFrameScheduled = false;

packages/shared/ReactScheduler.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
import {
12+
now,
13+
scheduleWork,
14+
cancelScheduledWork,
15+
} from 'react-scheduler/src/ReactScheduler';
16+
17+
export {now, scheduleWork, cancelScheduledWork};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
const {now, scheduleWork, cancelScheduledWork} = require('customSchedule');
10+
11+
export {now, scheduleWork, cancelScheduledWork};

packages/shared/requestAnimationFrameForReact.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
1313
import warning from 'fbjs/lib/warning';
1414

15+
// We capture a local reference to any global, in case it gets polyfilled after
16+
// this module is initially evaluated.
17+
// We want to be using a consistent implementation.
18+
const requestAnimationFrame = global.requestAnimationFrame;
19+
1520
if (__DEV__) {
1621
if (
1722
ExecutionEnvironment.canUseDOM &&

scripts/rollup/bundles.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,14 @@ const bundles = [
371371
/******* React Scheduler (experimental) *******/
372372
{
373373
label: 'react-scheduler',
374-
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
374+
bundleTypes: [
375+
UMD_DEV,
376+
UMD_PROD,
377+
NODE_DEV,
378+
NODE_PROD,
379+
FB_WWW_DEV,
380+
FB_WWW_PROD,
381+
],
375382
moduleType: ISOMORPHIC,
376383
entry: 'react-scheduler',
377384
global: 'ReactScheduler',

scripts/rollup/forks.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const forks = Object.freeze({
7979
},
8080

8181
// This logic is forked on www to use the 'acrossTransitions' version.
82+
// This will be removed soon, see internal task T29442940
8283
'shared/requestAnimationFrameForReact': (bundleType, entry) => {
8384
switch (bundleType) {
8485
case FB_WWW_DEV:
@@ -89,6 +90,16 @@ const forks = Object.freeze({
8990
}
9091
},
9192

93+
'shared/ReactScheduler': (bundleType, entry) => {
94+
switch (bundleType) {
95+
case FB_WWW_DEV:
96+
case FB_WWW_PROD:
97+
return 'shared/forks/ReactScheduler.www.js';
98+
default:
99+
return null;
100+
}
101+
},
102+
92103
// This logic is forked on www to blacklist warnings.
93104
'shared/lowPriorityWarning': (bundleType, entry) => {
94105
switch (bundleType) {

0 commit comments

Comments
 (0)