-
Notifications
You must be signed in to change notification settings - Fork 48.7k
[Flight] Implement FlightClient in terms of Thenable/Promises instead of throwing Promises #25260
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
Changes from all commits
73e6a44
23920ea
744b7bb
fda26be
372c008
737b236
b41cf4f
1c481e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,11 @@ | |
* @flow | ||
*/ | ||
|
||
import type {Thenable} from 'shared/ReactTypes'; | ||
import type { | ||
Thenable, | ||
FulfilledThenable, | ||
RejectedThenable, | ||
} from 'shared/ReactTypes'; | ||
|
||
export type WebpackSSRMap = { | ||
[clientId: string]: { | ||
|
@@ -56,7 +60,9 @@ const asyncModuleCache: Map<string, Thenable<any>> = new Map(); | |
|
||
// Start preloading the modules since we might need them soon. | ||
// This function doesn't suspend. | ||
export function preloadModule<T>(moduleData: ModuleReference<T>): void { | ||
export function preloadModule<T>( | ||
moduleData: ModuleReference<T>, | ||
): null | Thenable<any> { | ||
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. This changes the protocol of preloading modules. It has to suspend here and not in require module. This is not updated in ReactFlightDOMRelayClientIntegration because they live in www. So someone else has to update it. I don't think it's currently used there so it's not blocking. The protocol is that @frandiox this will also affect the Vite parts. 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. cc @kassens |
||
const chunks = moduleData.chunks; | ||
const promises = []; | ||
for (let i = 0; i < chunks.length; i++) { | ||
|
@@ -72,20 +78,35 @@ export function preloadModule<T>(moduleData: ModuleReference<T>): void { | |
} | ||
} | ||
if (moduleData.async) { | ||
const modulePromise: any = Promise.all(promises).then(() => { | ||
return __webpack_require__(moduleData.id); | ||
}); | ||
modulePromise.then( | ||
value => { | ||
modulePromise.status = 'fulfilled'; | ||
modulePromise.value = value; | ||
}, | ||
reason => { | ||
modulePromise.status = 'rejected'; | ||
modulePromise.reason = reason; | ||
}, | ||
); | ||
asyncModuleCache.set(moduleData.id, modulePromise); | ||
const existingPromise = asyncModuleCache.get(moduleData.id); | ||
if (existingPromise) { | ||
if (existingPromise.status === 'fulfilled') { | ||
return null; | ||
} | ||
return existingPromise; | ||
} else { | ||
const modulePromise: Thenable<T> = Promise.all(promises).then(() => { | ||
return __webpack_require__(moduleData.id); | ||
}); | ||
modulePromise.then( | ||
value => { | ||
const fulfilledThenable: FulfilledThenable<mixed> = (modulePromise: any); | ||
fulfilledThenable.status = 'fulfilled'; | ||
fulfilledThenable.value = value; | ||
}, | ||
reason => { | ||
const rejectedThenable: RejectedThenable<mixed> = (modulePromise: any); | ||
rejectedThenable.status = 'rejected'; | ||
rejectedThenable.reason = reason; | ||
}, | ||
); | ||
asyncModuleCache.set(moduleData.id, modulePromise); | ||
return modulePromise; | ||
} | ||
} else if (promises.length > 0) { | ||
return Promise.all(promises); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
|
@@ -99,23 +120,10 @@ export function requireModule<T>(moduleData: ModuleReference<T>): T { | |
const promise: any = asyncModuleCache.get(moduleData.id); | ||
if (promise.status === 'fulfilled') { | ||
moduleExports = promise.value; | ||
} else if (promise.status === 'rejected') { | ||
throw promise.reason; | ||
} else { | ||
throw promise; | ||
throw promise.reason; | ||
} | ||
} else { | ||
const chunks = moduleData.chunks; | ||
for (let i = 0; i < chunks.length; i++) { | ||
const chunkId = chunks[i]; | ||
const entry = chunkCache.get(chunkId); | ||
if (entry !== null) { | ||
// We assume that preloadModule has been called before. | ||
// So we don't expect to see entry being undefined here, that's an error. | ||
// Let's throw either an error or the Promise. | ||
throw entry; | ||
} | ||
} | ||
moduleExports = __webpack_require__(moduleData.id); | ||
} | ||
if (moduleData.name === '*') { | ||
|
Uh oh!
There was an error while loading. Please reload this page.