Skip to content

Commit

Permalink
Track current event at the beginning of a scheduled task
Browse files Browse the repository at this point in the history
That way we can ignore this event when we log where an update happened.
  • Loading branch information
sebmarkbage committed Nov 14, 2024
1 parent 6cf85c7 commit 30a13cf
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/react-art/src/ReactFiberConfigART.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ export function resolveUpdatePriority(): EventPriority {
return currentUpdatePriority || DefaultEventPriority;
}

export function trackSchedulerEvent(): void {}

export function resolveEventType(): null | string {
return null;
}
Expand Down
9 changes: 7 additions & 2 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,19 @@ export function shouldAttemptEagerTransition(): boolean {
return false;
}

let schedulerEvent: void | Event = undefined;
export function trackSchedulerEvent(): void {
schedulerEvent = window.event;
}

export function resolveEventType(): null | string {
const event = window.event;
return event ? event.type : null;
return event && event !== schedulerEvent ? event.type : null;
}

export function resolveEventTimeStamp(): number {
const event = window.event;
return event ? event.timeStamp : -1.1;
return event && event !== schedulerEvent ? event.timeStamp : -1.1;
}

export const isPrimaryRenderer = true;
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-renderer/src/ReactFiberConfigFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ export function resolveUpdatePriority(): EventPriority {
return DefaultEventPriority;
}

export function trackSchedulerEvent(): void {}

export function resolveEventType(): null | string {
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-renderer/src/ReactFiberConfigNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ export function resolveUpdatePriority(): EventPriority {
return DefaultEventPriority;
}

export function trackSchedulerEvent(): void {}

export function resolveEventType(): null | string {
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
return currentEventPriority;
},

trackSchedulerEvent(): void {},

resolveEventType(): null | string {
return null;
},
Expand Down
14 changes: 14 additions & 0 deletions packages/react-reconciler/src/ReactFiberRootScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
disableSchedulerTimeoutInWorkLoop,
enableProfilerTimer,
enableProfilerNestedUpdatePhase,
enableComponentPerformanceTrack,
enableSiblingPrerendering,
} from 'shared/ReactFeatureFlags';
import {
Expand Down Expand Up @@ -64,6 +65,7 @@ import {
supportsMicrotasks,
scheduleMicrotask,
shouldAttemptEagerTransition,
trackSchedulerEvent,
} from './ReactFiberConfig';

import ReactSharedInternals from 'shared/ReactSharedInternals';
Expand Down Expand Up @@ -225,6 +227,12 @@ function flushSyncWorkAcrossRoots_impl(
}

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

// This function is always called inside a microtask. It should never be
// called synchronously.
didScheduleMicrotask = false;
Expand Down Expand Up @@ -428,6 +436,12 @@ function performWorkOnRootViaSchedulerTask(
resetNestedUpdateFlag();
}

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

// Flush any pending passive effects before deciding which lanes to work on,
// in case they schedule additional work.
const originalCallbackNode = root.callbackNode;
Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import {
setCurrentUpdatePriority,
getCurrentUpdatePriority,
resolveUpdatePriority,
trackSchedulerEvent,
} from './ReactFiberConfig';

import {createWorkInProgress, resetWorkInProgress} from './ReactFiber';
Expand Down Expand Up @@ -3161,6 +3162,11 @@ function commitRootImpl(
// with setTimeout
pendingPassiveTransitions = transitions;
scheduleCallback(NormalSchedulerPriority, () => {
if (enableProfilerTimer && enableComponentPerformanceTrack) {
// Track the currently executing event if there is one so we can ignore this
// event when logging events.
trackSchedulerEvent();
}
flushPassiveEffects(true);
// This render triggered passive effects: release the root cache pool
// *after* passive effects fire to avoid freeing a cache pool that may
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('ReactFiberHostContext', () => {
}
return DefaultEventPriority;
},
trackSchedulerEvent: function () {},
resolveEventType: function () {
return null;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const getInstanceFromScope = $$$config.getInstanceFromScope;
export const setCurrentUpdatePriority = $$$config.setCurrentUpdatePriority;
export const getCurrentUpdatePriority = $$$config.getCurrentUpdatePriority;
export const resolveUpdatePriority = $$$config.resolveUpdatePriority;
export const trackSchedulerEvent = $$$config.trackSchedulerEvent;
export const resolveEventType = $$$config.resolveEventType;
export const resolveEventTimeStamp = $$$config.resolveEventTimeStamp;
export const shouldAttemptEagerTransition =
Expand Down
3 changes: 2 additions & 1 deletion packages/react-test-renderer/src/ReactFiberConfigTestHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ export function resolveUpdatePriority(): EventPriority {
}
return DefaultEventPriority;
}

export function trackSchedulerEvent(): void {}
export function resolveEventType(): null | string {
return null;
}

export function resolveEventTimeStamp(): number {
return -1.1;
}
Expand Down

0 comments on commit 30a13cf

Please sign in to comment.