Skip to content

Commit

Permalink
[scheduler] 4/n Allow splitting out schedule in fb-www, prepare to …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
flarnie authored May 29, 2018
1 parent 001f9ef commit ff724d3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import * as ReactScheduler from 'react-scheduler';
import * as ReactScheduler from 'shared/ReactScheduler';
import Transform from 'art/core/transform';
import Mode from 'art/modes/current';
import invariant from 'fbjs/lib/invariant';
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

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

import * as ReactDOMComponentTree from './ReactDOMComponentTree';
import * as ReactDOMFiberComponent from './ReactDOMFiberComponent';
Expand Down
12 changes: 10 additions & 2 deletions packages/react-scheduler/src/ReactScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ export type CallbackIdType = CallbackConfigType;
import requestAnimationFrameForReact from 'shared/requestAnimationFrameForReact';
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';

// We capture a local reference to any global, in case it gets polyfilled after
// this module is initially evaluated.
// We want to be using a consistent implementation.
const Date = global.Date;
const setTimeout = global.setTimeout;
const clearTimeout = global.clearTimeout;

const hasNativePerformanceNow =
typeof performance === 'object' && typeof performance.now === 'function';

let now;
if (hasNativePerformanceNow) {
const Performance = performance;
now = function() {
return performance.now();
return Performance.now();
};
} else {
now = function() {
Expand Down Expand Up @@ -224,7 +232,7 @@ if (!ExecutionEnvironment.canUseDOM) {
};
// Assumes that we have addEventListener in this environment. Might need
// something better for old IE.
window.addEventListener('message', idleTick, false);
global.addEventListener('message', idleTick, false);

const animationTick = function(rafTime) {
isAnimationFrameScheduled = false;
Expand Down
17 changes: 17 additions & 0 deletions packages/shared/ReactScheduler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';
import {
now,
scheduleWork,
cancelScheduledWork,
} from 'react-scheduler/src/ReactScheduler';

export {now, scheduleWork, cancelScheduledWork};
11 changes: 11 additions & 0 deletions packages/shared/forks/ReactScheduler.www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';
const {now, scheduleWork, cancelScheduledWork} = require('customSchedule');

export {now, scheduleWork, cancelScheduledWork};
5 changes: 5 additions & 0 deletions packages/shared/requestAnimationFrameForReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import warning from 'fbjs/lib/warning';

// We capture a local reference to any global, in case it gets polyfilled after
// this module is initially evaluated.
// We want to be using a consistent implementation.
const requestAnimationFrame = global.requestAnimationFrame;

if (__DEV__) {
if (
ExecutionEnvironment.canUseDOM &&
Expand Down
9 changes: 8 additions & 1 deletion scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,14 @@ const bundles = [
/******* React Scheduler (experimental) *******/
{
label: 'react-scheduler',
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
bundleTypes: [
UMD_DEV,
UMD_PROD,
NODE_DEV,
NODE_PROD,
FB_WWW_DEV,
FB_WWW_PROD,
],
moduleType: ISOMORPHIC,
entry: 'react-scheduler',
global: 'ReactScheduler',
Expand Down
11 changes: 11 additions & 0 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const forks = Object.freeze({
},

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

'shared/ReactScheduler': (bundleType, entry) => {
switch (bundleType) {
case FB_WWW_DEV:
case FB_WWW_PROD:
return 'shared/forks/ReactScheduler.www.js';
default:
return null;
}
},

// This logic is forked on www to blacklist warnings.
'shared/lowPriorityWarning': (bundleType, entry) => {
switch (bundleType) {
Expand Down

0 comments on commit ff724d3

Please sign in to comment.