2022-10-13 11:13:39 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2022-10-13 11:13:39 +08:00
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type {CacheDispatcher} from 'react-reconciler/src/ReactInternalTypes';
|
|
|
|
|
|
2022-10-20 06:37:00 +08:00
|
|
|
function createSignal(): AbortSignal {
|
|
|
|
|
return new AbortController().signal;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 11:13:39 +08:00
|
|
|
export const DefaultCacheDispatcher: CacheDispatcher = {
|
|
|
|
|
getCacheSignal(): AbortSignal {
|
2022-10-20 06:37:00 +08:00
|
|
|
if (!currentCache) {
|
|
|
|
|
throw new Error('Reading the cache is only supported while rendering.');
|
|
|
|
|
}
|
|
|
|
|
let entry: AbortSignal | void = (currentCache.get(createSignal): any);
|
|
|
|
|
if (entry === undefined) {
|
|
|
|
|
entry = createSignal();
|
|
|
|
|
// $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
|
currentCache.set(createSignal, entry);
|
|
|
|
|
}
|
|
|
|
|
return entry;
|
2022-10-13 11:13:39 +08:00
|
|
|
},
|
|
|
|
|
getCacheForType<T>(resourceType: () => T): T {
|
|
|
|
|
if (!currentCache) {
|
|
|
|
|
throw new Error('Reading the cache is only supported while rendering.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let entry: T | void = (currentCache.get(resourceType): any);
|
|
|
|
|
if (entry === undefined) {
|
|
|
|
|
entry = resourceType();
|
|
|
|
|
// TODO: Warn if undefined?
|
|
|
|
|
// $FlowFixMe[incompatible-use] found when upgrading Flow
|
|
|
|
|
currentCache.set(resourceType, entry);
|
|
|
|
|
}
|
|
|
|
|
return entry;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let currentCache: Map<Function, mixed> | null = null;
|
|
|
|
|
|
|
|
|
|
export function setCurrentCache(
|
|
|
|
|
cache: Map<Function, mixed> | null,
|
|
|
|
|
): Map<Function, mixed> | null {
|
|
|
|
|
currentCache = cache;
|
|
|
|
|
return currentCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getCurrentCache(): Map<Function, mixed> | null {
|
|
|
|
|
return currentCache;
|
|
|
|
|
}
|