Skip to content

Commit 2c83514

Browse files
committed
Decouple setting the current fiber and phase
These are two distinct actions. This helps centralize setting the current fiber in the scheduler. I'm also removing a hack that's only useful in the corner case if you use unstable_renderSubtree() and top component has invalid child context type. This makes the code more straightforward.
1 parent 4f5a5f8 commit 2c83514

File tree

6 files changed

+23
-52
lines changed

6 files changed

+23
-52
lines changed

src/renderers/shared/fiber/ReactDebugCurrentFiber.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ function resetCurrentFiber() {
5555
ReactDebugCurrentFiber.phase = null;
5656
}
5757

58-
function setCurrentFiber(fiber: Fiber | null, phase: LifeCyclePhase | null) {
58+
function setCurrentFiber(fiber: Fiber) {
5959
ReactDebugCurrentFrame.getCurrentStack = getCurrentFiberStackAddendum;
6060
ReactDebugCurrentFiber.current = fiber;
61+
ReactDebugCurrentFiber.phase = null;
62+
}
63+
64+
function setCurrentPhase(phase: LifeCyclePhase | null) {
6165
ReactDebugCurrentFiber.phase = phase;
6266
}
6367

@@ -66,6 +70,7 @@ var ReactDebugCurrentFiber = {
6670
phase: (null: LifeCyclePhase | null),
6771
resetCurrentFiber,
6872
setCurrentFiber,
73+
setCurrentPhase,
6974
getCurrentFiberOwnerName,
7075
getCurrentFiberStackAddendum,
7176
};

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
207207

208208
if (__DEV__) {
209209
ReactCurrentOwner.current = workInProgress;
210-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, 'render');
210+
ReactDebugCurrentFiber.setCurrentPhase('render');
211211
nextChildren = fn(nextProps, context);
212-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
212+
ReactDebugCurrentFiber.setCurrentPhase(null);
213213
} else {
214214
nextChildren = fn(nextProps, context);
215215
}
@@ -281,9 +281,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
281281
ReactCurrentOwner.current = workInProgress;
282282
let nextChildren;
283283
if (__DEV__) {
284-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, 'render');
284+
ReactDebugCurrentFiber.setCurrentPhase('render');
285285
nextChildren = instance.render();
286-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
286+
ReactDebugCurrentFiber.setCurrentPhase(null);
287287
} else {
288288
nextChildren = instance.render();
289289
}
@@ -726,7 +726,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
726726
}
727727

728728
if (__DEV__) {
729-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
729+
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
730730
}
731731

732732
switch (workInProgress.tag) {

src/renderers/shared/fiber/ReactFiberCompleteWork.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
188188
renderPriority: PriorityLevel,
189189
): Fiber | null {
190190
if (__DEV__) {
191-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
191+
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
192192
}
193193

194194
// Get the latest props.

src/renderers/shared/fiber/ReactFiberContext.js

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ exports.getMaskedContext = function(
8989

9090
if (__DEV__) {
9191
const name = getComponentName(workInProgress) || 'Unknown';
92-
if (workInProgress !== ReactDebugCurrentFiber.current) {
93-
warning(
94-
false,
95-
'Expected the work in progress to match the currently processed fiber. ' +
96-
'This error is likely caused by a bug in React. Please file an issue.',
97-
);
98-
}
9992
checkPropTypes(
10093
contextTypes,
10194
context,
@@ -158,11 +151,7 @@ exports.pushTopLevelContextObject = function(
158151
push(didPerformWorkStackCursor, didChange, fiber);
159152
};
160153

161-
function processChildContext(
162-
fiber: Fiber,
163-
parentContext: Object,
164-
isReconciling: boolean,
165-
): Object {
154+
function processChildContext(fiber: Fiber, parentContext: Object): Object {
166155
const instance = fiber.stateNode;
167156
const childContextTypes = fiber.type.childContextTypes;
168157

@@ -189,19 +178,11 @@ function processChildContext(
189178

190179
let childContext;
191180
if (__DEV__) {
192-
// TODO: we only have to store the "previous" fiber and phase and restore them
193-
// because this method can be called outside of reconciliation. We can remove this
194-
// when we stop supporting unstable_renderSubtreeIntoContainer.
195-
const previousCurrentFiber = ReactDebugCurrentFiber.current;
196-
const previousCurrentPhase = ReactDebugCurrentFiber.phase;
197-
ReactDebugCurrentFiber.setCurrentFiber(fiber, 'getChildContext');
181+
ReactDebugCurrentFiber.setCurrentPhase('getChildContext');
198182
startPhaseTimer(fiber, 'getChildContext');
199183
childContext = instance.getChildContext();
200184
stopPhaseTimer();
201-
ReactDebugCurrentFiber.setCurrentFiber(
202-
previousCurrentFiber,
203-
previousCurrentPhase,
204-
);
185+
ReactDebugCurrentFiber.setCurrentPhase(null);
205186
} else {
206187
childContext = instance.getChildContext();
207188
}
@@ -215,29 +196,18 @@ function processChildContext(
215196
}
216197
if (__DEV__) {
217198
const name = getComponentName(fiber) || 'Unknown';
218-
// We can only provide accurate element stacks if we pass work-in-progress tree
219-
// during the begin or complete phase. However currently this function is also
220-
// called from unstable_renderSubtree legacy implementation. In this case it unsafe to
221-
// assume anything about the given fiber. We won't pass it down if we aren't sure.
222-
// TODO: remove this hack when we delete unstable_renderSubtree in Fiber.
223-
const workInProgress = isReconciling ? fiber : null;
224-
// TODO: we only have to store the "previous" fiber and phase and restore them
225-
// because this method can be called outside of reconciliation. We can remove this
226-
// when we stop supporting unstable_renderSubtreeIntoContainer.
227-
const previousCurrentFiber = ReactDebugCurrentFiber.current;
228-
const previousCurrentPhase = ReactDebugCurrentFiber.phase;
229-
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
230199
checkPropTypes(
231200
childContextTypes,
232201
childContext,
233202
'child context',
234203
name,
204+
// In practice, there is one case in which we won't get a stack. It's when
205+
// somebody calls unstable_renderSubtreeIntoContainer() and we process
206+
// context from the parent component instance. The stack will be missing
207+
// because it's outside of the reconciliation, and so the pointer has not
208+
// been set. This is rare and doesn't matter. We'll also remove that API.
235209
ReactDebugCurrentFiber.getCurrentFiberStackAddendum,
236210
);
237-
ReactDebugCurrentFiber.setCurrentFiber(
238-
previousCurrentFiber,
239-
previousCurrentPhase,
240-
);
241211
}
242212

243213
return {...parentContext, ...childContext};
@@ -285,11 +255,7 @@ exports.invalidateContextProvider = function(
285255
// Merge parent and own context.
286256
// Skip this if we're not updating due to sCU.
287257
// This avoids unnecessarily recomputing memoized values.
288-
const mergedContext = processChildContext(
289-
workInProgress,
290-
previousContext,
291-
true,
292-
);
258+
const mergedContext = processChildContext(workInProgress, previousContext);
293259
instance.__reactInternalMemoizedMergedChildContext = mergedContext;
294260

295261
// Replace the old (or empty) context with the new one.

src/renderers/shared/fiber/ReactFiberReconciler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export type Reconciler<C, I, TI> = {
181181
getContextForSubtree._injectFiber(function(fiber: Fiber) {
182182
const parentContext = findCurrentUnmaskedContext(fiber);
183183
return isContextProvider(fiber)
184-
? processChildContext(fiber, parentContext, false)
184+
? processChildContext(fiber, parentContext)
185185
: parentContext;
186186
});
187187

src/renderers/shared/fiber/ReactFiberScheduler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
317317
function commitAllHostEffects() {
318318
while (nextEffect !== null) {
319319
if (__DEV__) {
320-
ReactDebugCurrentFiber.setCurrentFiber(nextEffect, null);
320+
ReactDebugCurrentFiber.setCurrentFiber(nextEffect);
321321
recordEffect();
322322
}
323323

0 commit comments

Comments
 (0)