Skip to content
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

[Flight] Add getCacheForType() to the dispatcher #20315

Merged
merged 13 commits into from
Dec 3, 2020
Prev Previous commit
Next Next commit
Unroll async/await because build system
  • Loading branch information
gaearon committed Dec 3, 2020
commit 5186e0108d845e7e88d73866f4fc1ec09c25ac57
35 changes: 19 additions & 16 deletions packages/react-suspense-test-utils/src/ReactSuspenseTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function unsupported() {
invariant(false, 'This feature is not supported by ReactSuspenseTestUtils.');
}

export async function waitForSuspense<T>(fn: () => T): Promise<T> {
export function waitForSuspense<T>(fn: () => T): Promise<T> {
const cache: Map<Function, mixed> = new Map();
const testDispatcher: Dispatcher = {
getCacheForType<R>(resourceType: () => R): R {
Expand Down Expand Up @@ -45,21 +45,24 @@ export async function waitForSuspense<T>(fn: () => T): Promise<T> {
useOpaqueIdentifier: unsupported,
useMutableSource: unsupported,
};
while (true) {
const prevDispatcher = ReactCurrentDispatcher.current;
ReactCurrentDispatcher.current = testDispatcher;
try {
return fn();
} catch (promise) {
if (typeof promise.then === 'function') {
await promise;
} else {
throw promise;
// Not using async/await because we don't compile it.
return new Promise((resolve, reject) => {
function retry() {
const prevDispatcher = ReactCurrentDispatcher.current;
ReactCurrentDispatcher.current = testDispatcher;
try {
const result = fn();
resolve(result);
} catch (thrownValue) {
if (typeof thrownValue.then === 'function') {
thrownValue.then(retry, reject);
gaearon marked this conversation as resolved.
Show resolved Hide resolved
} else {
reject(thrownValue);
}
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
}
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
}
}
// eslint-disable-next-line no-unreachable
return (undefined: any);
retry();
});
}