{"version":3,"file":"bundle.esm.js","sources":["../src/cache.ts","../src/context.ts","../src/entry.ts","../src/key-trie.ts","../src/index.ts"],"sourcesContent":["interface Node {\n key: K;\n value: V;\n newer: Node | null;\n older: Node | null;\n}\n\nfunction defaultDispose() {}\n\nexport class Cache {\n private map = new Map>();\n private newest: Node | null = null;\n private oldest: Node | null = null;\n\n constructor(\n private max = Infinity,\n public dispose: (value: V, key: K) => void = defaultDispose,\n ) {}\n\n public has(key: K) {\n return this.map.has(key);\n }\n\n public get(key: K) {\n const entry = this.getEntry(key);\n return entry && entry.value;\n }\n\n private getEntry(key: K): Node | void {\n const entry = this.map.get(key);\n\n if (entry && entry !== this.newest) {\n const { older, newer } = entry;\n\n if (newer) {\n newer.older = older;\n }\n\n if (older) {\n older.newer = newer;\n }\n\n entry.older = this.newest;\n entry.older!.newer = entry;\n\n entry.newer = null;\n this.newest = entry;\n\n if (entry === this.oldest) {\n this.oldest = newer;\n }\n }\n\n return entry;\n }\n\n public set(key: K, value: V) {\n let entry = this.getEntry(key);\n if (entry) {\n return entry.value = value;\n }\n\n entry = {\n key: key,\n value: value,\n newer: null,\n older: this.newest\n };\n\n if (this.newest) {\n this.newest.newer = entry;\n }\n\n this.newest = entry;\n this.oldest = this.oldest || entry;\n\n this.map.set(key, entry);\n\n return entry.value;\n }\n\n public clean() {\n while (this.oldest && this.map.size > this.max) {\n this.delete(this.oldest.key);\n }\n }\n\n public delete(key: K) {\n const entry = this.map.get(key);\n if (entry) {\n if (entry === this.newest) {\n this.newest = entry.older;\n }\n\n if (entry === this.oldest) {\n this.oldest = entry.newer;\n }\n\n if (entry.newer) {\n entry.newer.older = entry.older;\n }\n\n if (entry.older) {\n entry.older.newer = entry.newer;\n }\n\n this.map.delete(key);\n this.dispose(entry.value, key);\n\n return true;\n }\n\n return false;\n }\n}\n","import { AnyEntry } from \"./entry\";\nimport { Slot } from \"@wry/context\";\n\nexport const parentEntrySlot = new Slot();\n\nexport {\n bind as bindContext,\n noContext,\n setTimeout,\n asyncFromGen,\n} from \"@wry/context\";\n","import { parentEntrySlot } from \"./context\";\nimport { OptimisticWrapOptions } from \"./index\";\n\nconst reusableEmptyArray: AnyEntry[] = [];\nconst emptySetPool: Set[] = [];\nconst POOL_TARGET_SIZE = 100;\n\n// Since this package might be used browsers, we should avoid using the\n// Node built-in assert module.\nfunction assert(condition: any, optionalMessage?: string) {\n if (! condition) {\n throw new Error(optionalMessage || \"assertion failure\");\n }\n}\n\n// Since exceptions are cached just like normal values, we need an efficient\n// way of representing unknown, ordinary, and exceptional values.\ntype Value =\n | [] // unknown\n | [T] // known value\n | [void, any]; // known exception\n\nfunction valueIs(a: Value, b: Value) {\n const len = a.length;\n return (\n // Unknown values are not equal to each other.\n len > 0 &&\n // Both values must be ordinary (or both exceptional) to be equal.\n len === b.length &&\n // The underlying value or exception must be the same.\n a[len - 1] === b[len - 1]\n );\n}\n\nfunction valueGet(value: Value): T {\n switch (value.length) {\n case 0: throw new Error(\"unknown value\");\n case 1: return value[0];\n case 2: throw value[1];\n }\n}\n\nfunction valueCopy(value: Value): Value {\n return value.slice(0) as Value;\n}\n\nexport type AnyEntry = Entry;\n\nexport class Entry {\n public static count = 0;\n\n public subscribe: OptimisticWrapOptions[\"subscribe\"];\n public unsubscribe?: () => any;\n public reportOrphan?: (this: Entry) => any;\n\n public readonly parents = new Set();\n public readonly childValues = new Map>();\n\n // When this Entry has children that are dirty, this property becomes\n // a Set containing other Entry objects, borrowed from emptySetPool.\n // When the set becomes empty, it gets recycled back to emptySetPool.\n public dirtyChildren: Set | null = null;\n\n public dirty = true;\n public recomputing = false;\n public readonly value: Value = [];\n\n constructor(\n public readonly fn: (...args: TArgs) => TValue,\n public args: TArgs,\n ) {\n ++Entry.count;\n }\n\n // This is the most important method of the Entry API, because it\n // determines whether the cached this.value can be returned immediately,\n // or must be recomputed. The overall performance of the caching system\n // depends on the truth of the following observations: (1) this.dirty is\n // usually false, (2) this.dirtyChildren is usually null/empty, and thus\n // (3) valueGet(this.value) is usually returned without recomputation.\n public recompute(): TValue {\n assert(! this.recomputing, \"already recomputing\");\n\n if (! rememberParent(this) && maybeReportOrphan(this)) {\n // The recipient of the entry.reportOrphan callback decided to dispose\n // of this orphan entry by calling entry.dispose(), so we don't need to\n // (and should not) proceed with the recomputation.\n return void 0 as any;\n }\n\n return mightBeDirty(this)\n ? reallyRecompute(this)\n : valueGet(this.value);\n }\n\n public setDirty() {\n if (this.dirty) return;\n this.dirty = true;\n this.value.length = 0;\n reportDirty(this);\n // We can go ahead and unsubscribe here, since any further dirty\n // notifications we receive will be redundant, and unsubscribing may\n // free up some resources, e.g. file watchers.\n maybeUnsubscribe(this);\n }\n\n public dispose() {\n forgetChildren(this).forEach(maybeReportOrphan);\n maybeUnsubscribe(this);\n\n // Because this entry has been kicked out of the cache (in index.js),\n // we've lost the ability to find out if/when this entry becomes dirty,\n // whether that happens through a subscription, because of a direct call\n // to entry.setDirty(), or because one of its children becomes dirty.\n // Because of this loss of future information, we have to assume the\n // worst (that this entry might have become dirty very soon), so we must\n // immediately mark this entry's parents as dirty. Normally we could\n // just call entry.setDirty() rather than calling parent.setDirty() for\n // each parent, but that would leave this entry in parent.childValues\n // and parent.dirtyChildren, which would prevent the child from being\n // truly forgotten.\n this.parents.forEach(parent => {\n parent.setDirty();\n forgetChild(parent, this);\n });\n }\n}\n\nfunction rememberParent(child: AnyEntry) {\n const parent = parentEntrySlot.getValue();\n if (parent) {\n child.parents.add(parent);\n\n if (! parent.childValues.has(child)) {\n parent.childValues.set(child, []);\n }\n\n if (mightBeDirty(child)) {\n reportDirtyChild(parent, child);\n } else {\n reportCleanChild(parent, child);\n }\n\n return parent;\n }\n}\n\nfunction reallyRecompute(entry: AnyEntry) {\n // Since this recomputation is likely to re-remember some of this\n // entry's children, we forget our children here but do not call\n // maybeReportOrphan until after the recomputation finishes.\n const originalChildren = forgetChildren(entry);\n\n // Set entry as the parent entry while calling recomputeNewValue(entry).\n parentEntrySlot.withValue(entry, recomputeNewValue, [entry]);\n\n if (maybeSubscribe(entry)) {\n // If we successfully recomputed entry.value and did not fail to\n // (re)subscribe, then this Entry is no longer explicitly dirty.\n setClean(entry);\n }\n\n // Now that we've had a chance to re-remember any children that were\n // involved in the recomputation, we can safely report any orphan\n // children that remain.\n originalChildren.forEach(maybeReportOrphan);\n\n return valueGet(entry.value);\n}\n\nfunction recomputeNewValue(entry: AnyEntry) {\n entry.recomputing = true;\n // Set entry.value as unknown.\n entry.value.length = 0;\n try {\n // If entry.fn succeeds, entry.value will become a normal Value.\n entry.value[0] = entry.fn.apply(null, entry.args);\n } catch (e) {\n // If entry.fn throws, entry.value will become exceptional.\n entry.value[1] = e;\n }\n // Either way, this line is always reached.\n entry.recomputing = false;\n}\n\nfunction mightBeDirty(entry: AnyEntry) {\n return entry.dirty || !!(entry.dirtyChildren && entry.dirtyChildren.size);\n}\n\nfunction setClean(entry: AnyEntry) {\n entry.dirty = false;\n\n if (mightBeDirty(entry)) {\n // This Entry may still have dirty children, in which case we can't\n // let our parents know we're clean just yet.\n return;\n }\n\n reportClean(entry);\n}\n\nfunction reportDirty(child: AnyEntry) {\n child.parents.forEach(parent => reportDirtyChild(parent, child));\n}\n\nfunction reportClean(child: AnyEntry) {\n child.parents.forEach(parent => reportCleanChild(parent, child));\n}\n\n// Let a parent Entry know that one of its children may be dirty.\nfunction reportDirtyChild(parent: AnyEntry, child: AnyEntry) {\n // Must have called rememberParent(child) before calling\n // reportDirtyChild(parent, child).\n assert(parent.childValues.has(child));\n assert(mightBeDirty(child));\n\n if (! parent.dirtyChildren) {\n parent.dirtyChildren = emptySetPool.pop() || new Set;\n\n } else if (parent.dirtyChildren.has(child)) {\n // If we already know this child is dirty, then we must have already\n // informed our own parents that we are dirty, so we can terminate\n // the recursion early.\n return;\n }\n\n parent.dirtyChildren.add(child);\n reportDirty(parent);\n}\n\n// Let a parent Entry know that one of its children is no longer dirty.\nfunction reportCleanChild(parent: AnyEntry, child: AnyEntry) {\n // Must have called rememberChild(child) before calling\n // reportCleanChild(parent, child).\n assert(parent.childValues.has(child));\n assert(! mightBeDirty(child));\n\n const childValue = parent.childValues.get(child)!;\n if (childValue.length === 0) {\n parent.childValues.set(child, valueCopy(child.value));\n } else if (! valueIs(childValue, child.value)) {\n parent.setDirty();\n }\n\n removeDirtyChild(parent, child);\n\n if (mightBeDirty(parent)) {\n return;\n }\n\n reportClean(parent);\n}\n\nfunction removeDirtyChild(parent: AnyEntry, child: AnyEntry) {\n const dc = parent.dirtyChildren;\n if (dc) {\n dc.delete(child);\n if (dc.size === 0) {\n if (emptySetPool.length < POOL_TARGET_SIZE) {\n emptySetPool.push(dc);\n }\n parent.dirtyChildren = null;\n }\n }\n}\n\n// If the given entry has a reportOrphan method, and no remaining parents,\n// call entry.reportOrphan and return true iff it returns true. The\n// reportOrphan function should return true to indicate entry.dispose()\n// has been called, and the entry has been removed from any other caches\n// (see index.js for the only current example).\nfunction maybeReportOrphan(entry: AnyEntry) {\n return entry.parents.size === 0 &&\n typeof entry.reportOrphan === \"function\" &&\n entry.reportOrphan() === true;\n}\n\n// Removes all children from this entry and returns an array of the\n// removed children.\nfunction forgetChildren(parent: AnyEntry) {\n let children = reusableEmptyArray;\n\n if (parent.childValues.size > 0) {\n children = [];\n parent.childValues.forEach((_value, child) => {\n forgetChild(parent, child);\n children.push(child);\n });\n }\n\n // After we forget all our children, this.dirtyChildren must be empty\n // and therefore must have been reset to null.\n assert(parent.dirtyChildren === null);\n\n return children;\n}\n\nfunction forgetChild(parent: AnyEntry, child: AnyEntry) {\n child.parents.delete(parent);\n parent.childValues.delete(child);\n removeDirtyChild(parent, child);\n}\n\nfunction maybeSubscribe(entry: AnyEntry) {\n if (typeof entry.subscribe === \"function\") {\n try {\n maybeUnsubscribe(entry); // Prevent double subscriptions.\n entry.unsubscribe = entry.subscribe.apply(null, entry.args);\n } catch (e) {\n // If this Entry has a subscribe function and it threw an exception\n // (or an unsubscribe function it previously returned now throws),\n // return false to indicate that we were not able to subscribe (or\n // unsubscribe), and this Entry should remain dirty.\n entry.setDirty();\n return false;\n }\n }\n\n // Returning true indicates either that there was no entry.subscribe\n // function or that it succeeded.\n return true;\n}\n\nfunction maybeUnsubscribe(entry: AnyEntry) {\n const { unsubscribe } = entry;\n if (typeof unsubscribe === \"function\") {\n entry.unsubscribe = void 0;\n unsubscribe();\n }\n}\n","// A trie data structure that holds object keys weakly, yet can also hold\n// non-object keys, unlike the native `WeakMap`.\nexport class KeyTrie {\n // Since a `WeakMap` cannot hold primitive values as keys, we need a\n // backup `Map` instance to hold primitive keys. Both `this._weakMap`\n // and `this._strongMap` are lazily initialized.\n private weak?: WeakMap>;\n private strong?: Map>;\n private data?: K;\n\n constructor(private readonly weakness: boolean) {}\n\n public lookup(...array: T): K {\n return this.lookupArray(array);\n }\n\n public lookupArray(array: T): K {\n let node: KeyTrie = this;\n array.forEach(key => node = node.getChildTrie(key));\n return node.data || (node.data = Object.create(null));\n }\n\n private getChildTrie(key: any) {\n const map = this.weakness && isObjRef(key)\n ? this.weak || (this.weak = new WeakMap>())\n : this.strong || (this.strong = new Map>());\n let child = map.get(key);\n if (!child) map.set(key, child = new KeyTrie(this.weakness));\n return child;\n }\n}\n\nfunction isObjRef(value: any) {\n switch (typeof value) {\n case \"object\":\n if (value === null) break;\n // Fall through to return true...\n case \"function\":\n return true;\n }\n return false;\n}\n","import { Cache } from \"./cache\";\nimport { Entry, AnyEntry } from \"./entry\";\nimport { parentEntrySlot } from \"./context\";\nimport { KeyTrie } from \"./key-trie\";\n\n// These helper functions are important for making optimism work with\n// asynchronous code. In order to register parent-child dependencies,\n// optimism needs to know about any currently active parent computations.\n// In ordinary synchronous code, the parent context is implicit in the\n// execution stack, but asynchronous code requires some extra guidance in\n// order to propagate context from one async task segment to the next.\nexport {\n bindContext,\n noContext,\n setTimeout,\n asyncFromGen,\n} from \"./context\";\n\n// Since the Cache uses a Map internally, any value or object reference can\n// be safely used as a key, though common types include object and string.\nexport type TCacheKey = any;\n\n// The defaultMakeCacheKey function is remarkably powerful, because it gives\n// a unique object for any shallow-identical list of arguments. If you need\n// to implement a custom makeCacheKey function, you may find it helpful to\n// delegate the final work to defaultMakeCacheKey, which is why we export it\n// here. However, you may want to avoid defaultMakeCacheKey if your runtime\n// does not support WeakMap, or you have the ability to return a string key.\n// In those cases, just write your own custom makeCacheKey functions.\nconst keyTrie = new KeyTrie(typeof WeakMap === \"function\");\nexport function defaultMakeCacheKey(...args: any[]) {\n return keyTrie.lookupArray(args);\n}\n\n// If you're paranoid about memory leaks, or you want to avoid using WeakMap\n// under the hood, but you still need the behavior of defaultMakeCacheKey,\n// import this constructor to create your own tries.\nexport { KeyTrie }\n\nexport type OptimisticWrapperFunction<\n TArgs extends any[],\n TResult,\n> = ((...args: TArgs) => TResult) & {\n // The .dirty(...) method of an optimistic function takes exactly the\n // same parameter types as the original function.\n dirty: (...args: TArgs) => void;\n};\n\nexport type OptimisticWrapOptions = {\n // The maximum number of cache entries that should be retained before the\n // cache begins evicting the oldest ones.\n max?: number;\n // If a wrapped function is \"disposable,\" then its creator does not\n // care about its return value, and it should be removed from the cache\n // immediately when it no longer has any parents that depend on it.\n disposable?: boolean;\n // The makeCacheKey function takes the same arguments that were passed to\n // the wrapper function and returns a single value that can be used as a key\n // in a Map to identify the cached result.\n makeCacheKey?: (...args: TArgs) => TCacheKey;\n // If provided, the subscribe function should either return an unsubscribe\n // function or return nothing.\n subscribe?: (...args: TArgs) => (() => any) | undefined;\n};\n\nconst caches = new Set>();\n\nexport function wrap<\n TArgs extends any[],\n TResult,\n>(\n originalFunction: (...args: TArgs) => TResult,\n options: OptimisticWrapOptions = Object.create(null),\n) {\n const cache = new Cache>(\n options.max || Math.pow(2, 16),\n entry => entry.dispose(),\n );\n\n const disposable = !! options.disposable;\n const makeCacheKey = options.makeCacheKey || defaultMakeCacheKey;\n\n function optimistic(): TResult {\n if (disposable && ! parentEntrySlot.hasValue()) {\n // If there's no current parent computation, and this wrapped\n // function is disposable (meaning we don't care about entry.value,\n // just dependency tracking), then we can short-cut everything else\n // in this function, because entry.recompute() is going to recycle\n // the entry object without recomputing anything, anyway.\n return void 0 as any;\n }\n\n const key = makeCacheKey.apply(null, arguments as any);\n if (key === void 0) {\n return originalFunction.apply(null, arguments as any);\n }\n\n const args = Array.prototype.slice.call(arguments) as TArgs;\n\n let entry = cache.get(key);\n if (entry) {\n entry.args = args;\n } else {\n entry = new Entry(originalFunction, args);\n cache.set(key, entry);\n entry.subscribe = options.subscribe;\n if (disposable) {\n entry.reportOrphan = () => cache.delete(key);\n }\n }\n\n const value = entry.recompute();\n\n // Move this entry to the front of the least-recently used queue,\n // since we just finished computing its value.\n cache.set(key, entry);\n\n caches.add(cache);\n\n // Clean up any excess entries in the cache, but only if there is no\n // active parent entry, meaning we're not in the middle of a larger\n // computation that might be flummoxed by the cleaning.\n if (! parentEntrySlot.hasValue()) {\n caches.forEach(cache => cache.clean());\n caches.clear();\n }\n\n // If options.disposable is truthy, the caller of wrap is telling us\n // they don't care about the result of entry.recompute(), so we should\n // avoid returning the value, so it won't be accidentally used.\n return disposable ? void 0 as any : value;\n }\n\n optimistic.dirty = function () {\n const key = makeCacheKey.apply(null, arguments as any);\n const child = key !== void 0 && cache.get(key);\n if (child) {\n child.setDirty();\n }\n };\n\n return optimistic as OptimisticWrapperFunction;\n}\n"],"names":[],"mappings":";;;AAOA,SAAS,cAAc,MAAK;AAE5B;IAKE,eACU,GAAc,EACf,OAAoD;QADnD,oBAAA,EAAA,cAAc;QACf,wBAAA,EAAA,wBAAoD;QADnD,QAAG,GAAH,GAAG,CAAW;QACf,YAAO,GAAP,OAAO,CAA6C;QANrD,QAAG,GAAG,IAAI,GAAG,EAAiB,CAAC;QAC/B,WAAM,GAAsB,IAAI,CAAC;QACjC,WAAM,GAAsB,IAAI,CAAC;KAKrC;IAEG,mBAAG,GAAV,UAAW,GAAM;QACf,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;KAC1B;IAEM,mBAAG,GAAV,UAAW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