-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove _ctor field from Lazy components #18217
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2404c8
This type is all wrong and nothing cares because it's all any
sebmarkbage a1627d7
Refine Flow types of Lazy Components
sebmarkbage 1ac618d
Remove _ctor field from Lazy components
sebmarkbage 1b37fc2
Check for _ctor in case it's an older isomorphic that created it
sebmarkbage 326c710
Move types and constants from shared to isomorphic
sebmarkbage 01bf197
Move constants to shared
sebmarkbage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,46 +7,40 @@ | |
* @flow | ||
*/ | ||
|
||
export type Thenable<T, R> = { | ||
then(resolve: (T) => mixed, reject: (mixed) => mixed): R, | ||
... | ||
}; | ||
import type { | ||
PendingLazyComponent, | ||
ResolvedLazyComponent, | ||
RejectedLazyComponent, | ||
LazyComponent, | ||
} from 'react/src/ReactLazy'; | ||
|
||
export type LazyComponent<T> = { | ||
$$typeof: Symbol | number, | ||
_ctor: () => Thenable<{default: T, ...}, mixed>, | ||
_status: 0 | 1 | 2, | ||
_result: any, | ||
... | ||
}; | ||
|
||
type ResolvedLazyComponent<T> = { | ||
$$typeof: Symbol | number, | ||
_ctor: () => Thenable<{default: T, ...}, mixed>, | ||
_status: 1, | ||
_result: any, | ||
... | ||
}; | ||
|
||
export const Uninitialized = -1; | ||
export const Pending = 0; | ||
export const Resolved = 1; | ||
export const Rejected = 2; | ||
import { | ||
Uninitialized, | ||
Pending, | ||
Resolved, | ||
Rejected, | ||
} from './ReactLazyStatusTags'; | ||
|
||
export function refineResolvedLazyComponent<T>( | ||
lazyComponent: LazyComponent<T>, | ||
): ResolvedLazyComponent<T> | null { | ||
): T | null { | ||
return lazyComponent._status === Resolved ? lazyComponent._result : null; | ||
} | ||
|
||
export function initializeLazyComponentType( | ||
lazyComponent: LazyComponent<any>, | ||
): void { | ||
if (lazyComponent._status === Uninitialized) { | ||
lazyComponent._status = Pending; | ||
const ctor = lazyComponent._ctor; | ||
let ctor = lazyComponent._result; | ||
if (!ctor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coercion is OK here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we detect existence of something unknown things we tend to do this type of check. |
||
// TODO: Remove this later. THis only exists in case you use an older "react" package. | ||
ctor = ((lazyComponent: any)._ctor: typeof ctor); | ||
} | ||
const thenable = ctor(); | ||
lazyComponent._result = thenable; | ||
// Transition to the next state. | ||
const pending: PendingLazyComponent<any> = (lazyComponent: any); | ||
pending._status = Pending; | ||
pending._result = thenable; | ||
thenable.then( | ||
moduleObject => { | ||
if (lazyComponent._status === Pending) { | ||
|
@@ -61,14 +55,18 @@ export function initializeLazyComponentType( | |
); | ||
} | ||
} | ||
lazyComponent._status = Resolved; | ||
lazyComponent._result = defaultExport; | ||
// Transition to the next state. | ||
const resolved: ResolvedLazyComponent<any> = (lazyComponent: any); | ||
resolved._status = Resolved; | ||
resolved._result = defaultExport; | ||
} | ||
}, | ||
error => { | ||
if (lazyComponent._status === Pending) { | ||
lazyComponent._status = Rejected; | ||
lazyComponent._result = error; | ||
// Transition to the next state. | ||
const rejected: RejectedLazyComponent = (lazyComponent: any); | ||
rejected._status = Rejected; | ||
rejected._result = error; | ||
} | ||
}, | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
// TODO: Move this to "react" once we can import from externals. | ||
export const Uninitialized = -1; | ||
export const Pending = 0; | ||
export const Resolved = 1; | ||
export const Rejected = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also upstreamed all of this to the isomorphic package since it owns this data structure. I'll think a bit how we can move the other two helpers out of
/shared
.