2016-12-21 10:04:38 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-12-21 10:04:38 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-12-21 10:04:38 +08:00
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
|
Split cross-package types from implementation
Some of our internal reconciler types have leaked into other packages.
Usually, these types are treated as opaque; we don't read and write
to its fields. This is good.
However, the type is often passed back to a reconciler method. For
example, React DOM creates a FiberRoot with `createContainer`, then
passes that root to `updateContainer`. It doesn't do anything with the
root except pass it through, but because `updateContainer` expects a
full FiberRoot, React DOM is still coupled to all its fields.
I don't know if there's an idiomatic way to handle this in Flow. Opaque
types are simlar, but those only work within a single file. AFAIK,
there's no way to use a package as the boundary for opaqueness.
The immediate problem this presents is that the reconciler refactor will
involve changes to our internal data structures. I don't want to have to
fork every single package that happens to pass through a Fiber or
FiberRoot, or access any one of its fields. So my current plan is to
share the same Flow type across both forks. The shared type will be a
superset of each implementation's type, e.g. Fiber will have both an
`expirationTime` field and a `lanes` field. The implementations will
diverge, but not the types.
To do this, I lifted the type definitions into a separate module.
2020-04-09 14:48:24 +08:00
|
|
|
import type {Fiber} from './ReactInternalTypes';
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2022-09-10 04:03:48 +08:00
|
|
|
export type StackCursor<T> = {current: T};
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
const valueStack: Array<any> = [];
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
let fiberStack: Array<Fiber | null>;
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
|
fiberStack = [];
|
|
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
let index = -1;
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
function createCursor<T>(defaultValue: T): StackCursor<T> {
|
|
|
|
|
return {
|
|
|
|
|
current: defaultValue,
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
function isEmpty(): boolean {
|
|
|
|
|
return index === -1;
|
|
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
function pop<T>(cursor: StackCursor<T>, fiber: Fiber): void {
|
|
|
|
|
if (index < 0) {
|
2018-03-16 10:27:44 +08:00
|
|
|
if (__DEV__) {
|
2019-12-15 02:09:25 +08:00
|
|
|
console.error('Unexpected pop.');
|
2018-03-16 10:27:44 +08:00
|
|
|
}
|
2018-05-19 18:29:11 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-12-22 07:05:46 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
|
if (fiber !== fiberStack[index]) {
|
2019-12-15 02:09:25 +08:00
|
|
|
console.error('Unexpected Fiber popped.');
|
2018-05-19 18:29:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
cursor.current = valueStack[index];
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
valueStack[index] = null;
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
|
fiberStack[index] = null;
|
2018-03-16 10:27:44 +08:00
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
index--;
|
|
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
function push<T>(cursor: StackCursor<T>, value: T, fiber: Fiber): void {
|
|
|
|
|
index++;
|
2016-12-22 07:05:46 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
valueStack[index] = cursor.current;
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
|
fiberStack[index] = fiber;
|
2018-03-16 10:27:44 +08:00
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
cursor.current = value;
|
|
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
function checkThatStackIsEmpty() {
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
if (index !== -1) {
|
2019-12-15 02:09:25 +08:00
|
|
|
console.error(
|
|
|
|
|
'Expected an empty stack. Something was not reset properly.',
|
|
|
|
|
);
|
2018-03-16 10:27:44 +08:00
|
|
|
}
|
2016-12-21 10:04:38 +08:00
|
|
|
}
|
2018-05-19 18:29:11 +08:00
|
|
|
}
|
2018-03-16 10:27:44 +08:00
|
|
|
|
2018-05-19 18:29:11 +08:00
|
|
|
function resetStackAfterFatalErrorInDev() {
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
index = -1;
|
|
|
|
|
valueStack.length = 0;
|
|
|
|
|
fiberStack.length = 0;
|
|
|
|
|
}
|
2017-11-03 03:50:03 +08:00
|
|
|
}
|
2018-05-19 18:29:11 +08:00
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
createCursor,
|
|
|
|
|
isEmpty,
|
|
|
|
|
pop,
|
|
|
|
|
push,
|
|
|
|
|
// DEV only:
|
|
|
|
|
checkThatStackIsEmpty,
|
|
|
|
|
resetStackAfterFatalErrorInDev,
|
|
|
|
|
};
|