Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
38 lines
1.4 KiB
Plaintext
38 lines
1.4 KiB
Plaintext
import { cache } from 'react';
|
|
// Module-scoped cache container that holds all cached, stable containers
|
|
// - these may hold the stable value, or a promise to the stable value
|
|
const globalCacheContainer = {};
|
|
/**
|
|
* Creates a selective cache function that provides more control over React's request-level caching behavior.
|
|
*
|
|
* @param namespace - A namespace to group related cached values
|
|
* @returns A function that manages cached values within the specified namespace
|
|
*/
|
|
export function selectiveCache(namespace) {
|
|
// Create a stable namespace container if it doesn't exist
|
|
if (!globalCacheContainer[namespace]) {
|
|
globalCacheContainer[namespace] = cache((...args) => ({
|
|
value: null
|
|
}));
|
|
}
|
|
/**
|
|
* Gets or creates a cached value for a specific key within the namespace
|
|
*
|
|
* @param key - The key to identify the cached value
|
|
* @param factory - A function that produces the value if not cached
|
|
* @returns The cached or newly created value
|
|
*/
|
|
const getCached = async (factory, ...cacheArgs) => {
|
|
const stableObjectFn = globalCacheContainer[namespace];
|
|
const stableObject = stableObjectFn(...cacheArgs);
|
|
if (stableObject?.value && 'then' in stableObject.value && typeof stableObject.value?.then === 'function') {
|
|
return await stableObject.value;
|
|
}
|
|
stableObject.value = factory();
|
|
return await stableObject.value;
|
|
};
|
|
return {
|
|
get: getCached
|
|
};
|
|
}
|
|
//# sourceMappingURL=selectiveCache.js.map |