Skip to content

Commit

Permalink
add transition progress callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaruan committed Jun 30, 2022
1 parent 4cd788a commit 62a7daf
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 8 deletions.
24 changes: 21 additions & 3 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ import {
enqueuePendingPassiveProfilerEffect,
restorePendingUpdaters,
addTransitionStartCallbackToPendingTransition,
addTransitionProgressCallbackToPendingTransition,
addTransitionCompleteCallbackToPendingTransition,
addMarkerCompleteCallbackToPendingTransition,
setIsRunningInsertionEffect,
Expand Down Expand Up @@ -1093,6 +1094,7 @@ function commitTransitionProgress(offscreenFiber: Fiber) {
const isHidden = nextState !== null;

const pendingMarkers = offscreenInstance.pendingMarkers;
const transitions = offscreenInstance.transitions;
// If there is a name on the suspense boundary, store that in
// the pending boundaries.
let name = null;
Expand All @@ -1110,9 +1112,18 @@ function commitTransitionProgress(offscreenFiber: Fiber) {
// to the pending boundary set if it's there
if (pendingMarkers !== null) {
pendingMarkers.forEach(pendingBoundaries => {
pendingBoundaries.set(offscreenInstance, {
name,
});
if (!pendingBoundaries.has(offscreenInstance)) {
pendingBoundaries.set(offscreenInstance, {
name,
});

transitions.forEach(transition => {
addTransitionProgressCallbackToPendingTransition({
transition,
pending: pendingBoundaries,
});
});
}
});
}
} else if (wasHidden && !isHidden) {
Expand All @@ -1123,6 +1134,13 @@ function commitTransitionProgress(offscreenFiber: Fiber) {
pendingMarkers.forEach(pendingBoundaries => {
if (pendingBoundaries.has(offscreenInstance)) {
pendingBoundaries.delete(offscreenInstance);

transitions.forEach(transition => {
addTransitionProgressCallbackToPendingTransition({
transition,
pending: pendingBoundaries,
});
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export type TransitionObject = {
startTime: number,
};

export type TransitionProgressObject = {
transition: TransitionObject,
pending: PendingSuspenseBoundaries,
};

export type MarkerTransitionObject = TransitionObject & {markerName: string};
export type PendingTransitionCallbacks = {
transitionStart: Array<TransitionObject> | null,
transitionProgress: Array<TransitionProgressObject> | null,
transitionComplete: Array<TransitionObject> | null,
markerComplete: Array<MarkerTransitionObject> | null,
};
Expand Down Expand Up @@ -80,6 +86,25 @@ export function processTransitionCallbacks(
});
}

const transitionProgress = pendingTransitions.transitionProgress;
if (transitionProgress !== null) {
const logged = new Set();
transitionProgress.forEach(({transition, pending}) => {
if (
callbacks.onTransitionProgress != null &&
!logged.has(transition)
) {
callbacks.onTransitionProgress(
transition.name,
transition.startTime,
endTime,
Array.from(pending.values()),
);
logged.add(transition);
}
});
}

const transitionComplete = pendingTransitions.transitionComplete;
if (transitionComplete !== null) {
transitionComplete.forEach(transition => {
Expand Down
24 changes: 24 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ export function addTransitionStartCallbackToPendingTransition(
if (currentPendingTransitionCallbacks === null) {
currentPendingTransitionCallbacks = {
transitionStart: [],
transitionProgress: null,
transitionComplete: null,
markerComplete: null,
};
Expand All @@ -358,6 +359,7 @@ export function addMarkerCompleteCallbackToPendingTransition(
if (currentPendingTransitionCallbacks === null) {
currentPendingTransitionCallbacks = {
transitionStart: null,
transitionProgress: null,
transitionComplete: null,
markerComplete: [],
};
Expand All @@ -371,13 +373,35 @@ export function addMarkerCompleteCallbackToPendingTransition(
}
}

export function addTransitionProgressCallbackToPendingTransition(
transition: TransitionProgressObject,
) {
if (enableTransitionTracing) {
if (currentPendingTransitionCallbacks === null) {
currentPendingTransitionCallbacks = {
transitionStart: null,
transitionProgress: [],
transitionComplete: null,
markerComplete: null,
};
}

if (currentPendingTransitionCallbacks.transitionProgress === null) {
currentPendingTransitionCallbacks.transitionProgress = [];
}

currentPendingTransitionCallbacks.transitionProgress.push(transition);
}
}

export function addTransitionCompleteCallbackToPendingTransition(
transition: TransitionObject,
) {
if (enableTransitionTracing) {
if (currentPendingTransitionCallbacks === null) {
currentPendingTransitionCallbacks = {
transitionStart: null,
transitionProgress: null,
transitionComplete: [],
markerComplete: null,
};
Expand Down
Loading

0 comments on commit 62a7daf

Please sign in to comment.