export const contextCacheSymbol = Symbol.for('Pothos.contextCache'); export function initContextCache() { return { [contextCacheSymbol]: {}, }; } export interface ContextCache { (context: C, ...args: Args): T; delete: (context: C) => void; } export function createContextCache( create: (context: C, ...args: Args) => T, ): ContextCache { const cache = new WeakMap(); const getOrCreate = (context: C, ...args: Args) => { const cacheKey = (context as { [contextCacheSymbol]: object })[contextCacheSymbol] || context; if (cache.has(cacheKey)) { return cache.get(cacheKey)!; } const entry = create(context, ...args); cache.set(cacheKey, entry); return entry; }; getOrCreate.delete = (context: C) => { const cacheKey = (context as { [contextCacheSymbol]: object })[contextCacheSymbol] || context; cache.delete(cacheKey); }; return getOrCreate; }