Skip to content

Commit fb3f7bf

Browse files
authored
Avoid importing Scheduler directly (#14757)
* 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
1 parent 81470a0 commit fb3f7bf

9 files changed

Lines changed: 84 additions & 61 deletions

File tree

‎packages/react-art/src/ReactARTHostConfig.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import{
9+
unstable_scheduleCallbackasscheduleDeferredCallback,
10+
unstable_cancelCallbackascancelDeferredCallback,
11+
}from'scheduler';
812
export{
913
unstable_nowasnow,
1014
unstable_scheduleCallbackasscheduleDeferredCallback,
@@ -337,6 +341,8 @@ export function getChildHostContext() {
337341
exportconstscheduleTimeout=setTimeout;
338342
exportconstcancelTimeout=clearTimeout;
339343
exportconstnoTimeout=-1;
344+
exportconstschedulePassiveEffects=scheduleDeferredCallback;
345+
exportconstcancelPassiveEffects=cancelDeferredCallback;
340346

341347
exportfunctionshouldSetTextContent(type,props){
342348
return(

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export type ChildSet = void; // Unused
6969
exporttypeTimeoutHandle=TimeoutID;
7070
exporttypeNoTimeout=-1;
7171

72+
import{
73+
unstable_scheduleCallbackasscheduleDeferredCallback,
74+
unstable_cancelCallbackascancelDeferredCallback,
75+
}from'scheduler';
7276
export{
7377
unstable_nowasnow,
7478
unstable_scheduleCallbackasscheduleDeferredCallback,
@@ -296,6 +300,8 @@ export const scheduleTimeout =
296300
exportconstcancelTimeout=
297301
typeofclearTimeout==='function' ? clearTimeout : (undefined: any);
298302
exportconstnoTimeout=-1;
303+
exportconstschedulePassiveEffects=scheduleDeferredCallback;
304+
exportconstcancelPassiveEffects=cancelDeferredCallback;
299305

300306
// -------------------
301307
// Mutation

‎packages/react-native-renderer/src/ReactFabricHostConfig.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ export const shouldYield = ReactNativeFrameSchedulingShouldYield;
330330
export const scheduleTimeout = setTimeout;
331331
export const cancelTimeout = clearTimeout;
332332
export const noTimeout = -1;
333+
export const schedulePassiveEffects = scheduleDeferredCallback;
334+
export const cancelPassiveEffects = cancelDeferredCallback;
333335

334336
// -------------------
335337
// Persistence

‎packages/react-native-renderer/src/ReactNativeHostConfig.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ export const shouldYield = ReactNativeFrameSchedulingShouldYield;
243243
exportconstscheduleTimeout=setTimeout;
244244
exportconstcancelTimeout=clearTimeout;
245245
exportconstnoTimeout=-1;
246+
exportconstschedulePassiveEffects=scheduleDeferredCallback;
247+
exportconstcancelPassiveEffects=cancelDeferredCallback;
246248

247249
exportfunctionshouldDeprioritizeSubtree(type: string,props: Props): boolean{
248250
returnfalse;

‎packages/react-noop-renderer/src/createReactNoop.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ if (__DEV__) {
4747
functioncreateReactNoop(reconciler: Function,useMutation: boolean){
4848
letscheduledCallback=null;
4949
letscheduledCallbackTimeout=-1;
50+
letscheduledPassiveCallback=null;
5051
letinstanceCounter=0;
5152
lethostDiffCounter=0;
5253
lethostUpdateCounter=0;
@@ -338,6 +339,21 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
338339
scheduleTimeout: setTimeout,
339340
cancelTimeout: clearTimeout,
340341
noTimeout: -1,
342+
schedulePassiveEffects(callback){
343+
if(scheduledCallback){
344+
thrownewError(
345+
'Scheduling a callback twice is excessive. Instead, keep track of '+
346+
'whether the callback has already been scheduled.',
347+
);
348+
}
349+
scheduledPassiveCallback=callback;
350+
},
351+
cancelPassiveEffects(){
352+
if(scheduledPassiveCallback===null){
353+
thrownewError('No passive effects callback is scheduled.');
354+
}
355+
scheduledPassiveCallback =null;
356+
},
341357

342358
prepareForCommit(): void{},
343359

@@ -854,6 +870,16 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
854870
returnyieldedValues;
855871
},
856872

873+
flushPassiveEffects(){
874+
// Trick to flush passive effects without exposing an internal API:
875+
// Create a throwaway root and schedule a dummy update on it.
876+
constrootID='bloopandthenmoreletterstoavoidaconflict';
877+
constcontainer={rootID: rootID,children: []};
878+
rootContainers.set(rootID,container);
879+
constroot=NoopRenderer.createContainer(container,true,false);
880+
NoopRenderer.updateContainer(null,root,null,null);
881+
},
882+
857883
// Logs the current state of the tree.
858884
dumpTree(rootID: string=DEFAULT_ROOT_ID){
859885
constroot=roots.get(rootID);

‎packages/react-reconciler/src/ReactFiberScheduler.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import {
1717
__subscriberRef,
1818
unstable_wrapasSchedule_tracing_wrap,
1919
}from'scheduler/tracing';
20-
import{
21-
unstable_scheduleCallbackasSchedule_scheduleCallback,
22-
unstable_cancelCallbackasSchedule_cancelCallback,
23-
}from'scheduler';
2420
import{
2521
invokeGuardedCallback,
2622
hasCaughtError,
@@ -83,6 +79,8 @@ import {
8379
scheduleTimeout,
8480
cancelTimeout,
8581
noTimeout,
82+
schedulePassiveEffects,
83+
cancelPassiveEffects,
8684
}from'./ReactFiberHostConfig';
8785
import{
8886
markPendingPriorityLevel,
@@ -587,8 +585,10 @@ function markLegacyErrorBoundaryAsFailed(instance: mixed) {
587585
}
588586

589587
functionflushPassiveEffects(){
588+
if(passiveEffectCallbackHandle!==null){
589+
cancelPassiveEffects(passiveEffectCallbackHandle);
590+
}
590591
if(passiveEffectCallback!==null){
591-
Schedule_cancelCallback(passiveEffectCallbackHandle);
592592
// We call the scheduled callback instead of commitPassiveEffects directly
593593
// to ensure tracing works correctly.
594594
passiveEffectCallback();
@@ -795,7 +795,7 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void {
795795
// here because that code is still in flux.
796796
callback=Schedule_tracing_wrap(callback);
797797
}
798-
passiveEffectCallbackHandle=Schedule_scheduleCallback(callback);
798+
passiveEffectCallbackHandle=schedulePassiveEffects(callback);
799799
passiveEffectCallback=callback;
800800
}
801801

0 commit comments

Comments
 (0)