Skip to content

Commit

Permalink
Decouple setting the current fiber and phase
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gaearon committed Sep 28, 2017
1 parent 4f5a5f8 commit 2c83514
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 52 deletions.
7 changes: 6 additions & 1 deletion src/renderers/shared/fiber/ReactDebugCurrentFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ function resetCurrentFiber() {
ReactDebugCurrentFiber.phase = null;
}

function setCurrentFiber(fiber: Fiber | null, phase: LifeCyclePhase | null) {
function setCurrentFiber(fiber: Fiber) {
ReactDebugCurrentFrame.getCurrentStack = getCurrentFiberStackAddendum;
ReactDebugCurrentFiber.current = fiber;
ReactDebugCurrentFiber.phase = null;
}

function setCurrentPhase(phase: LifeCyclePhase | null) {
ReactDebugCurrentFiber.phase = phase;
}

Expand All @@ -66,6 +70,7 @@ var ReactDebugCurrentFiber = {
phase: (null: LifeCyclePhase | null),
resetCurrentFiber,
setCurrentFiber,
setCurrentPhase,
getCurrentFiberOwnerName,
getCurrentFiberStackAddendum,
};
Expand Down
10 changes: 5 additions & 5 deletions src/renderers/shared/fiber/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(

if (__DEV__) {
ReactCurrentOwner.current = workInProgress;
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, 'render');
ReactDebugCurrentFiber.setCurrentPhase('render');
nextChildren = fn(nextProps, context);
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
ReactDebugCurrentFiber.setCurrentPhase(null);
} else {
nextChildren = fn(nextProps, context);
}
Expand Down Expand Up @@ -281,9 +281,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
ReactCurrentOwner.current = workInProgress;
let nextChildren;
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, 'render');
ReactDebugCurrentFiber.setCurrentPhase('render');
nextChildren = instance.render();
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
ReactDebugCurrentFiber.setCurrentPhase(null);
} else {
nextChildren = instance.render();
}
Expand Down Expand Up @@ -726,7 +726,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
}

if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
}

switch (workInProgress.tag) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shared/fiber/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
renderPriority: PriorityLevel,
): Fiber | null {
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
ReactDebugCurrentFiber.setCurrentFiber(workInProgress);
}

// Get the latest props.
Expand Down
52 changes: 9 additions & 43 deletions src/renderers/shared/fiber/ReactFiberContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ exports.getMaskedContext = function(

if (__DEV__) {
const name = getComponentName(workInProgress) || 'Unknown';
if (workInProgress !== ReactDebugCurrentFiber.current) {
warning(
false,
'Expected the work in progress to match the currently processed fiber. ' +
'This error is likely caused by a bug in React. Please file an issue.',
);
}
checkPropTypes(
contextTypes,
context,
Expand Down Expand Up @@ -158,11 +151,7 @@ exports.pushTopLevelContextObject = function(
push(didPerformWorkStackCursor, didChange, fiber);
};

function processChildContext(
fiber: Fiber,
parentContext: Object,
isReconciling: boolean,
): Object {
function processChildContext(fiber: Fiber, parentContext: Object): Object {
const instance = fiber.stateNode;
const childContextTypes = fiber.type.childContextTypes;

Expand All @@ -189,19 +178,11 @@ function processChildContext(

let childContext;
if (__DEV__) {
// TODO: we only have to store the "previous" fiber and phase and restore them
// because this method can be called outside of reconciliation. We can remove this
// when we stop supporting unstable_renderSubtreeIntoContainer.
const previousCurrentFiber = ReactDebugCurrentFiber.current;
const previousCurrentPhase = ReactDebugCurrentFiber.phase;
ReactDebugCurrentFiber.setCurrentFiber(fiber, 'getChildContext');
ReactDebugCurrentFiber.setCurrentPhase('getChildContext');
startPhaseTimer(fiber, 'getChildContext');
childContext = instance.getChildContext();
stopPhaseTimer();
ReactDebugCurrentFiber.setCurrentFiber(
previousCurrentFiber,
previousCurrentPhase,
);
ReactDebugCurrentFiber.setCurrentPhase(null);
} else {
childContext = instance.getChildContext();
}
Expand All @@ -215,29 +196,18 @@ function processChildContext(
}
if (__DEV__) {
const name = getComponentName(fiber) || 'Unknown';
// We can only provide accurate element stacks if we pass work-in-progress tree
// during the begin or complete phase. However currently this function is also
// called from unstable_renderSubtree legacy implementation. In this case it unsafe to
// assume anything about the given fiber. We won't pass it down if we aren't sure.
// TODO: remove this hack when we delete unstable_renderSubtree in Fiber.
const workInProgress = isReconciling ? fiber : null;
// TODO: we only have to store the "previous" fiber and phase and restore them
// because this method can be called outside of reconciliation. We can remove this
// when we stop supporting unstable_renderSubtreeIntoContainer.
const previousCurrentFiber = ReactDebugCurrentFiber.current;
const previousCurrentPhase = ReactDebugCurrentFiber.phase;
ReactDebugCurrentFiber.setCurrentFiber(workInProgress, null);
checkPropTypes(
childContextTypes,
childContext,
'child context',
name,
// In practice, there is one case in which we won't get a stack. It's when
// somebody calls unstable_renderSubtreeIntoContainer() and we process
// context from the parent component instance. The stack will be missing
// because it's outside of the reconciliation, and so the pointer has not
// been set. This is rare and doesn't matter. We'll also remove that API.
ReactDebugCurrentFiber.getCurrentFiberStackAddendum,
);
ReactDebugCurrentFiber.setCurrentFiber(
previousCurrentFiber,
previousCurrentPhase,
);
}

return {...parentContext, ...childContext};
Expand Down Expand Up @@ -285,11 +255,7 @@ exports.invalidateContextProvider = function(
// Merge parent and own context.
// Skip this if we're not updating due to sCU.
// This avoids unnecessarily recomputing memoized values.
const mergedContext = processChildContext(
workInProgress,
previousContext,
true,
);
const mergedContext = processChildContext(workInProgress, previousContext);
instance.__reactInternalMemoizedMergedChildContext = mergedContext;

// Replace the old (or empty) context with the new one.
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shared/fiber/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export type Reconciler<C, I, TI> = {
getContextForSubtree._injectFiber(function(fiber: Fiber) {
const parentContext = findCurrentUnmaskedContext(fiber);
return isContextProvider(fiber)
? processChildContext(fiber, parentContext, false)
? processChildContext(fiber, parentContext)
: parentContext;
});

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shared/fiber/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
function commitAllHostEffects() {
while (nextEffect !== null) {
if (__DEV__) {
ReactDebugCurrentFiber.setCurrentFiber(nextEffect, null);
ReactDebugCurrentFiber.setCurrentFiber(nextEffect);
recordEffect();
}

Expand Down

0 comments on commit 2c83514

Please sign in to comment.