deno.land / x / pothos@release-1713397530 / packages / core / utils / context-cache.ts

context-cache.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// @ts-nocheckexport const contextCacheSymbol = Symbol.for("Pothos.contextCache");export function initContextCache() { return { [contextCacheSymbol]: {}, };}export interface ContextCache<T, C extends object, Args extends unknown[]> { (context: C, ...args: Args): T; delete: (context: C) => void;}export function createContextCache<T, C extends object = object, Args extends unknown[] = []>(create: (context: C, ...args: Args) => T): ContextCache<T, C, Args> { const cache = new WeakMap<object, T>(); 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;}
pothos

Version Info

Tagged at
a year ago