mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
44b594403f
* Add fetch instrumentation in cached contexts * Avoid unhandled rejection errors for Promises that we intentionally ignore In the final passes, we ignore the newly generated Promises and use the previous ones. This ensures that if those generate errors, that we intentionally ignore those. * Add extra fetch properties if there were any
57 lines
1.6 KiB
JavaScript
57 lines
1.6 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* 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';
|
|
|
|
function createSignal(): AbortSignal {
|
|
return new AbortController().signal;
|
|
}
|
|
|
|
export const DefaultCacheDispatcher: CacheDispatcher = {
|
|
getCacheSignal(): AbortSignal {
|
|
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;
|
|
},
|
|
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;
|
|
}
|