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AApB,CAAoB,CAAC;IAE/C,OAAO,EAAE,CAAC;AACZ,CAAC;AAQD,SAAS,SAAS,CAAC,KAAoB;IACrC,IAAI,KAAK,CAAC,gBAAgB,EAAE;QAC1B,KAAK,CAAC,gBAAgB,EAAE,CAAC;KAC1B;AACH,CAAC","sourcesContent":["import { dep, OptimisticDependencyFunction } from \"optimism\";\nimport { Slot } from \"@wry/context\";\nimport { InMemoryCache } from \"./inMemoryCache\";\nimport { ApolloCache } from '../../core';\n\nexport interface ReactiveVar<T> {\n (newValue?: T): T;\n onNextChange(listener: ReactiveListener<T>): () => void;\n attachCache(cache: ApolloCache<any>): this;\n forgetCache(cache: ApolloCache<any>): boolean;\n}\n\nexport type ReactiveListener<T> = (value: T) => any;\n\n// Contextual Slot that acquires its value when custom read functions are\n// called in Policies#readField.\nexport const cacheSlot = new Slot<ApolloCache<any>>();\n\nconst cacheInfoMap = new WeakMap<ApolloCache<any>, {\n vars: Set<ReactiveVar<any>>;\n dep: OptimisticDependencyFunction<ReactiveVar<any>>;\n}>();\n\nfunction getCacheInfo(cache: ApolloCache<any>) {\n let info = cacheInfoMap.get(cache)!;\n if (!info) {\n cacheInfoMap.set(cache, info = {\n vars: new Set,\n dep: dep(),\n });\n }\n return info;\n}\n\nexport function forgetCache(cache: ApolloCache<any>) {\n getCacheInfo(cache).vars.forEach(rv => rv.forgetCache(cache));\n}\n\n// Calling forgetCache(cache) serves to silence broadcasts and allows the\n// cache to be garbage collected. However, the varsByCache WeakMap\n// preserves the set of reactive variables that were previously associated\n// with this cache, which makes it possible to \"recall\" the cache at a\n// later time, by reattaching it to those variables. If the cache has been\n// garbage collected in the meantime, because it is no longer reachable,\n// you won't be able to call recallCache(cache), and the cache will\n// automatically disappear from the varsByCache WeakMap.\nexport function recallCache(cache: ApolloCache<any>) {\n getCacheInfo(cache).vars.forEach(rv => rv.attachCache(cache));\n}\n\nexport function makeVar<T>(value: T): ReactiveVar<T> {\n const caches = new Set<ApolloCache<any>>();\n const listeners = new Set<ReactiveListener<T>>();\n\n const rv: ReactiveVar<T> = function (newValue) {\n if (arguments.length > 0) {\n if (value !== newValue) {\n value = newValue!;\n caches.forEach(cache => {\n // Invalidate any fields with custom read functions that\n // consumed this variable, so query results involving those\n // fields will be recomputed the next time we read them.\n getCacheInfo(cache).dep.dirty(rv);\n // Broadcast changes to any caches that have previously read\n // from this variable.\n broadcast(cache);\n });\n // Finally, notify any listeners added via rv.onNextChange.\n const oldListeners = Array.from(listeners);\n listeners.clear();\n oldListeners.forEach(listener => listener(value));\n }\n } else {\n // When reading from the variable, obtain the current cache from\n // context via cacheSlot. This isn't entirely foolproof, but it's\n // the same system that powers varDep.\n const cache = cacheSlot.getValue();\n if (cache) {\n attach(cache);\n getCacheInfo(cache).dep(rv);\n }\n }\n\n return value;\n };\n\n rv.onNextChange = listener => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n };\n\n const attach = rv.attachCache = cache => {\n caches.add(cache);\n getCacheInfo(cache).vars.add(rv);\n return rv;\n };\n\n rv.forgetCache = cache => caches.delete(cache);\n\n return rv;\n}\n\ntype Broadcastable = ApolloCache<any> & {\n // This method is protected in InMemoryCache, which we are ignoring, but\n // we still want some semblance of type safety when we call it.\n broadcastWatches?: InMemoryCache[\"broadcastWatches\"];\n};\n\nfunction broadcast(cache: Broadcastable) {\n if (cache.broadcastWatches) {\n cache.broadcastWatches();\n }\n}\n"]}
\No newline at end of file