| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {isPromise} from 'shared/utils'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | /** |
| b69ab31 | | | 11 | * Return the promise result, raise the promise error, or "suspend" React's rendering. |
| b69ab31 | | | 12 | * The callsite should have `<Suspense>` and `<ErrorBoundary>` in a parent component |
| b69ab31 | | | 13 | * to support suspension and error handling, or use `<SuspenseBoundary>`. |
| b69ab31 | | | 14 | * |
| b69ab31 | | | 15 | * Be aware that the function that produces `promise` should returning |
| b69ab31 | | | 16 | * `Promise.resolve(data)`, and instead return `data` directly. This is because |
| b69ab31 | | | 17 | * Javascript does not provide a way to test if a Promise is resolved without async. |
| b69ab31 | | | 18 | * So the Promise will be treated as "pending" temporarily, rendering the Suspense |
| b69ab31 | | | 19 | * fallback. The actual Suspense children will lose their states because the |
| b69ab31 | | | 20 | * fallback replaces them. See the `maybePromise` below. |
| b69ab31 | | | 21 | * |
| b69ab31 | | | 22 | * Example: |
| b69ab31 | | | 23 | * |
| b69ab31 | | | 24 | * ``` |
| b69ab31 | | | 25 | * // Parent component with `<Suspense>`. |
| b69ab31 | | | 26 | * function Container(props: {path: string}) { |
| b69ab31 | | | 27 | * return <SuspenseBoundary><Inner /></SuspenseBoundary>; |
| b69ab31 | | | 28 | * } |
| b69ab31 | | | 29 | * |
| b69ab31 | | | 30 | * // Child component using `usePromise`. |
| b69ab31 | | | 31 | * function Inner() { |
| b69ab31 | | | 32 | * const data = usePromise(maybePromise()); |
| b69ab31 | | | 33 | * return <Data data={data} />; |
| b69ab31 | | | 34 | * } |
| b69ab31 | | | 35 | * |
| b69ab31 | | | 36 | * function maybePromise(): Data | Promise<Data> { |
| b69ab31 | | | 37 | * if (isDataReady()) { |
| b69ab31 | | | 38 | * // Do not return Promise.resolve(data). That loses <Inner /> state. |
| b69ab31 | | | 39 | * return data; |
| b69ab31 | | | 40 | * } |
| b69ab31 | | | 41 | * ... |
| b69ab31 | | | 42 | * } |
| b69ab31 | | | 43 | * ``` |
| b69ab31 | | | 44 | * |
| b69ab31 | | | 45 | * Alternatively, the promise can be passed from a more "stable" stateful |
| b69ab31 | | | 46 | * parent component that keeps the promise object unchanged when <Suspense> |
| b69ab31 | | | 47 | * switches between fallback to non-fallback: |
| b69ab31 | | | 48 | * |
| b69ab31 | | | 49 | * ``` |
| b69ab31 | | | 50 | * // Parent component with `<Suspense>`. |
| b69ab31 | | | 51 | * function Container(props: {path: string}) { |
| b69ab31 | | | 52 | * const loader = useLoader(); |
| b69ab31 | | | 53 | * const promise = loader.load(props.path); |
| b69ab31 | | | 54 | * return <SuspenseBoundary><Inner promise={promise} /></SuspenseBoundary>; |
| b69ab31 | | | 55 | * } |
| b69ab31 | | | 56 | * |
| b69ab31 | | | 57 | * // Child component using `usePromise`. |
| b69ab31 | | | 58 | * function Inner(props: {promise: ...}) { |
| b69ab31 | | | 59 | * // The promise is from the parent of <Suspense />. |
| b69ab31 | | | 60 | * const data = usePromise(promise); |
| b69ab31 | | | 61 | * return <Data data={data} />; |
| b69ab31 | | | 62 | * } |
| b69ab31 | | | 63 | * ``` |
| b69ab31 | | | 64 | * |
| b69ab31 | | | 65 | * See also https://github.com/reactjs/react.dev/blob/3364c93feb358a7d1ac2e8d8b0468c3e32214062/src/content/reference/react/Suspense.md?plain=1#L141 |
| b69ab31 | | | 66 | */ |
| b69ab31 | | | 67 | export function usePromise<T>(promise: T | PromiseExt<T>): T { |
| b69ab31 | | | 68 | if (!isPromise(promise)) { |
| b69ab31 | | | 69 | return promise; |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | const status = promise.usePromiseStatus; |
| b69ab31 | | | 72 | if (status === undefined) { |
| b69ab31 | | | 73 | promise.usePromiseStatus = 'pending'; |
| b69ab31 | | | 74 | promise.then( |
| b69ab31 | | | 75 | resolve => { |
| b69ab31 | | | 76 | promise.usePromiseStatus = ['ok', resolve]; |
| b69ab31 | | | 77 | }, |
| b69ab31 | | | 78 | error => { |
| b69ab31 | | | 79 | promise.usePromiseStatus = ['error', error]; |
| b69ab31 | | | 80 | }, |
| b69ab31 | | | 81 | ); |
| b69ab31 | | | 82 | // This is the undocumented API to make <Suspense /> render its fallback. |
| b69ab31 | | | 83 | // React might change it in the future. But it has been like this for years. |
| b69ab31 | | | 84 | throw promise; |
| b69ab31 | | | 85 | } else if (status === 'pending') { |
| b69ab31 | | | 86 | throw promise; |
| b69ab31 | | | 87 | } else if (status[0] === 'ok') { |
| b69ab31 | | | 88 | return status[1]; |
| b69ab31 | | | 89 | } else { |
| b69ab31 | | | 90 | throw status[1]; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | export interface PromiseExt<T> extends Promise<T> { |
| b69ab31 | | | 95 | usePromiseStatus?: ['ok', T] | ['error', Error] | 'pending'; |
| b69ab31 | | | 96 | } |