Skip to content

Commit

Permalink
Initial commit (facebook#12624)
Browse files Browse the repository at this point in the history
This is the first step - pulling the ReactDOMFrameScheduling module out
into a separate package.

Co-authored-by: Brandon Dail <aweary@users.noreply.github.com>
  • Loading branch information
2 people authored and rhagigi committed Apr 19, 2018
1 parent 8434ddf commit 0c65777
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/react-art/src/ReactART.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import ReactFiberReconciler from 'react-reconciler';
import * as ReactDOMFrameScheduling from 'shared/ReactDOMFrameScheduling';
import * as ReactScheduler from 'react-scheduler';
import Mode from 'art/modes/current';
import FastNoSideEffects from 'art/modes/fast-noSideEffects';
import Transform from 'art/core/transform';
Expand Down Expand Up @@ -468,15 +468,15 @@ const ARTRenderer = ReactFiberReconciler({
return emptyObject;
},

scheduleDeferredCallback: ReactDOMFrameScheduling.rIC,
scheduleDeferredCallback: ReactScheduler.rIC,

shouldSetTextContent(type, props) {
return (
typeof props.children === 'string' || typeof props.children === 'number'
);
},

now: ReactDOMFrameScheduling.now,
now: ReactScheduler.now,

mutation: {
appendChild(parentInstance, child) {
Expand Down
35 changes: 35 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,39 @@ describe('ReactDOM', () => {
Object.defineProperty(global, 'document', documentDescriptor);
}
});

it('warns when requestAnimationFrame is not polyfilled in the browser', () => {
const previousRAF = global.requestAnimationFrame;
try {
global.requestAnimationFrame = undefined;
jest.resetModules();
expect(() => require('react-dom')).toWarnDev(
'React depends on requestAnimationFrame.',
);
} finally {
global.requestAnimationFrame = previousRAF;
}
});

// We're just testing importing, not using it.
// It is important because even isomorphic components may import it.
it('can import findDOMNode in Node environment', () => {
const previousRAF = global.requestAnimationFrame;
const previousRIC = global.requestIdleCallback;
const prevWindow = global.window;
try {
global.requestAnimationFrame = undefined;
global.requestIdleCallback = undefined;
// Simulate the Node environment:
delete global.window;
jest.resetModules();
expect(() => {
require('react-dom');
}).not.toThrow();
} finally {
global.requestAnimationFrame = previousRAF;
global.requestIdleCallback = previousRIC;
global.window = prevWindow;
}
});
});
8 changes: 4 additions & 4 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as EventPluginRegistry from 'events/EventPluginRegistry';
import * as EventPropagators from 'events/EventPropagators';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
import ReactVersion from 'shared/ReactVersion';
import * as ReactDOMFrameScheduling from 'shared/ReactDOMFrameScheduling';
import * as ReactScheduler from 'react-scheduler';
import {ReactCurrentOwner} from 'shared/ReactGlobalSharedState';
import getComponentName from 'shared/getComponentName';
import invariant from 'fbjs/lib/invariant';
Expand Down Expand Up @@ -688,7 +688,7 @@ const DOMRenderer = ReactFiberReconciler({
return textNode;
},

now: ReactDOMFrameScheduling.now,
now: ReactScheduler.now,

mutation: {
commitMount(
Expand Down Expand Up @@ -984,8 +984,8 @@ const DOMRenderer = ReactFiberReconciler({
},
},

scheduleDeferredCallback: ReactDOMFrameScheduling.rIC,
cancelDeferredCallback: ReactDOMFrameScheduling.cIC,
scheduleDeferredCallback: ReactScheduler.rIC,
cancelDeferredCallback: ReactScheduler.cIC,
});

ReactGenericBatching.injection.injectRenderer(DOMRenderer);
Expand Down
4 changes: 4 additions & 0 deletions packages/react-scheduler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# React Scheduler

This is a work in progress - we are building a utility to better coordinate
React and other JavaScript work.
12 changes: 12 additions & 0 deletions packages/react-scheduler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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';

export * from './src/ReactScheduler';
7 changes: 7 additions & 0 deletions packages/react-scheduler/npm/index.js
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/react-scheduler.production.min.js');
} else {
module.exports = require('./cjs/react-scheduler.development.js');
}
23 changes: 23 additions & 0 deletions packages/react-scheduler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "react-scheduler",
"version": "0.1.0-alpha-1",
"private": true,
"description": "unstable scheduling helper for coordinating React and other JS libraries",
"main": "index.js",
"repository": "facebook/react",
"license": "MIT",
"keywords": [
"react"
],
"bugs": {
"url": "https://github.com/facebook/react/issues"
},
"homepage": "https://reactjs.org/",
"files": [
"LICENSE",
"README.md",
"index.js",
"cjs/",
"umd/"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
* @flow
*/

'use strict';

/**
* A scheduling library to allow scheduling work with more granular priority and
* control than requestAnimationFrame and requestIdleCallback.
* Current TODO items:
* X- Pull out the rIC polyfill built into React
* - Initial test coverage
* - Support for multiple callbacks
* - Support for two priorities; serial and deferred
* - Better test coverage
* - Better docblock
* - Polish documentation, API
*/

// This is a built-in polyfill for requestIdleCallback. It works by scheduling
// a requestAnimationFrame, storing the time for the start of the frame, then
// scheduling a postMessage which gets scheduled after paint. Within the
Expand Down
10 changes: 10 additions & 0 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ const bundles = [
global: 'createSubscription',
externals: ['react'],
},

/******* React Scheduler (experimental) *******/
{
label: 'react-scheduler',
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
moduleType: ISOMORPHIC,
entry: 'react-scheduler',
global: 'ReactScheduler',
externals: [],
},
];

// Based on deep-freeze by substack (public domain)
Expand Down

0 comments on commit 0c65777

Please sign in to comment.