Skip to content

Commit

Permalink
old
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaruan committed Mar 1, 2022
1 parent e0f0a19 commit c7dd02c
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 79 deletions.
135 changes: 122 additions & 13 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ import {
markSkippedUpdateLanes,
getWorkInProgressRoot,
pushRenderLanes,
getWorkInProgressTransitions,
generateNewSuspenseOffscreenID,
} from './ReactFiberWorkLoop.old';
import {setWorkInProgressVersion} from './ReactMutableSource.old';
import {pushCacheProvider, CacheContext} from './ReactFiberCacheComponent.old';
Expand All @@ -246,7 +246,9 @@ import {
getSuspendedCache,
pushTransition,
getOffscreenDeferredCache,
getSuspendedTransitions,
} from './ReactFiberTransition.old';
import {pushRootPendingSuspenseBoundaries} from './ReactFiberTracingMarkerComponent.old';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

Expand Down Expand Up @@ -645,13 +647,14 @@ function updateOffscreenComponent(
const nextState: OffscreenState = {
baseLanes: NoLanes,
cachePool: null,
transitions: null,
};
workInProgress.memoizedState = nextState;
if (enableCache) {
// push the cache pool even though we're going to bail out
// because otherwise there'd be a context mismatch
if (current !== null) {
pushTransition(workInProgress, null);
pushTransition(workInProgress, null, null);
}
}
pushRenderLanes(workInProgress, renderLanes);
Expand All @@ -678,14 +681,15 @@ function updateOffscreenComponent(
const nextState: OffscreenState = {
baseLanes: nextBaseLanes,
cachePool: spawnedCachePool,
transitions: null,
};
workInProgress.memoizedState = nextState;
workInProgress.updateQueue = null;
if (enableCache) {
// push the cache pool even though we're going to bail out
// because otherwise there'd be a context mismatch
if (current !== null) {
pushTransition(workInProgress, null);
pushTransition(workInProgress, null, null);
}
}

Expand Down Expand Up @@ -713,6 +717,7 @@ function updateOffscreenComponent(
const nextState: OffscreenState = {
baseLanes: NoLanes,
cachePool: null,
transitions: null,
};
workInProgress.memoizedState = nextState;
// Push the lanes that were skipped when we bailed out.
Expand All @@ -723,7 +728,7 @@ function updateOffscreenComponent(
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
const prevCachePool = prevState !== null ? prevState.cachePool : null;
pushTransition(workInProgress, prevCachePool);
pushTransition(workInProgress, prevCachePool, null);
}

pushRenderLanes(workInProgress, subtreeRenderLanes);
Expand All @@ -736,14 +741,14 @@ function updateOffscreenComponent(

subtreeRenderLanes = mergeLanes(prevState.baseLanes, renderLanes);

if (enableCache) {
if (enableCache || enableTransitionTracing) {
// If the render that spawned this one accessed the cache pool, resume
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
const prevCachePool = prevState.cachePool;
pushTransition(workInProgress, prevCachePool);
const transitions = prevState.transitions;
pushTransition(workInProgress, prevCachePool, transitions);
}

// Since we're not hidden anymore, reset the state
workInProgress.memoizedState = null;
} else {
Expand All @@ -757,7 +762,7 @@ function updateOffscreenComponent(
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
if (current !== null) {
pushTransition(workInProgress, null);
pushTransition(workInProgress, null, null);
}
}
}
Expand Down Expand Up @@ -1326,9 +1331,12 @@ function updateHostRoot(current, workInProgress, renderLanes) {

const root: FiberRoot = workInProgress.stateNode;

if (enableCache || enableTransitionTracing) {
pushRootTransition(root, renderLanes);
}

if (enableCache) {
const nextCache: Cache = nextState.cache;
pushRootTransition(root);
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
Expand All @@ -1337,7 +1345,28 @@ function updateHostRoot(current, workInProgress, renderLanes) {
}

if (enableTransitionTracing) {
workInProgress.memoizedState.transitions = getWorkInProgressTransitions();
const transitions = getSuspendedTransitions();
const nextTransitions = [];
if (transitions !== null) {
transitions.forEach(transition => {
nextTransitions.push(transition);
});
}
const rootTransitions = prevState.transitions;
if (rootTransitions != null) {
rootTransitions.forEach(transition => {
nextTransitions.push(transition);
});
}

let pendingSuspenseBoundaries = prevState.pendingSuspenseBoundaries;
if (pendingSuspenseBoundaries === null) {
pendingSuspenseBoundaries = new Map();
}
// probably have to actually copy this
workInProgress.memoizedState.transitions = nextTransitions;
workInProgress.memoizedState.pendingSuspenseBoundaries = pendingSuspenseBoundaries;
pushRootPendingSuspenseBoundaries(workInProgress);
}

// Caution: React DevTools currently depends on this property
Expand Down Expand Up @@ -1910,6 +1939,7 @@ function mountSuspenseOffscreenState(renderLanes: Lanes): OffscreenState {
return {
baseLanes: renderLanes,
cachePool: getSuspendedCache(),
transitions: getSuspendedTransitions(),
};
}

Expand Down Expand Up @@ -1941,9 +1971,29 @@ function updateSuspenseOffscreenState(
cachePool = getSuspendedCache();
}
}

const transitions = [];
if (enableTransitionTracing) {
const prevTransitions = prevOffscreenState.transitions;
const newTransitions = getSuspendedTransitions();
if (prevTransitions !== null) {
prevTransitions.forEach(transition => {
transitions.push(transition);
});
}
if (newTransitions !== null) {
newTransitions.forEach(transition => {
if (!transitions.includes(transition)) {
transitions.push(transition);
}
});
}
}

return {
baseLanes: mergeLanes(prevOffscreenState.baseLanes, renderLanes),
cachePool,
transitions: transitions.length > 0 ? transitions : null,
};
}

Expand Down Expand Up @@ -2274,9 +2324,17 @@ function mountSuspensePrimaryChildren(
renderLanes,
) {
const mode = workInProgress.mode;
const props = workInProgress.memoizedProps;
let name = null;
if (props !== null) {
name = props.name;
}

const primaryChildProps: OffscreenProps = {
mode: 'visible',
children: primaryChildren,
name,
id: generateNewSuspenseOffscreenID(),
};
const primaryChildFragment = mountWorkInProgressOffscreenFiber(
primaryChildProps,
Expand All @@ -2296,10 +2354,16 @@ function mountSuspenseFallbackChildren(
) {
const mode = workInProgress.mode;
const progressedPrimaryFragment: Fiber | null = workInProgress.child;

const props = workInProgress.memoizedProps;
let name = null;
if (props !== null) {
name = props.name;
}
const primaryChildProps: OffscreenProps = {
mode: 'hidden',
children: primaryChildren,
name,
id: generateNewSuspenseOffscreenID(),
};

let primaryChildFragment;
Expand Down Expand Up @@ -2377,15 +2441,22 @@ function updateSuspensePrimaryChildren(
primaryChildren,
renderLanes,
) {
const name = workInProgress.pendingProps.name;
const currentPrimaryChildFragment: Fiber = (current.child: any);
const currentFallbackChildFragment: Fiber | null =
currentPrimaryChildFragment.sibling;
const props =
currentPrimaryChildFragment.memoizedProps !== null
? currentPrimaryChildFragment.memoizedProps
: currentPrimaryChildFragment.pendingProps;

const primaryChildFragment = updateWorkInProgressOffscreenFiber(
currentPrimaryChildFragment,
{
mode: 'visible',
children: primaryChildren,
name,
id: props.id,
},
);
if ((workInProgress.mode & ConcurrentMode) === NoMode) {
Expand Down Expand Up @@ -2415,14 +2486,21 @@ function updateSuspenseFallbackChildren(
fallbackChildren,
renderLanes,
) {
const name = workInProgress.pendingProps.name;
const mode = workInProgress.mode;
const currentPrimaryChildFragment: Fiber = (current.child: any);
const currentFallbackChildFragment: Fiber | null =
currentPrimaryChildFragment.sibling;
const props =
currentPrimaryChildFragment.memoizedProps !== null
? currentPrimaryChildFragment.memoizedProps
: currentPrimaryChildFragment.pendingProps;

const primaryChildProps: OffscreenProps = {
mode: 'hidden',
children: primaryChildren,
name,
id: props.id,
};

let primaryChildFragment;
Expand Down Expand Up @@ -2581,10 +2659,13 @@ function mountSuspenseFallbackAfterRetryWithoutHydrating(
fallbackChildren,
renderLanes,
) {
const name = workInProgress.pendingProps.name;
const fiberMode = workInProgress.mode;
const primaryChildProps: OffscreenProps = {
mode: 'visible',
children: primaryChildren,
name,
id: generateNewSuspenseOffscreenID(),
};
const primaryChildFragment = mountWorkInProgressOffscreenFiber(
primaryChildProps,
Expand Down Expand Up @@ -3500,13 +3581,41 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
case HostRoot:
pushHostRootContext(workInProgress);
const root: FiberRoot = workInProgress.stateNode;
if (enableCache || enableTransitionTracing) {
pushRootTransition(root, renderLanes);
}

if (enableCache) {
const cache: Cache = current.memoizedState.cache;
pushCacheProvider(workInProgress, cache);
pushRootTransition(root);
}
if (enableTransitionTracing) {
workInProgress.memoizedState.transitions = getWorkInProgressTransitions();
const prevState = current.memoizedState;

const nextTransitions = [];
const transitions = getSuspendedTransitions();
if (transitions !== null) {
transitions.forEach(transition => {
nextTransitions.push(transition);
});
}
const rootTransitions = prevState.transitions;
if (rootTransitions != null) {
rootTransitions.forEach(transition => {
if (!nextTransitions.includes(transition))
nextTransitions.push(transition);
});
}

let pendingSuspenseBoundaries = prevState.pendingSuspenseBoundaries;
if (pendingSuspenseBoundaries == null) {
pendingSuspenseBoundaries = new Map();
}
// probably have to actually copy this
workInProgress.memoizedState.transitions = nextTransitions;
workInProgress.memoizedState.pendingSuspenseBoundaries = pendingSuspenseBoundaries;

pushRootPendingSuspenseBoundaries(workInProgress);
}
resetHydrationState();
break;
Expand Down
10 changes: 4 additions & 6 deletions packages/react-reconciler/src/ReactFiberCacheComponent.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';

import {pushProvider, popProvider} from './ReactFiberNewContext.old';
import * as Scheduler from 'scheduler';

export type CacheComponentState = {|
+parent: Cache,
+cache: Cache,
|};
export type Cache = {|
controller: AbortController,
data: Map<() => mixed, mixed>,
refCount: number,
|};

export type CacheComponentState = {|
+parent: Cache,
+cache: Cache,
|};

export type SpawnedCachePool = {|
+parent: Cache,
+pool: Cache,
Expand Down
Loading

0 comments on commit c7dd02c

Please sign in to comment.