main
js 66 lines 1.16 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 type {Fiber} from './ReactInternalTypes';
11
12 export type StackCursor<T> = {current: T};
13
14 const valueStack: Array<any> = [];
15
16 let fiberStack: Array<Fiber | null>;
17
18 if (__DEV__) {
19 fiberStack = [];
20 }
21
22 let index = -1;
23
24 function createCursor<T>(defaultValue: T): StackCursor<T> {
25 return {
26 current: defaultValue,
27 };
28 }
29
30 function pop<T>(cursor: StackCursor<T>, fiber: Fiber): void {
31 if (index < 0) {
32 if (__DEV__) {
33 console.error('Unexpected pop.');
34 }
35 return;
36 }
37
38 if (__DEV__) {
39 if (fiber !== fiberStack[index]) {
40 console.error('Unexpected Fiber popped.');
41 }
42 }
43
44 cursor.current = valueStack[index];
45
46 valueStack[index] = null;
47
48 if (__DEV__) {
49 fiberStack[index] = null;
50 }
51
52 index--;
53 }
54
55 function push<T>(cursor: StackCursor<T>, value: T, fiber: Fiber): void {
56 index++;
57
58 valueStack[index] = cursor.current;
59
60 if (__DEV__) {
61 fiberStack[index] = fiber;
62 }
63
64 cursor.current = value;
65 }
66 export {createCursor, pop, push};