UNPKG

@tldraw/utils

Version:

tldraw infinite canvas SDK (private utilities).

8 lines (7 loc) 1.12 kB
{ "version": 3, "sources": ["../../src/lib/cache.ts"], "sourcesContent": ["/**\n * A micro cache used when storing records in memory (using a WeakMap).\n * @public\n */\nexport class WeakCache<K extends object, V> {\n\t/** The map of items to their cached values. */\n\titems = new WeakMap<K, V>()\n\n\t/**\n\t * Get the cached value for a given record. If the record is not present in the map, the callback\n\t * will be used to create the value (with the result being stored in the cache for next time).\n\t *\n\t * @param item - The item to get.\n\t * @param cb - The callback to use to create the value when a cached value is not found.\n\t */\n\tget<P extends K>(item: P, cb: (item: P) => V) {\n\t\tif (!this.items.has(item)) {\n\t\t\tthis.items.set(item, cb(item))\n\t\t}\n\n\t\treturn this.items.get(item)!\n\t}\n}\n"], "mappings": "AAIO,MAAM,UAA+B;AAAA;AAAA,EAE3C,QAAQ,oBAAI,QAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS1B,IAAiB,MAAS,IAAoB;AAC7C,QAAI,CAAC,KAAK,MAAM,IAAI,IAAI,GAAG;AAC1B,WAAK,MAAM,IAAI,MAAM,GAAG,IAAI,CAAC;AAAA,IAC9B;AAEA,WAAO,KAAK,MAAM,IAAI,IAAI;AAAA,EAC3B;AACD;", "names": [] }