| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {AsyncDispatcher} from 'react-reconciler/src/ReactInternalTypes'; |
| 11 | |
| 12 | import {resolveRequest, getCache} from '../ReactFlightServer'; |
| 13 | import {resolveOwner} from './ReactFlightCurrentOwner'; |
| 14 | |
| 15 | function resolveCache(): Map<Function, mixed> { |
| 16 | const request = resolveRequest(); |
| 17 | if (request) { |
| 18 | return getCache(request); |
| 19 | } |
| 20 | return new Map(); |
| 21 | } |
| 22 | |
| 23 | export const DefaultAsyncDispatcher: AsyncDispatcher = { |
| 24 | getCacheForType<T>(resourceType: () => T): T { |
| 25 | const cache = resolveCache(); |
| 26 | let entry: T | void = cache.get(resourceType) as any; |
| 27 | if (entry === undefined) { |
| 28 | entry = resourceType(); |
| 29 | // TODO: Warn if undefined? |
| 30 | cache.set(resourceType, entry); |
| 31 | } |
| 32 | return entry; |
| 33 | }, |
| 34 | cacheSignal(): null | AbortSignal { |
| 35 | const request = resolveRequest(); |
| 36 | if (request) { |
| 37 | return request.cacheController.signal; |
| 38 | } |
| 39 | return null; |
| 40 | }, |
| 41 | } as any; |
| 42 | |
| 43 | if (__DEV__) { |
| 44 | DefaultAsyncDispatcher.getOwner = resolveOwner; |
| 45 | } |