2020-04-07 06:43:39 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2020-04-07 06:43:39 +08:00
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*
|
|
|
|
|
* @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';
|
2020-04-07 06:43:39 +08:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
HostComponent,
|
2022-10-01 07:14:04 +08:00
|
|
|
HostResource,
|
2022-10-11 23:42:42 +08:00
|
|
|
HostSingleton,
|
2020-04-07 06:43:39 +08:00
|
|
|
LazyComponent,
|
|
|
|
|
SuspenseComponent,
|
|
|
|
|
SuspenseListComponent,
|
|
|
|
|
FunctionComponent,
|
|
|
|
|
IndeterminateComponent,
|
|
|
|
|
ForwardRef,
|
|
|
|
|
SimpleMemoComponent,
|
|
|
|
|
ClassComponent,
|
|
|
|
|
} from './ReactWorkTags';
|
|
|
|
|
import {
|
|
|
|
|
describeBuiltInComponentFrame,
|
|
|
|
|
describeFunctionComponentFrame,
|
|
|
|
|
describeClassComponentFrame,
|
|
|
|
|
} from 'shared/ReactComponentStackFrame';
|
|
|
|
|
|
|
|
|
|
function describeFiber(fiber: Fiber): string {
|
|
|
|
|
const owner: null | Function = __DEV__
|
|
|
|
|
? fiber._debugOwner
|
|
|
|
|
? fiber._debugOwner.type
|
|
|
|
|
: null
|
|
|
|
|
: null;
|
|
|
|
|
const source = __DEV__ ? fiber._debugSource : null;
|
|
|
|
|
switch (fiber.tag) {
|
2022-10-01 07:14:04 +08:00
|
|
|
case HostResource:
|
2022-10-11 23:42:42 +08:00
|
|
|
case HostSingleton:
|
2020-04-07 06:43:39 +08:00
|
|
|
case HostComponent:
|
|
|
|
|
return describeBuiltInComponentFrame(fiber.type, source, owner);
|
|
|
|
|
case LazyComponent:
|
|
|
|
|
return describeBuiltInComponentFrame('Lazy', source, owner);
|
|
|
|
|
case SuspenseComponent:
|
|
|
|
|
return describeBuiltInComponentFrame('Suspense', source, owner);
|
|
|
|
|
case SuspenseListComponent:
|
|
|
|
|
return describeBuiltInComponentFrame('SuspenseList', source, owner);
|
|
|
|
|
case FunctionComponent:
|
|
|
|
|
case IndeterminateComponent:
|
|
|
|
|
case SimpleMemoComponent:
|
|
|
|
|
return describeFunctionComponentFrame(fiber.type, source, owner);
|
|
|
|
|
case ForwardRef:
|
|
|
|
|
return describeFunctionComponentFrame(fiber.type.render, source, owner);
|
|
|
|
|
case ClassComponent:
|
|
|
|
|
return describeClassComponentFrame(fiber.type, source, owner);
|
|
|
|
|
default:
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
|
2020-04-11 10:39:02 +08:00
|
|
|
try {
|
|
|
|
|
let info = '';
|
2022-10-05 03:39:26 +08:00
|
|
|
let node: Fiber = workInProgress;
|
2020-04-11 10:39:02 +08:00
|
|
|
do {
|
|
|
|
|
info += describeFiber(node);
|
2022-10-05 03:39:26 +08:00
|
|
|
// $FlowFixMe[incompatible-type] we bail out when we get a null
|
2020-04-11 10:39:02 +08:00
|
|
|
node = node.return;
|
|
|
|
|
} while (node);
|
|
|
|
|
return info;
|
|
|
|
|
} catch (x) {
|
|
|
|
|
return '\nError generating stack: ' + x.message + '\n' + x.stack;
|
|
|
|
|
}
|
2020-04-07 06:43:39 +08:00
|
|
|
}
|