| 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, Fiber} from './ReactInternalTypes'; |
| 11 | import type {Cache} from './ReactFiberCacheComponent'; |
| 12 | |
| 13 | import {readContext} from './ReactFiberNewContext'; |
| 14 | import {CacheContext} from './ReactFiberCacheComponent'; |
| 15 | |
| 16 | import {current as currentOwner} from './ReactCurrentFiber'; |
| 17 | |
| 18 | function getCacheForType<T>(resourceType: () => T): T { |
| 19 | const cache: Cache = readContext(CacheContext); |
| 20 | let cacheForType: T | void = cache.data.get(resourceType) as any; |
| 21 | if (cacheForType === undefined) { |
| 22 | cacheForType = resourceType(); |
| 23 | cache.data.set(resourceType, cacheForType); |
| 24 | } |
| 25 | return cacheForType; |
| 26 | } |
| 27 | |
| 28 | function cacheSignal(): null | AbortSignal { |
| 29 | const cache: Cache = readContext(CacheContext); |
| 30 | return cache.controller.signal; |
| 31 | } |
| 32 | |
| 33 | export const DefaultAsyncDispatcher: AsyncDispatcher = { |
| 34 | getCacheForType, |
| 35 | cacheSignal, |
| 36 | } as any; |
| 37 | |
| 38 | if (__DEV__) { |
| 39 | DefaultAsyncDispatcher.getOwner = (): null | Fiber => { |
| 40 | return currentOwner; |
| 41 | }; |
| 42 | } |