diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js index ea425725250ed..b7c92ae72d703 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js @@ -89,6 +89,7 @@ import { getCurrentEventPriority, supportsMicrotasks, errorHydratingContainer, + scheduleMicrotask, } from './ReactFiberHostConfig'; import { @@ -696,6 +697,13 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { // Special case: Sync React callbacks are scheduled on a special // internal queue scheduleSyncCallback(performSyncWorkOnRoot.bind(null, root)); + if (supportsMicrotasks) { + // Flush the queue in a microtask. + scheduleMicrotask(flushSyncCallbackQueue); + } else { + // Flush the queue in an Immediate task. + scheduleCallback(ImmediateSchedulerPriority, flushSyncCallbackQueue); + } newCallbackNode = null; } else if (newCallbackPriority === SyncBatchedLanePriority) { newCallbackNode = scheduleCallback( diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js index c29c9353b1732..f84e5b2eed569 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js @@ -89,6 +89,7 @@ import { getCurrentEventPriority, supportsMicrotasks, errorHydratingContainer, + scheduleMicrotask, } from './ReactFiberHostConfig'; import { @@ -696,6 +697,13 @@ function ensureRootIsScheduled(root: FiberRoot, currentTime: number) { // Special case: Sync React callbacks are scheduled on a special // internal queue scheduleSyncCallback(performSyncWorkOnRoot.bind(null, root)); + if (supportsMicrotasks) { + // Flush the queue in a microtask. + scheduleMicrotask(flushSyncCallbackQueue); + } else { + // Flush the queue in an Immediate task. + scheduleCallback(ImmediateSchedulerPriority, flushSyncCallbackQueue); + } newCallbackNode = null; } else if (newCallbackPriority === SyncBatchedLanePriority) { newCallbackNode = scheduleCallback( diff --git a/packages/react-reconciler/src/SchedulerWithReactIntegration.new.js b/packages/react-reconciler/src/SchedulerWithReactIntegration.new.js index 70f26e5fc4167..9efa57d327ef5 100644 --- a/packages/react-reconciler/src/SchedulerWithReactIntegration.new.js +++ b/packages/react-reconciler/src/SchedulerWithReactIntegration.new.js @@ -20,7 +20,6 @@ import { getCurrentUpdateLanePriority, setCurrentUpdateLanePriority, } from './ReactFiberLane.new'; -import {scheduleMicrotask, supportsMicrotasks} from './ReactFiberHostConfig'; const { unstable_scheduleCallback: Scheduler_scheduleCallback, @@ -71,7 +70,6 @@ export const requestPaint = Scheduler_requestPaint !== undefined ? Scheduler_requestPaint : () => {}; let syncQueue: Array | null = null; -let immediateQueueCallbackNode: mixed | null = null; let isFlushingSyncQueue: boolean = false; const initialTimeMs: number = Scheduler_now(); @@ -133,19 +131,6 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { // the next tick, or earlier if something calls `flushSyncCallbackQueue`. if (syncQueue === null) { syncQueue = [callback]; - - // TODO: Figure out how to remove this It's only here as a last resort if we - // forget to explicitly flush. - if (supportsMicrotasks) { - // Flush the queue in a microtask. - scheduleMicrotask(flushSyncCallbackQueueImpl); - } else { - // Flush the queue in the next tick. - immediateQueueCallbackNode = Scheduler_scheduleCallback( - Scheduler_ImmediatePriority, - flushSyncCallbackQueueImpl, - ); - } } else { // Push onto existing queue. Don't need to schedule a callback because // we already scheduled one when we created the queue. @@ -158,15 +143,6 @@ export function cancelCallback(callbackNode: mixed) { } export function flushSyncCallbackQueue() { - if (immediateQueueCallbackNode !== null) { - const node = immediateQueueCallbackNode; - immediateQueueCallbackNode = null; - Scheduler_cancelCallback(node); - } - flushSyncCallbackQueueImpl(); -} - -function flushSyncCallbackQueueImpl() { if (!isFlushingSyncQueue && syncQueue !== null) { // Prevent re-entrancy. isFlushingSyncQueue = true; @@ -199,4 +175,5 @@ function flushSyncCallbackQueueImpl() { isFlushingSyncQueue = false; } } + return null; } diff --git a/packages/react-reconciler/src/SchedulerWithReactIntegration.old.js b/packages/react-reconciler/src/SchedulerWithReactIntegration.old.js index 3d0d0920fd22a..bde9672f329ea 100644 --- a/packages/react-reconciler/src/SchedulerWithReactIntegration.old.js +++ b/packages/react-reconciler/src/SchedulerWithReactIntegration.old.js @@ -20,7 +20,6 @@ import { getCurrentUpdateLanePriority, setCurrentUpdateLanePriority, } from './ReactFiberLane.old'; -import {scheduleMicrotask, supportsMicrotasks} from './ReactFiberHostConfig'; const { unstable_scheduleCallback: Scheduler_scheduleCallback, @@ -71,7 +70,6 @@ export const requestPaint = Scheduler_requestPaint !== undefined ? Scheduler_requestPaint : () => {}; let syncQueue: Array | null = null; -let immediateQueueCallbackNode: mixed | null = null; let isFlushingSyncQueue: boolean = false; const initialTimeMs: number = Scheduler_now(); @@ -133,19 +131,6 @@ export function scheduleSyncCallback(callback: SchedulerCallback) { // the next tick, or earlier if something calls `flushSyncCallbackQueue`. if (syncQueue === null) { syncQueue = [callback]; - - // TODO: Figure out how to remove this It's only here as a last resort if we - // forget to explicitly flush. - if (supportsMicrotasks) { - // Flush the queue in a microtask. - scheduleMicrotask(flushSyncCallbackQueueImpl); - } else { - // Flush the queue in the next tick. - immediateQueueCallbackNode = Scheduler_scheduleCallback( - Scheduler_ImmediatePriority, - flushSyncCallbackQueueImpl, - ); - } } else { // Push onto existing queue. Don't need to schedule a callback because // we already scheduled one when we created the queue. @@ -158,15 +143,6 @@ export function cancelCallback(callbackNode: mixed) { } export function flushSyncCallbackQueue() { - if (immediateQueueCallbackNode !== null) { - const node = immediateQueueCallbackNode; - immediateQueueCallbackNode = null; - Scheduler_cancelCallback(node); - } - flushSyncCallbackQueueImpl(); -} - -function flushSyncCallbackQueueImpl() { if (!isFlushingSyncQueue && syncQueue !== null) { // Prevent re-entrancy. isFlushingSyncQueue = true; @@ -199,4 +175,5 @@ function flushSyncCallbackQueueImpl() { isFlushingSyncQueue = false; } } + return null; }