Skip to content

Commit

Permalink
transition tracing delete tracing marker
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaruan committed Aug 9, 2022
1 parent 17e2a15 commit a4647fa
Show file tree
Hide file tree
Showing 7 changed files with 673 additions and 9 deletions.
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,9 @@ export function createFiberFromTracingMarker(
const tracingMarkerInstance: TracingMarkerInstance = {
transitions: null,
pendingBoundaries: null,
deletions: null,
parents: null,
name: pendingProps.name,
};
fiber.stateNode = tracingMarkerInstance;
return fiber;
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,7 @@ function updateTracingMarkerComponent(
transitions: new Set(currentTransitions),
pendingBoundaries: new Map(),
name: workInProgress.pendingProps.name,
deletions: null,
};
workInProgress.stateNode = markerInstance;
}
Expand Down
151 changes: 147 additions & 4 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import type {
import type {HookFlags} from './ReactHookEffectTags';
import type {Cache} from './ReactFiberCacheComponent.new';
import type {RootState} from './ReactFiberRoot.new';
import type {Transition} from './ReactFiberTracingMarkerComponent.new';
import type {
Transition,
TracingMarkerInstance,
} from './ReactFiberTracingMarkerComponent.new';

import {
enableCreateEventHandleAPI,
Expand Down Expand Up @@ -146,6 +149,7 @@ import {
addTransitionProgressCallbackToPendingTransition,
addTransitionCompleteCallbackToPendingTransition,
addMarkerProgressCallbackToPendingTransition,
addMarkerIncompleteCallbackToPendingTransition,
addMarkerCompleteCallbackToPendingTransition,
setIsRunningInsertionEffect,
} from './ReactFiberWorkLoop.new';
Expand Down Expand Up @@ -1132,6 +1136,98 @@ function commitLayoutEffectOnFiber(
}
}

function abortParentMarkerTransitions(
fiber,
deletedFiber,
deletion,
isDeleted,
) {
const instance = deletedFiber.stateNode;
switch (fiber.tag) {
case TracingMarkerComponent:
const transitions = Array.from(deletedFiber.stateNode.transitions);

const markerInstance = fiber.stateNode;
const markerTransitions = markerInstance.transitions;
const abortMarker = transitions.some(transition =>
markerTransitions.has(transition),
);

if (abortMarker) {
if (markerInstance.deletions === null) {
markerInstance.deletions = new Set();
}

markerInstance.deletions.add(deletion);
addMarkerIncompleteCallbackToPendingTransition(
fiber.memoizedProps.name,
transitions,
markerInstance.deletions,
);

if (
!isDeleted &&
markerInstance.pendingBoundaries !== null &&
markerInstance.pendingBoundaries.has(instance)
) {
markerInstance.pendingBoundaries.delete(instance);

addMarkerProgressCallbackToPendingTransition(
markerInstance.name,
transitions,
markerInstance.pendingBoundaries,
);
}
}
break;
case HostRoot:
const root = fiber.stateNode;
const incompleteTransitions = root.incompleteTransitions;

instance.transitions.forEach(transition => {
if (incompleteTransitions.has(transition)) {
const transitionInstance = incompleteTransitions.get(transition);
if (transitionInstance.deletions === null) {
transitionInstance.deletions = [];
}
transitionInstance.deletions.push(deletion);

if (
transitionInstance.pendingBoundaries !== null &&
transitionInstance.pendingBoundaries.has(instance)
) {
transitionInstance.pendingBoundaries.delete(instance);
}
}
});
break;
default:
break;
}
}

function recursivelyAbortParentMarkerTransitions(
deletedFiber: Fiber,
nearestMountedAncestor,
deletion,
) {
let fiber = deletedFiber;
while (fiber !== null) {
abortParentMarkerTransitions(fiber, deletedFiber, deletion, true);
if (nearestMountedAncestor.deletions.includes(fiber)) {
break;
} else {
fiber = fiber.return;
}
}

fiber = nearestMountedAncestor;
while (fiber !== null) {
abortParentMarkerTransitions(fiber, deletedFiber, deletion, false);
fiber = fiber.return;
}
}

function commitTransitionProgress(offscreenFiber: Fiber) {
if (enableTransitionTracing) {
// This function adds suspense boundaries to the root
Expand Down Expand Up @@ -1987,6 +2083,20 @@ function commitDeletionEffectsOnFiber(
const prevOffscreenSubtreeWasHidden = offscreenSubtreeWasHidden;
offscreenSubtreeWasHidden =
prevOffscreenSubtreeWasHidden || deletedFiber.memoizedState !== null;

if (enableTransitionTracing) {
// We need to mark this fiber's parents as deleted
const instance: OffscreenInstance = deletedFiber.stateNode;
const markers = instance.pendingMarkers;
if (markers !== null) {
markers.forEach(marker => {
if (marker.pendingBoundaries.has(instance)) {
marker.pendingBoundaries.delete(instance);
}
});
}
}

recursivelyTraverseDeletionEffects(
finishedRoot,
nearestMountedAncestor,
Expand All @@ -2002,6 +2112,30 @@ function commitDeletionEffectsOnFiber(
}
break;
}
case TracingMarkerComponent: {
if (enableTransitionTracing) {
// We need to mark this fiber's parents as deleted
const instance: TracingMarkerInstance = deletedFiber.stateNode;
const transitions = instance.transitions;
if (transitions !== null) {
const deletion = {
type: 'marker',
name: deletedFiber.memoizedProps.name,
};
recursivelyAbortParentMarkerTransitions(
deletedFiber,
nearestMountedAncestor,
deletion,
);
}
}
recursivelyTraverseDeletionEffects(
finishedRoot,
nearestMountedAncestor,
deletedFiber,
);
return;
}
default: {
recursivelyTraverseDeletionEffects(
finishedRoot,
Expand Down Expand Up @@ -2987,6 +3121,11 @@ function commitOffscreenPassiveMountEffects(
}

commitTransitionProgress(finishedWork);

if (!isHidden) {
instance.transitions = null;
instance.pendingMarkers = null;
}
}
}

Expand Down Expand Up @@ -3023,14 +3162,16 @@ function commitTracingMarkerPassiveMountEffect(finishedWork: Fiber) {
(instance.pendingBoundaries === null ||
instance.pendingBoundaries.size === 0)
) {
instance.transitions.forEach(transition => {
if (instance.deletions === null) {
addMarkerCompleteCallbackToPendingTransition(
finishedWork.memoizedProps.name,
instance.transitions,
);
});
}
instance.transitions = null;
instance.pendingBoundaries = null;
instance.deletions = null;
instance.name = null;
}
}

Expand Down Expand Up @@ -3146,7 +3287,9 @@ function commitPassiveMountOnFiber(
incompleteTransitions.forEach((markerInstance, transition) => {
const pendingBoundaries = markerInstance.pendingBoundaries;
if (pendingBoundaries === null || pendingBoundaries.size === 0) {
addTransitionCompleteCallbackToPendingTransition(transition);
if (markerInstance.deletions === null) {
addTransitionCompleteCallbackToPendingTransition(transition);
}
incompleteTransitions.delete(transition);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ export type PendingTransitionCallbacks = {
transitionStart: Array<Transition> | null,
transitionProgress: Map<Transition, PendingBoundaries> | null,
transitionComplete: Array<Transition> | null,
markerProgress: Map<string, TracingMarkerInstance> | null,
markerProgress: Map<
string,
{pendingBoundaries: PendingBoundaries, transitions: Set<Transition>},
> | null,
markerIncomplete: Map<
string,
{deletions: Array<TransitionDeletion>, transitions: Set<Transition>},
> | null,
markerComplete: Map<string, Set<Transition>> | null,
};

Expand All @@ -39,7 +46,14 @@ export type BatchConfigTransition = {
export type TracingMarkerInstance = {|
pendingBoundaries: PendingBoundaries | null,
transitions: Set<Transition> | null,
name?: string,
deletions: Array<TransitionDeletion> | null,
name: string | null,
|};

export type TransitionDeletion = {|
type: 'error' | 'unknown' | 'marker' | 'suspense',
name?: string | null,
transitions: Set<Transition>,
|};

export type PendingBoundaries = Map<OffscreenInstance, SuspenseInfo>;
Expand All @@ -64,6 +78,7 @@ export function processTransitionCallbacks(
if (onMarkerProgress != null && markerProgress !== null) {
markerProgress.forEach((markerInstance, markerName) => {
if (markerInstance.transitions !== null) {
// TODO: Clone the suspense object so users can't modify it
const pending =
markerInstance.pendingBoundaries !== null
? Array.from(markerInstance.pendingBoundaries.values())
Expand Down Expand Up @@ -96,6 +111,31 @@ export function processTransitionCallbacks(
});
}

const markerIncomplete = pendingTransitions.markerIncomplete;
const onMarkerIncomplete = callbacks.onMarkerIncomplete;
if (onMarkerIncomplete != null && markerIncomplete !== null) {
markerIncomplete.forEach(({transitions, deletions}, markerName) => {
transitions.forEach(transition => {
const filteredDeletions = [];
deletions.forEach(deletion => {
const filteredDeletion = getFilteredDeletion(deletion, endTime);
if (filteredDeletion !== null) {
filteredDeletions.push(filteredDeletion);
}
});

if (filteredDeletions.length > 0) {
onMarkerIncomplete(
transition.name,
markerName,
transition.startTime,
filteredDeletions,
);
}
});
});
}

const transitionProgress = pendingTransitions.transitionProgress;
const onTransitionProgress = callbacks.onTransitionProgress;
if (onTransitionProgress != null && transitionProgress !== null) {
Expand All @@ -120,6 +160,21 @@ export function processTransitionCallbacks(
}
}

function getFilteredDeletion(deletion: TransitionDeletion, endTime: number) {
switch (deletion.type) {
case 'marker': {
return {
type: deletion.type,
name: deletion.name,
endTime,
};
}
default: {
return null;
}
}
}

// For every tracing marker, store a pointer to it. We will later access it
// to get the set of suspense boundaries that need to resolve before the
// tracing marker can be logged as complete
Expand Down Expand Up @@ -148,6 +203,9 @@ export function pushRootMarkerInstance(workInProgress: Fiber): void {
const markerInstance: TracingMarkerInstance = {
transitions: new Set([transition]),
pendingBoundaries: null,
deletions: null,
parents: null,
name: null,
};
root.incompleteTransitions.set(transition, markerInstance);
}
Expand Down
Loading

0 comments on commit a4647fa

Please sign in to comment.