Skip to content

Commit

Permalink
Avoid importing Scheduler directly (#14757)
Browse files Browse the repository at this point in the history
* Avoid importing Scheduler directly

The reconciler should not depend directly on Scheduler. This adds it to
the host config for the renderer instead.

(Except for `scheduler/tracing` imports, which are used only by the
profiling build. I've left those imports as-is, though I'm open to
directing those through the host config, too.)

* Make throwaway root id longer to appease Brian
  • Loading branch information
acdlite authored Feb 5, 2019
1 parent 81470a0 commit fb3f7bf
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 61 deletions.
6 changes: 6 additions & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import {
unstable_scheduleCallback as scheduleDeferredCallback,
unstable_cancelCallback as cancelDeferredCallback,
} from 'scheduler';
export {
unstable_now as now,
unstable_scheduleCallback as scheduleDeferredCallback,
Expand Down Expand Up @@ -337,6 +341,8 @@ export function getChildHostContext() {
export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
export const schedulePassiveEffects = scheduleDeferredCallback;
export const cancelPassiveEffects = cancelDeferredCallback;

export function shouldSetTextContent(type, props) {
return (
Expand Down
6 changes: 6 additions & 0 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export type ChildSet = void; // Unused
export type TimeoutHandle = TimeoutID;
export type NoTimeout = -1;

import {
unstable_scheduleCallback as scheduleDeferredCallback,
unstable_cancelCallback as cancelDeferredCallback,
} from 'scheduler';
export {
unstable_now as now,
unstable_scheduleCallback as scheduleDeferredCallback,
Expand Down Expand Up @@ -296,6 +300,8 @@ export const scheduleTimeout =
export const cancelTimeout =
typeof clearTimeout === 'function' ? clearTimeout : (undefined: any);
export const noTimeout = -1;
export const schedulePassiveEffects = scheduleDeferredCallback;
export const cancelPassiveEffects = cancelDeferredCallback;

// -------------------
// Mutation
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ export const shouldYield = ReactNativeFrameSchedulingShouldYield;
export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
export const schedulePassiveEffects = scheduleDeferredCallback;
export const cancelPassiveEffects = cancelDeferredCallback;

// -------------------
// Persistence
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ export const shouldYield = ReactNativeFrameSchedulingShouldYield;
export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
export const schedulePassiveEffects = scheduleDeferredCallback;
export const cancelPassiveEffects = cancelDeferredCallback;

export function shouldDeprioritizeSubtree(type: string, props: Props): boolean {
return false;
Expand Down
26 changes: 26 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if (__DEV__) {
function createReactNoop(reconciler: Function, useMutation: boolean) {
let scheduledCallback = null;
let scheduledCallbackTimeout = -1;
let scheduledPassiveCallback = null;
let instanceCounter = 0;
let hostDiffCounter = 0;
let hostUpdateCounter = 0;
Expand Down Expand Up @@ -338,6 +339,21 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
scheduleTimeout: setTimeout,
cancelTimeout: clearTimeout,
noTimeout: -1,
schedulePassiveEffects(callback) {
if (scheduledCallback) {
throw new Error(
'Scheduling a callback twice is excessive. Instead, keep track of ' +
'whether the callback has already been scheduled.',
);
}
scheduledPassiveCallback = callback;
},
cancelPassiveEffects() {
if (scheduledPassiveCallback === null) {
throw new Error('No passive effects callback is scheduled.');
}
scheduledPassiveCallback = null;
},

prepareForCommit(): void {},

Expand Down Expand Up @@ -854,6 +870,16 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
return yieldedValues;
},

flushPassiveEffects() {
// Trick to flush passive effects without exposing an internal API:
// Create a throwaway root and schedule a dummy update on it.
const rootID = 'bloopandthenmoreletterstoavoidaconflict';
const container = {rootID: rootID, children: []};
rootContainers.set(rootID, container);
const root = NoopRenderer.createContainer(container, true, false);
NoopRenderer.updateContainer(null, root, null, null);
},

// Logs the current state of the tree.
dumpTree(rootID: string = DEFAULT_ROOT_ID) {
const root = roots.get(rootID);
Expand Down
12 changes: 6 additions & 6 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import {
__subscriberRef,
unstable_wrap as Schedule_tracing_wrap,
} from 'scheduler/tracing';
import {
unstable_scheduleCallback as Schedule_scheduleCallback,
unstable_cancelCallback as Schedule_cancelCallback,
} from 'scheduler';
import {
invokeGuardedCallback,
hasCaughtError,
Expand Down Expand Up @@ -83,6 +79,8 @@ import {
scheduleTimeout,
cancelTimeout,
noTimeout,
schedulePassiveEffects,
cancelPassiveEffects,
} from './ReactFiberHostConfig';
import {
markPendingPriorityLevel,
Expand Down Expand Up @@ -587,8 +585,10 @@ function markLegacyErrorBoundaryAsFailed(instance: mixed) {
}

function flushPassiveEffects() {
if (passiveEffectCallbackHandle !== null) {
cancelPassiveEffects(passiveEffectCallbackHandle);
}
if (passiveEffectCallback !== null) {
Schedule_cancelCallback(passiveEffectCallbackHandle);
// We call the scheduled callback instead of commitPassiveEffects directly
// to ensure tracing works correctly.
passiveEffectCallback();
Expand Down Expand Up @@ -795,7 +795,7 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
// here because that code is still in flux.
callback = Schedule_tracing_wrap(callback);
}
passiveEffectCallbackHandle = Schedule_scheduleCallback(callback);
passiveEffectCallbackHandle = schedulePassiveEffects(callback);
passiveEffectCallback = callback;
}

Expand Down
Loading

0 comments on commit fb3f7bf

Please sign in to comment.