Skip to content

Commit

Permalink
use a stack for currentResources
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff committed Sep 30, 2022
1 parent f1925a8 commit b845eff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
22 changes: 9 additions & 13 deletions packages/react-dom-bindings/src/server/ReactDOMFloatServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ export function mergeBoundaryResources(
}

let currentResources: null | Resources = null;
let currentResourcesStack = [];

let previousDispatcher = null;
export function prepareToRenderResources(resources: Resources) {
currentResourcesStack.push(currentResources);
currentResources = resources;
}

previousDispatcher = ReactDOMSharedInternals.Dispatcher.current;
ReactDOMSharedInternals.Dispatcher.current = Dispatcher;
export function finishRenderingResources() {
currentResources = currentResourcesStack.pop();
}

export function setCurrentlyRenderingBoundaryResourcesTarget(
Expand All @@ -116,12 +119,10 @@ export function setCurrentlyRenderingBoundaryResourcesTarget(
resources.boundaryResources = boundaryResources;
}

export function finishRenderingResources() {
currentResources = null;

ReactDOMSharedInternals.Dispatcher.current = previousDispatcher;
previousDispatcher = null;
}
export const ReactDOMServerDispatcher = {
preload,
preinit,
};

type PreloadAs = ResourceType;
type PreloadOptions = {as: PreloadAs, crossOrigin?: string};
Expand Down Expand Up @@ -574,8 +575,3 @@ export function hoistResourcesToRoot(
});
boundaryResources.clear();
}

const Dispatcher = {
preload,
preinit,
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
prepareToRenderResources,
finishRenderingResources,
resourcesFromLink,
ReactDOMServerDispatcher,
} from './ReactDOMFloatServer';
export {
createResources,
Expand All @@ -73,6 +74,22 @@ export {
hoistResourcesToRoot,
} from './ReactDOMFloatServer';

import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
const ReactDOMCurrentDispatcher = ReactDOMSharedInternals.Dispatcher;

export function prepareToRender(resources: Resources): mixed {
prepareToRenderResources(resources);

let previousHostDispatcher = ReactDOMCurrentDispatcher.current;
ReactDOMCurrentDispatcher.current = ReactDOMServerDispatcher;
return previousHostDispatcher;
}

export function cleanupAfterRender(previousDispatcher: mixed) {
finishRenderingResources();
ReactDOMCurrentDispatcher.current = previousDispatcher;
}

// Used to distinguish these contexts from ones used in other renderers.
// E.g. this can be used to distinguish legacy renderers from this modern one.
export const isPrimaryRenderer = true;
Expand Down Expand Up @@ -2722,11 +2739,3 @@ function writeStyleResourceAttribute(
stringToChunk(escapeJSObjectForInstructionScripts(attributeValue)),
);
}

export function prepareToRender(resources: Resources) {
prepareToRenderResources(resources);
}

export function cleanupAfterRender() {
finishRenderingResources();
}
5 changes: 3 additions & 2 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,6 @@ function finishedTask(
}

function retryTask(request: Request, task: Task): void {
prepareToRender(request.resources);
if (enableFloat) {
const blockedBoundary = task.blockedBoundary;
setCurrentlyRenderingBoundaryResourcesTarget(
Expand Down Expand Up @@ -1892,7 +1891,6 @@ function retryTask(request: Request, task: Task): void {
if (__DEV__) {
currentTaskInDEV = prevTaskInDEV;
}
cleanupAfterRender();
}
}

Expand All @@ -1903,6 +1901,7 @@ export function performWork(request: Request): void {
const prevContext = getActiveContext();
const prevDispatcher = ReactCurrentDispatcher.current;
ReactCurrentDispatcher.current = Dispatcher;
const previousHostDispatcher = prepareToRender(request.resources);
let prevGetCurrentStackImpl;
if (__DEV__) {
prevGetCurrentStackImpl = ReactDebugCurrentFrame.getCurrentStack;
Expand All @@ -1927,6 +1926,8 @@ export function performWork(request: Request): void {
} finally {
setCurrentResponseState(prevResponseState);
ReactCurrentDispatcher.current = prevDispatcher;
cleanupAfterRender(previousHostDispatcher);

if (__DEV__) {
ReactDebugCurrentFrame.getCurrentStack = prevGetCurrentStackImpl;
}
Expand Down

0 comments on commit b845eff

Please sign in to comment.