Skip to content

Commit e7ed64c

Browse files
committed
Move context handling back into scheduler
As per discussion, this is better because then this code only runs for the class type of work. We will still need to fix bailouts for deep setState calls.
1 parent f9e1bc9 commit e7ed64c

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ var {
2727
var ReactTypeOfWork = require('ReactTypeOfWork');
2828
var {
2929
getMaskedContext,
30+
isContextProvider,
31+
pushContextProvider,
32+
resetContext,
3033
} = require('ReactFiberContext');
3134
var {
3235
IndeterminateComponent,
@@ -195,6 +198,10 @@ module.exports = function<T, P, I, TI, C>(
195198
ReactCurrentOwner.current = workInProgress;
196199
const nextChildren = instance.render();
197200
reconcileChildren(current, workInProgress, nextChildren);
201+
// Put context on the stach because we will work on children
202+
if (isContextProvider(workInProgress)) {
203+
pushContextProvider(workInProgress);
204+
}
198205
return workInProgress.child;
199206
}
200207

@@ -352,6 +359,10 @@ module.exports = function<T, P, I, TI, C>(
352359

353360
cloneChildFibers(current, workInProgress);
354361
markChildAsProgressed(current, workInProgress, priorityLevel);
362+
// Put context on the stach because we will work on children
363+
if (isContextProvider(workInProgress)) {
364+
pushContextProvider(workInProgress);
365+
}
355366
return workInProgress.child;
356367
}
357368

@@ -362,6 +373,11 @@ module.exports = function<T, P, I, TI, C>(
362373
}
363374

364375
function beginWork(current : ?Fiber, workInProgress : Fiber, priorityLevel : PriorityLevel) : ?Fiber {
376+
if (!workInProgress.return) {
377+
// Don't start new work with context on the stack.
378+
resetContext();
379+
}
380+
365381
if (workInProgress.pendingWorkPriority === NoWork ||
366382
workInProgress.pendingWorkPriority > priorityLevel) {
367383
return bailoutOnLowPriority(current, workInProgress);

src/renderers/shared/fiber/ReactFiberCompleteWork.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import type { HostConfig } from 'ReactFiberReconciler';
1818
import type { ReifiedYield } from 'ReactReifiedYield';
1919

2020
var { reconcileChildFibers } = require('ReactChildFiber');
21+
var {
22+
isContextProvider,
23+
popContextProvider,
24+
} = require('ReactFiberContext');
2125
var ReactTypeOfWork = require('ReactTypeOfWork');
2226
var ReactTypeOfSideEffect = require('ReactTypeOfSideEffect');
2327
var {
@@ -125,6 +129,10 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
125129
return null;
126130
case ClassComponent:
127131
transferOutput(workInProgress.child, workInProgress);
132+
// We are leaving this subtree, so pop context if any.
133+
if (isContextProvider(workInProgress)) {
134+
popContextProvider();
135+
}
128136
// Don't use the state queue to compute the memoized state. We already
129137
// merged it and assigned it to the instance. Transfer it from there.
130138
// Also need to transfer the props, because pendingProps will be null

src/renderers/shared/fiber/ReactFiberScheduler.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ var ReactCurrentOwner = require('ReactCurrentOwner');
2424

2525
var { cloneFiber } = require('ReactFiber');
2626

27-
var {
28-
isContextProvider,
29-
pushContextProvider,
30-
popContextProvider,
31-
resetContext,
32-
} = require('ReactFiberContext');
33-
3427
var {
3528
NoWork,
3629
SynchronousPriority,
@@ -274,11 +267,6 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
274267
const current = workInProgress.alternate;
275268
const next = completeWork(current, workInProgress);
276269

277-
// We are leaving this subtree, so pop context if any.
278-
if (isContextProvider(workInProgress)) {
279-
popContextProvider();
280-
}
281-
282270
resetWorkPriority(workInProgress);
283271

284272
// The work is now done. We don't need this anymore. This flags
@@ -357,11 +345,6 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
357345
}
358346

359347
function performUnitOfWork(workInProgress : Fiber) : ?Fiber {
360-
if (!workInProgress.return) {
361-
// Don't start new work with context on the stack.
362-
resetContext();
363-
}
364-
365348
// The current, flushed, state of this fiber is the alternate.
366349
// Ideally nothing should rely on this, but relying on it here
367350
// means that we don't need an additional field on the work in
@@ -377,12 +360,7 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
377360
ReactFiberInstrumentation.debugTool.onDidBeginWork(workInProgress);
378361
}
379362

380-
if (next) {
381-
// There is work deeper in the tree, so push the context if it exists.
382-
if (isContextProvider(workInProgress)) {
383-
pushContextProvider(workInProgress);
384-
}
385-
} else {
363+
if (!next) {
386364
if (__DEV__ && ReactFiberInstrumentation.debugTool) {
387365
ReactFiberInstrumentation.debugTool.onWillCompleteWork(workInProgress);
388366
}

0 commit comments

Comments
 (0)