Skip to content

Commit c2ec69a

Browse files
acdliterhagigi
authored andcommitted
Unify context stack implementations (facebook#12359)
* Use module pattern so context stack is isolated per renderer * Unify context implementations Implements the new context API on top of the existing ReactStack that we already use for host context and legacy context. Now there is a single array that we push and pop from. This makes the interrupt path slightly slower, since when we reset the unit of work pointer, we have to iterate over the stack (like before) *and* switch on the type of work (not like before). On the other hand, this unifies all of the unwinding behavior in the UnwindWork module. * Add DEV only warning if stack is not reset properly
1 parent 7c55a8b commit c2ec69a

11 files changed

+547
-412
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {HostConfig} from 'react-reconciler';
1111
import type {ReactProviderType, ReactContext} from 'shared/ReactTypes';
1212
import type {Fiber} from 'react-reconciler/src/ReactFiber';
1313
import type {HostContext} from './ReactFiberHostContext';
14+
import type {LegacyContext} from './ReactFiberContext';
15+
import type {NewContext} from './ReactFiberNewContext';
1416
import type {HydrationContext} from './ReactFiberHydrationContext';
1517
import type {FiberRoot} from './ReactFiberRoot';
1618
import type {ExpirationTime} from './ReactFiberExpirationTime';
@@ -57,15 +59,6 @@ import {
5759
cloneChildFibers,
5860
} from './ReactChildFiber';
5961
import {processUpdateQueue} from './ReactFiberUpdateQueue';
60-
import {
61-
getMaskedContext,
62-
getUnmaskedContext,
63-
hasContextChanged as hasLegacyContextChanged,
64-
pushContextProvider as pushLegacyContextProvider,
65-
pushTopLevelContextObject,
66-
invalidateContextProvider,
67-
} from './ReactFiberContext';
68-
import {pushProvider} from './ReactFiberNewContext';
6962
import {NoWork, Never} from './ReactFiberExpirationTime';
7063
import {AsyncMode, StrictMode} from './ReactTypeOfMode';
7164
import MAX_SIGNED_31_BIT_INT from './maxSigned31BitInt';
@@ -83,6 +76,8 @@ if (__DEV__) {
8376
export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
8477
config: HostConfig<T, P, I, TI, HI, PI, C, CC, CX, PL>,
8578
hostContext: HostContext<C, CX>,
79+
legacyContext: LegacyContext,
80+
newContext: NewContext,
8681
hydrationContext: HydrationContext<C, CX>,
8782
scheduleWork: (fiber: Fiber, expirationTime: ExpirationTime) => void,
8883
computeExpirationForFiber: (fiber: Fiber) => ExpirationTime,
@@ -91,6 +86,17 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
9186

9287
const {pushHostContext, pushHostContainer} = hostContext;
9388

89+
const {pushProvider} = newContext;
90+
91+
const {
92+
getMaskedContext,
93+
getUnmaskedContext,
94+
hasContextChanged: hasLegacyContextChanged,
95+
pushContextProvider: pushLegacyContextProvider,
96+
pushTopLevelContextObject,
97+
invalidateContextProvider,
98+
} = legacyContext;
99+
94100
const {
95101
enterHydrationState,
96102
resetHydrationState,
@@ -105,6 +111,7 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
105111
resumeMountClassInstance,
106112
updateClassInstance,
107113
} = ReactFiberClassComponent(
114+
legacyContext,
108115
scheduleWork,
109116
computeExpirationForFiber,
110117
memoizeProps,

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import type {Fiber} from './ReactFiber';
1111
import type {ExpirationTime} from './ReactFiberExpirationTime';
12+
import type {LegacyContext} from './ReactFiberContext';
1213
import type {CapturedValue} from './ReactCapturedValue';
1314

1415
import {Update} from 'shared/ReactTypeOfSideEffect';
@@ -29,17 +30,10 @@ import warning from 'fbjs/lib/warning';
2930

3031
import {startPhaseTimer, stopPhaseTimer} from './ReactDebugFiberPerf';
3132
import {StrictMode} from './ReactTypeOfMode';
32-
import {
33-
cacheContext,
34-
getMaskedContext,
35-
getUnmaskedContext,
36-
isContextConsumer,
37-
} from './ReactFiberContext';
3833
import {
3934
insertUpdateIntoFiber,
4035
processUpdateQueue,
4136
} from './ReactFiberUpdateQueue';
42-
import {hasContextChanged} from './ReactFiberContext';
4337

4438
const fakeInternalInstance = {};
4539
const isArray = Array.isArray;
@@ -110,11 +104,20 @@ function callGetDerivedStateFromCatch(ctor: any, capturedValues: Array<mixed>) {
110104
}
111105

112106
export default function(
107+
legacyContext: LegacyContext,
113108
scheduleWork: (fiber: Fiber, expirationTime: ExpirationTime) => void,
114109
computeExpirationForFiber: (fiber: Fiber) => ExpirationTime,
115110
memoizeProps: (workInProgress: Fiber, props: any) => void,
116111
memoizeState: (workInProgress: Fiber, state: any) => void,
117112
) {
113+
const {
114+
cacheContext,
115+
getMaskedContext,
116+
getUnmaskedContext,
117+
isContextConsumer,
118+
hasContextChanged,
119+
} = legacyContext;
120+
118121
// Class component state updater
119122
const updater = {
120123
isMounted,

packages/react-reconciler/src/ReactFiberCompleteWork.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {HostConfig} from 'react-reconciler';
1111
import type {Fiber} from './ReactFiber';
1212
import type {ExpirationTime} from './ReactFiberExpirationTime';
1313
import type {HostContext} from './ReactFiberHostContext';
14+
import type {LegacyContext} from './ReactFiberContext';
15+
import type {NewContext} from './ReactFiberNewContext';
1416
import type {HydrationContext} from './ReactFiberHydrationContext';
1517
import type {FiberRoot} from './ReactFiberRoot';
1618

@@ -46,15 +48,12 @@ import {
4648
import invariant from 'fbjs/lib/invariant';
4749

4850
import {reconcileChildFibers} from './ReactChildFiber';
49-
import {
50-
popContextProvider as popLegacyContextProvider,
51-
popTopLevelContextObject as popTopLevelLegacyContextObject,
52-
} from './ReactFiberContext';
53-
import {popProvider} from './ReactFiberNewContext';
5451

5552
export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
5653
config: HostConfig<T, P, I, TI, HI, PI, C, CC, CX, PL>,
5754
hostContext: HostContext<C, CX>,
55+
legacyContext: LegacyContext,
56+
newContext: NewContext,
5857
hydrationContext: HydrationContext<C, CX>,
5958
) {
6059
const {
@@ -74,6 +73,13 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
7473
popHostContainer,
7574
} = hostContext;
7675

76+
const {
77+
popContextProvider: popLegacyContextProvider,
78+
popTopLevelContextObject: popTopLevelLegacyContextObject,
79+
} = legacyContext;
80+
81+
const {popProvider} = newContext;
82+
7783
const {
7884
prepareToHydrateHostInstance,
7985
prepareToHydrateHostTextInstance,

0 commit comments

Comments
 (0)