Files
react/packages/shared/ReactLazyComponent.js
Andrew Clark 5031ebf6be Accept promise as element type (#13397)
* Accept promise as element type

On the initial render, the element will suspend as if a promise were
thrown from inside the body of the unresolved component. Siblings should
continue rendering and if the parent is a Placeholder, the promise
should be captured by that Placeholder.

When the promise resolves, rendering resumes. If the resolved value
has a `default` property, it is assumed to be the default export of
an ES module, and we use that as the component type. If it does not have
a `default` property, we use the resolved value itself.

The resolved value is stored as an expando on the promise/thenable.

* Use special types of work for lazy components

Because reconciliation is a hot path, this adds ClassComponentLazy,
FunctionalComponentLazy, and ForwardRefLazy as special types of work.
The other types are not supported, but wouldn't be placed into a
separate module regardless.

* Resolve defaultProps for lazy types

* Remove some calls to isContextProvider

isContextProvider checks the fiber tag, but it's typically called after
we've already refined the type of work. We should get rid of it. I
removed some of them in the previous commit, and deleted a few more
in this one. I left a few behind because the remaining ones would
require additional refactoring that feels outside the scope of this PR.

* Remove getLazyComponentTypeIfResolved

* Return baseProps instead of null

The caller compares the result to baseProps to see if anything changed.

* Avoid redundant checks by inlining getFiberTagFromObjectType

* Move tag resolution to ReactFiber module

* Pass next props to update* functions

We should do this with all types of work in the future.

* Refine component type before pushing/popping context

Removes unnecessary checks.

* Replace all occurrences of _reactResult with helper

* Move shared thenable logic to `shared` package

* Check type of wrapper object before resolving to `default` export

* Return resolved tag instead of reassigning
2018-08-16 09:21:59 -07:00

37 lines
861 B
JavaScript

/**
* 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
*/
export type Thenable<T> = {
then(resolve: (T) => mixed, reject: (mixed) => mixed): mixed,
_reactStatus?: 0 | 1 | 2,
_reactResult: any,
};
type ResolvedThenable<T> = {
then(resolve: (T) => mixed, reject: (mixed) => mixed): mixed,
_reactStatus?: 1,
_reactResult: T,
};
export const Pending = 0;
export const Resolved = 1;
export const Rejected = 2;
export function getResultFromResolvedThenable<T>(
thenable: ResolvedThenable<T>,
): T {
return thenable._reactResult;
}
export function refineResolvedThenable<T>(
thenable: Thenable<T>,
): ResolvedThenable<T> | null {
return thenable._reactStatus === Resolved ? thenable._reactResult : null;
}