Skip to content

Commit

Permalink
Don't call trackSchedulerEvent() in microtasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Dec 20, 2024
1 parent 2031209 commit 3709ba4
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/react-reconciler/src/ReactFiberRootScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ export function ensureRootIsScheduled(root: FiberRoot): void {
// We're inside an `act` scope.
if (!didScheduleMicrotask_act) {
didScheduleMicrotask_act = true;
scheduleImmediateTask(processRootScheduleInMicrotask);
scheduleImmediateRootScheduleTask();
}
} else {
if (!didScheduleMicrotask) {
didScheduleMicrotask = true;
scheduleImmediateTask(processRootScheduleInMicrotask);
scheduleImmediateRootScheduleTask();
}
}

Expand Down Expand Up @@ -229,13 +229,17 @@ function flushSyncWorkAcrossRoots_impl(
isFlushingWork = false;
}

function processRootScheduleInMicrotask() {
function processRootScheduleInImmediateTask() {
if (enableProfilerTimer && enableComponentPerformanceTrack) {
// Track the currently executing event if there is one so we can ignore this
// event when logging events.
trackSchedulerEvent();
}

processRootScheduleInMicrotask();
}

function processRootScheduleInMicrotask() {
// This function is always called inside a microtask. It should never be
// called synchronously.
didScheduleMicrotask = false;
Expand Down Expand Up @@ -558,15 +562,15 @@ function cancelCallback(callbackNode: mixed) {
}
}

function scheduleImmediateTask(cb: () => mixed) {
function scheduleImmediateRootScheduleTask() {
if (__DEV__ && ReactSharedInternals.actQueue !== null) {
// Special case: Inside an `act` scope, we push microtasks to the fake `act`
// callback queue. This is because we currently support calling `act`
// without awaiting the result. The plan is to deprecate that, and require
// that you always await the result so that the microtasks have a chance to
// run. But it hasn't happened yet.
ReactSharedInternals.actQueue.push(() => {
cb();
processRootScheduleInMicrotask();
return null;
});
}
Expand All @@ -588,14 +592,20 @@ function scheduleImmediateTask(cb: () => mixed) {
// wrong semantically but it prevents an infinite loop. The bug is
// Safari's, not ours, so we just do our best to not crash even though
// the behavior isn't completely correct.
Scheduler_scheduleCallback(ImmediateSchedulerPriority, cb);
Scheduler_scheduleCallback(
ImmediateSchedulerPriority,
processRootScheduleInImmediateTask,
);
return;
}
cb();
processRootScheduleInMicrotask();
});
} else {
// If microtasks are not supported, use Scheduler.
Scheduler_scheduleCallback(ImmediateSchedulerPriority, cb);
Scheduler_scheduleCallback(
ImmediateSchedulerPriority,
processRootScheduleInImmediateTask,
);
}
}

Expand Down

0 comments on commit 3709ba4

Please sign in to comment.