main
js 46 lines 1.5 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import {REACT_CONSUMER_TYPE, REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
11
12 import type {ReactContext} from 'shared/ReactTypes';
13
14 export function createContext<T>(defaultValue: T): ReactContext<T> {
15 // TODO: Second argument used to be an optional `calculateChangedBits`
16 // function. Warn to reserve for future use?
17
18 const context: ReactContext<T> = {
19 $$typeof: REACT_CONTEXT_TYPE,
20 // As a workaround to support multiple concurrent renderers, we categorize
21 // some renderers as primary and others as secondary. We only expect
22 // there to be two concurrent renderers at most: React Native (primary) and
23 // Fabric (secondary); React DOM (primary) and React ART (secondary).
24 // Secondary renderers store their context values on separate fields.
25 _currentValue: defaultValue,
26 _currentValue2: defaultValue,
27 // Used to track how many concurrent renderers this context currently
28 // supports within in a single renderer. Such as parallel server rendering.
29 _threadCount: 0,
30 // These are circular
31 Provider: null as any,
32 Consumer: null as any,
33 };
34
35 context.Provider = context;
36 context.Consumer = {
37 $$typeof: REACT_CONSUMER_TYPE,
38 _context: context,
39 };
40 if (__DEV__) {
41 context._currentRenderer = null;
42 context._currentRenderer2 = null;
43 }
44
45 return context;
46 }