.
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
||||
import { createDefaultCacheHandler } from '../lib/cache-handlers/default';
|
||||
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE ? (message, ...args)=>{
|
||||
console.log(`use-cache: ${message}`, ...args);
|
||||
} : undefined;
|
||||
const handlersSymbol = Symbol.for('@next/cache-handlers');
|
||||
const handlersMapSymbol = Symbol.for('@next/cache-handlers-map');
|
||||
const handlersSetSymbol = Symbol.for('@next/cache-handlers-set');
|
||||
/**
|
||||
* The reference to the cache handlers. We store the cache handlers on the
|
||||
* global object so that we can access the same instance across different
|
||||
* boundaries (such as different copies of the same module).
|
||||
*/ const reference = globalThis;
|
||||
/**
|
||||
* Initialize the cache handlers.
|
||||
* @param cacheMaxMemorySize - The maximum memory size of the cache in bytes, if
|
||||
* not provided, the default memory size will be used.
|
||||
* @returns `true` if the cache handlers were initialized, `false` if they were already initialized.
|
||||
*/ export function initializeCacheHandlers(cacheMaxMemorySize) {
|
||||
// If the cache handlers have already been initialized, don't do it again.
|
||||
if (reference[handlersMapSymbol]) {
|
||||
debug == null ? void 0 : debug('cache handlers already initialized');
|
||||
return false;
|
||||
}
|
||||
debug == null ? void 0 : debug('initializing cache handlers');
|
||||
reference[handlersMapSymbol] = new Map();
|
||||
// Initialize the cache from the symbol contents first.
|
||||
if (reference[handlersSymbol]) {
|
||||
let fallback;
|
||||
if (reference[handlersSymbol].DefaultCache) {
|
||||
debug == null ? void 0 : debug('setting "default" cache handler from symbol');
|
||||
fallback = reference[handlersSymbol].DefaultCache;
|
||||
} else {
|
||||
debug == null ? void 0 : debug('setting "default" cache handler from default');
|
||||
fallback = createDefaultCacheHandler(cacheMaxMemorySize);
|
||||
}
|
||||
reference[handlersMapSymbol].set('default', fallback);
|
||||
if (reference[handlersSymbol].RemoteCache) {
|
||||
debug == null ? void 0 : debug('setting "remote" cache handler from symbol');
|
||||
reference[handlersMapSymbol].set('remote', reference[handlersSymbol].RemoteCache);
|
||||
} else {
|
||||
debug == null ? void 0 : debug('setting "remote" cache handler from default');
|
||||
reference[handlersMapSymbol].set('remote', fallback);
|
||||
}
|
||||
} else {
|
||||
const handler = createDefaultCacheHandler(cacheMaxMemorySize);
|
||||
debug == null ? void 0 : debug('setting "default" cache handler from default');
|
||||
reference[handlersMapSymbol].set('default', handler);
|
||||
debug == null ? void 0 : debug('setting "remote" cache handler from default');
|
||||
reference[handlersMapSymbol].set('remote', handler);
|
||||
}
|
||||
// Create a set of the cache handlers.
|
||||
reference[handlersSetSymbol] = new Set(reference[handlersMapSymbol].values());
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Get a cache handler by kind.
|
||||
* @param kind - The kind of cache handler to get.
|
||||
* @returns The cache handler, or `undefined` if it does not exist.
|
||||
* @throws If the cache handlers are not initialized.
|
||||
*/ export function getCacheHandler(kind) {
|
||||
// This should never be called before initializeCacheHandlers.
|
||||
if (!reference[handlersMapSymbol]) {
|
||||
throw Object.defineProperty(new Error('Cache handlers not initialized'), "__NEXT_ERROR_CODE", {
|
||||
value: "E649",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
return reference[handlersMapSymbol].get(kind);
|
||||
}
|
||||
/**
|
||||
* Get a set iterator over the cache handlers.
|
||||
* @returns An iterator over the cache handlers, or `undefined` if they are not
|
||||
* initialized.
|
||||
*/ export function getCacheHandlers() {
|
||||
if (!reference[handlersSetSymbol]) {
|
||||
return undefined;
|
||||
}
|
||||
return reference[handlersSetSymbol].values();
|
||||
}
|
||||
/**
|
||||
* Get a map iterator over the cache handlers (keyed by kind).
|
||||
* @returns An iterator over the cache handler entries, or `undefined` if they
|
||||
* are not initialized.
|
||||
* @throws If the cache handlers are not initialized.
|
||||
*/ export function getCacheHandlerEntries() {
|
||||
if (!reference[handlersMapSymbol]) {
|
||||
return undefined;
|
||||
}
|
||||
return reference[handlersMapSymbol].entries();
|
||||
}
|
||||
/**
|
||||
* Set a cache handler by kind.
|
||||
* @param kind - The kind of cache handler to set.
|
||||
* @param cacheHandler - The cache handler to set.
|
||||
*/ export function setCacheHandler(kind, cacheHandler) {
|
||||
// This should never be called before initializeCacheHandlers.
|
||||
if (!reference[handlersMapSymbol] || !reference[handlersSetSymbol]) {
|
||||
throw Object.defineProperty(new Error('Cache handlers not initialized'), "__NEXT_ERROR_CODE", {
|
||||
value: "E649",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
debug == null ? void 0 : debug('setting cache handler for "%s"', kind);
|
||||
reference[handlersMapSymbol].set(kind, cacheHandler);
|
||||
reference[handlersSetSymbol].add(cacheHandler);
|
||||
}
|
||||
|
||||
//# sourceMappingURL=handlers.js.map
|
||||
Reference in New Issue
Block a user