forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReactFiberNewContext.js
70 lines (62 loc) · 1.96 KB
/
ReactFiberNewContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Fiber} from './ReactFiber';
import type {ReactContext} from 'shared/ReactTypes';
import warning from 'fbjs/lib/warning';
let stack: Array<Fiber> = [];
let index = -1;
let rendererSigil;
if (__DEV__) {
// Use this to detect multiple renderers using the same context
rendererSigil = {};
}
export function pushProvider(providerFiber: Fiber): void {
index += 1;
stack[index] = providerFiber;
const context: ReactContext<any> = providerFiber.type.context;
context.currentValue = providerFiber.pendingProps.value;
context.changedBits = providerFiber.stateNode;
if (__DEV__) {
warning(
context._currentRenderer === null ||
context._currentRenderer === rendererSigil,
'Detected multiple renderers concurrently rendering the ' +
'same context provider. This is currently unsupported.',
);
context._currentRenderer = rendererSigil;
}
}
export function popProvider(providerFiber: Fiber): void {
if (__DEV__) {
warning(index > -1 && providerFiber === stack[index], 'Unexpected pop.');
}
stack[index] = null;
index -= 1;
const context: ReactContext<any> = providerFiber.type.context;
if (index < 0) {
context.currentValue = context.defaultValue;
context.changedBits = 0;
} else {
const previousProviderFiber = stack[index];
context.currentValue = previousProviderFiber.pendingProps.value;
context.changedBits = previousProviderFiber.stateNode;
}
}
export function resetProviderStack(): void {
for (let i = index; i > -1; i--) {
const providerFiber = stack[i];
const context: ReactContext<any> = providerFiber.type.context;
context.currentValue = context.defaultValue;
context.changedBits = 0;
stack[i] = null;
if (__DEV__) {
context._currentRenderer = null;
}
}
}