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ent":["import { DocumentNode, GraphQLError } from 'graphql';\nimport { equal } from \"@wry/equality\";\n\nimport { Cache, ApolloCache } from '../cache';\nimport { DeepMerger } from \"../utilities\"\nimport { mergeIncrementalData } from '../utilities/common/incrementalResult';\nimport { WatchQueryOptions, ErrorPolicy } from './watchQueryOptions';\nimport { ObservableQuery, reobserveCacheFirst } from './ObservableQuery';\nimport { QueryListener } from './types';\nimport { FetchResult } from '../link/core';\nimport {\n ObservableSubscription,\n isNonEmptyArray,\n graphQLResultHasError,\n canUseWeakMap,\n} from '../utilities';\nimport {\n NetworkStatus,\n isNetworkRequestInFlight,\n} from './networkStatus';\nimport { ApolloError } from '../errors';\nimport { QueryManager } from './QueryManager';\n\nexport type QueryStoreValue = Pick<QueryInfo,\n | \"variables\"\n | \"networkStatus\"\n | \"networkError\"\n | \"graphQLErrors\"\n >;\n\nexport const enum CacheWriteBehavior {\n FORBID,\n OVERWRITE,\n MERGE,\n};\n\nconst destructiveMethodCounts = new (\n canUseWeakMap ? WeakMap : Map\n)<ApolloCache<any>, number>();\n\nfunction wrapDestructiveCacheMethod(\n cache: ApolloCache<any>,\n methodName: keyof ApolloCache<any>,\n) {\n const original = cache[methodName];\n if (typeof original === \"function\") {\n cache[methodName] = function () {\n destructiveMethodCounts.set(\n cache,\n // The %1e15 allows the count to wrap around to 0 safely every\n // quadrillion evictions, so there's no risk of overflow. To be\n // clear, this is more of a pedantic principle than something\n // that matters in any conceivable practical scenario.\n (destructiveMethodCounts.get(cache)! + 1) % 1e15,\n );\n return original.apply(this, arguments);\n };\n }\n}\n\nfunction cancelNotifyTimeout(info: QueryInfo) {\n if (info[\"notifyTimeout\"]) {\n clearTimeout(info[\"notifyTimeout\"]);\n info[\"notifyTimeout\"] = void 0;\n }\n}\n\n// A QueryInfo object represents a single query managed by the\n// QueryManager, which tracks all QueryInfo objects by queryId in its\n// this.queries Map. QueryInfo objects store the latest results and errors\n// for the given query, and are responsible for reporting those results to\n// the corresponding ObservableQuery, via the QueryInfo.notify method.\n// Results are reported asynchronously whenever setDiff marks the\n// QueryInfo object as dirty, though a call to the QueryManager's\n// broadcastQueries method may trigger the notification before it happens\n// automatically. This class used to be a simple interface type without\n// any field privacy or meaningful methods, which is why it still has so\n// many public fields. The effort to lock down and simplify the QueryInfo\n// interface is ongoing, and further improvements are welcome.\nexport class QueryInfo {\n listeners = new Set<QueryListener>();\n document: DocumentNode | null = null;\n lastRequestId = 1;\n subscriptions = new Set<ObservableSubscription>();\n variables?: Record<string, any>;\n networkStatus?: NetworkStatus;\n networkError?: Error | null;\n graphQLErrors?: ReadonlyArray<GraphQLError>;\n stopped = false;\n\n private cache: ApolloCache<any>;\n\n constructor(\n queryManager: QueryManager<any>,\n public readonly queryId = queryManager.generateQueryId(),\n ) {\n const cache = this.cache = queryManager.cache;\n\n // Track how often cache.evict is called, since we want eviction to\n // override the feud-stopping logic in the markResult method, by\n // causing shouldWrite to return true. Wrapping the cache.evict method\n // is a bit of a hack, but it saves us from having to make eviction\n // counting an official part of the ApolloCache API.\n if (!destructiveMethodCounts.has(cache)) {\n destructiveMethodCounts.set(cache, 0);\n wrapDestructiveCacheMethod(cache, \"evict\");\n wrapDestructiveCacheMethod(cache, \"modify\");\n wrapDestructiveCacheMethod(cache, \"reset\");\n }\n }\n\n public init(query: {\n document: DocumentNode;\n variables: Record<string, any> | undefined,\n // The initial networkStatus for this fetch, most often\n // NetworkStatus.loading, but also possibly fetchMore, poll, refetch,\n // or setVariables.\n networkStatus?: NetworkStatus,\n observableQuery?: ObservableQuery<any>;\n lastRequestId?: number;\n }): this {\n let networkStatus = query.networkStatus || NetworkStatus.loading;\n if (this.variables &&\n this.networkStatus !== NetworkStatus.loading &&\n !equal(this.variables, query.variables)) {\n networkStatus = NetworkStatus.setVariables;\n }\n\n if (!equal(query.variables, this.variables)) {\n this.lastDiff = void 0;\n }\n\n Object.assign(this, {\n document: query.document,\n variables: query.variables,\n networkError: null,\n graphQLErrors: this.graphQLErrors || [],\n networkStatus,\n });\n\n if (query.observableQuery) {\n this.setObservableQuery(query.observableQuery);\n }\n\n if (query.lastRequestId) {\n this.lastRequestId = query.lastRequestId;\n }\n\n return this;\n }\n\n private dirty: boolean = false;\n\n private notifyTimeout?: ReturnType<typeof setTimeout>;\n\n reset() {\n cancelNotifyTimeout(this);\n this.dirty = false;\n }\n\n getDiff(variables = this.variables): Cache.DiffResult<any> {\n const options = this.getDiffOptions(variables);\n\n if (this.lastDiff && equal(options, this.lastDiff.options)) {\n return this.lastDiff.diff;\n }\n\n this.updateWatch(this.variables = variables);\n\n const oq = this.observableQuery;\n if (oq && oq.options.fetchPolicy === \"no-cache\") {\n return { complete: false };\n }\n\n const diff = this.cache.diff(options);\n this.updateLastDiff(diff, options);\n return diff;\n }\n\n private lastDiff?: {\n diff: Cache.DiffResult<any>,\n options: Cache.DiffOptions,\n };\n\n private updateLastDiff(\n diff: Cache.DiffResult<any> | null,\n options?: Cache.DiffOptions,\n ) {\n this.lastDiff = diff ? {\n diff,\n options: options || this.getDiffOptions(),\n } : void 0;\n }\n\n private getDiffOptions(variables = this.variables): Cache.DiffOptions {\n return {\n query: this.document!,\n variables,\n returnPartialData: true,\n optimistic: true,\n canonizeResults: this.observableQuery?.options.canonizeResults,\n };\n }\n\n setDiff(diff: Cache.DiffResult<any> | null) {\n const oldDiff = this.lastDiff && this.lastDiff.diff;\n this.updateLastDiff(diff);\n if (!this.dirty &&\n !equal(oldDiff && oldDiff.result,\n diff && diff.result)) {\n this.dirty = true;\n if (!this.notifyTimeout) {\n this.notifyTimeout = setTimeout(() => this.notify(), 0);\n }\n }\n }\n\n public readonly observableQuery: ObservableQuery<any> | null = null;\n private oqListener?: QueryListener;\n\n setObservableQuery(oq: ObservableQuery<any> | null) {\n if (oq === this.observableQuery) return;\n\n if (this.oqListener) {\n this.listeners.delete(this.oqListener);\n }\n\n (this as any).observableQuery = oq;\n\n if (oq) {\n oq[\"queryInfo\"] = this;\n this.listeners.add(this.oqListener = () => {\n const diff = this.getDiff();\n if (diff.fromOptimisticTransaction) {\n // If this diff came from an optimistic transaction, deliver the\n // current cache data to the ObservableQuery, but don't perform a\n // reobservation, since oq.reobserveCacheFirst might make a network\n // request, and we never want to trigger network requests in the\n // middle of optimistic updates.\n oq[\"observe\"]();\n } else {\n // Otherwise, make the ObservableQuery \"reobserve\" the latest data\n // using a temporary fetch policy of \"cache-first\", so complete cache\n // results have a chance to be delivered without triggering additional\n // network requests, even when options.fetchPolicy is \"network-only\"\n // or \"cache-and-network\". All other fetch policies are preserved by\n // this method, and are handled by calling oq.reobserve(). If this\n // reobservation is spurious, isDifferentFromLastResult still has a\n // chance to catch it before delivery to ObservableQuery subscribers.\n reobserveCacheFirst(oq);\n }\n });\n } else {\n delete this.oqListener;\n }\n }\n\n notify() {\n cancelNotifyTimeout(this);\n\n if (this.shouldNotify()) {\n this.listeners.forEach(listener => listener(this));\n }\n\n this.dirty = false;\n }\n\n private shouldNotify() {\n if (!this.dirty || !this.listeners.size) {\n return false;\n }\n\n if (isNetworkRequestInFlight(this.networkStatus) &&\n this.observableQuery) {\n const { fetchPolicy } = this.observableQuery.options;\n if (fetchPolicy !== \"cache-only\" &&\n fetchPolicy !== \"cache-and-network\") {\n return false;\n }\n }\n\n return true;\n }\n\n public stop() {\n if (!this.stopped) {\n this.stopped = true;\n\n // Cancel the pending notify timeout\n this.reset();\n\n this.cancel();\n // Revert back to the no-op version of cancel inherited from\n // QueryInfo.prototype.\n this.cancel = QueryInfo.prototype.cancel;\n\n this.subscriptions.forEach(sub => sub.unsubscribe());\n\n const oq = this.observableQuery;\n if (oq) oq.stopPolling();\n }\n }\n\n // This method is a no-op by default, until/unless overridden by the\n // updateWatch method.\n private cancel() {}\n\n private lastWatch?: Cache.WatchOptions;\n\n private updateWatch(variables = this.variables) {\n const oq = this.observableQuery;\n if (oq && oq.options.fetchPolicy === \"no-cache\") {\n return;\n }\n\n const watchOptions: Cache.WatchOptions = {\n // Although this.getDiffOptions returns Cache.DiffOptions instead of\n // Cache.WatchOptions, all the overlapping options should be the same, so\n // we can reuse getDiffOptions here, for consistency.\n ...this.getDiffOptions(variables),\n watcher: this,\n callback: diff => this.setDiff(diff),\n };\n\n if (!this.lastWatch ||\n !equal(watchOptions, this.lastWatch)) {\n this.cancel();\n this.cancel = this.cache.watch(this.lastWatch = watchOptions);\n }\n }\n\n private lastWrite?: {\n result: FetchResult<any>;\n variables: WatchQueryOptions[\"variables\"];\n dmCount: number | undefined;\n };\n\n public resetLastWrite() {\n this.lastWrite = void 0;\n }\n\n private shouldWrite(\n result: FetchResult<any>,\n variables: WatchQueryOptions[\"variables\"],\n ) {\n const { lastWrite } = this;\n return !(\n lastWrite &&\n // If cache.evict has been called since the last time we wrote this\n // data into the cache, there's a chance writing this result into\n // the cache will repair what was evicted.\n lastWrite.dmCount === destructiveMethodCounts.get(this.cache) &&\n equal(variables, lastWrite.variables) &&\n equal(result.data, lastWrite.result.data)\n );\n }\n\n public markResult<T>(\n result: FetchResult<T>,\n document: DocumentNode,\n options: Pick<WatchQueryOptions,\n | \"variables\"\n | \"fetchPolicy\"\n | \"errorPolicy\">,\n cacheWriteBehavior: CacheWriteBehavior,\n ) {\n const merger = new DeepMerger();\n const graphQLErrors = isNonEmptyArray(result.errors)\n ? result.errors.slice(0)\n : [];\n\n // Cancel the pending notify timeout (if it exists) to prevent extraneous network\n // requests. To allow future notify timeouts, diff and dirty are reset as well.\n this.reset();\n\n if ('incremental' in result && isNonEmptyArray(result.incremental)) {\n const mergedData = mergeIncrementalData(this.getDiff().result, result);\n result.data = mergedData;\n\n // Detect the first chunk of a deferred query and merge it with existing\n // cache data. This ensures a `cache-first` fetch policy that returns\n // partial cache data or a `cache-and-network` fetch policy that already\n // has full data in the cache does not complain when trying to merge the\n // initial deferred server data with existing cache data.\n } else if ('hasNext' in result && result.hasNext) {\n const diff = this.getDiff();\n result.data = merger.merge(diff.result, result.data)\n }\n\n this.graphQLErrors = graphQLErrors;\n\n if (options.fetchPolicy === 'no-cache') {\n this.updateLastDiff(\n { result: result.data, complete: true },\n this.getDiffOptions(options.variables),\n );\n\n } else if (cacheWriteBehavior !== CacheWriteBehavior.FORBID) {\n if (shouldWriteResult(result, options.errorPolicy)) {\n // Using a transaction here so we have a chance to read the result\n // back from the cache before the watch callback fires as a result\n // of writeQuery, so we can store the new diff quietly and ignore\n // it when we receive it redundantly from the watch callback.\n this.cache.performTransaction(cache => {\n if (this.shouldWrite(result, options.variables)) {\n cache.writeQuery({\n query: document,\n data: result.data as T,\n variables: options.variables,\n overwrite: cacheWriteBehavior === CacheWriteBehavior.OVERWRITE,\n });\n\n this.lastWrite = {\n result,\n variables: options.variables,\n dmCount: destructiveMethodCounts.get(this.cache),\n };\n } else {\n // If result is the same as the last result we received from\n // the network (and the variables match too), avoid writing\n // result into the cache again. The wisdom of skipping this\n // cache write is far from obvious, since any cache write\n // could be the one that puts the cache back into a desired\n // state, fixing corruption or missing data. However, if we\n // always write every network result into the cache, we enable\n // feuds between queries competing to update the same data in\n // incompatible ways, which can lead to an endless cycle of\n // cache broadcasts and useless network requests. As with any\n // feud, eventually one side must step back from the brink,\n // letting the other side(s) have the last word(s). There may\n // be other points where we could break this cycle, such as\n // silencing the broadcast for cache.writeQuery (not a good\n // idea, since it just delays the feud a bit) or somehow\n // avoiding the network request that just happened (also bad,\n // because the server could return useful new data). All\n // options considered, skipping this cache write seems to be\n // the least damaging place to break the cycle, because it\n // reflects the intuition that we recently wrote this exact\n // result into the cache, so the cache *should* already/still\n // contain this data. If some other query has clobbered that\n // data in the meantime, that's too bad, but there will be no\n // winners if every query blindly reverts to its own version\n // of the data. This approach also gives the network a chance\n // to return new data, which will be written into the cache as\n // usual, notifying only those queries that are directly\n // affected by the cache updates, as usual. In the future, an\n // even more sophisticated cache could perhaps prevent or\n // mitigate the clobbering somehow, but that would make this\n // particular cache write even less important, and thus\n // skipping it would be even safer than it is today.\n if (this.lastDiff &&\n this.lastDiff.diff.complete) {\n // Reuse data from the last good (complete) diff that we\n // received, when possible.\n result.data = this.lastDiff.diff.result;\n return;\n }\n // If the previous this.diff was incomplete, fall through to\n // re-reading the latest data with cache.diff, below.\n }\n\n const diffOptions = this.getDiffOptions(options.variables);\n const diff = cache.diff<T>(diffOptions);\n\n // In case the QueryManager stops this QueryInfo before its\n // results are delivered, it's important to avoid restarting the\n // cache watch when markResult is called.\n if (!this.stopped) {\n // Any time we're about to update this.diff, we need to make\n // sure we've started watching the cache.\n this.updateWatch(options.variables);\n }\n\n // If we're allowed to write to the cache, and we can read a\n // complete result from the cache, update result.data to be the\n // result from the cache, rather than the raw network result.\n // Set without setDiff to avoid triggering a notify call, since\n // we have other ways of notifying for this result.\n this.updateLastDiff(diff, diffOptions);\n if (diff.complete) {\n result.data = diff.result;\n }\n });\n } else {\n this.lastWrite = void 0;\n }\n }\n }\n\n public markReady() {\n this.networkError = null;\n return this.networkStatus = NetworkStatus.ready;\n }\n\n public markError(error: ApolloError) {\n this.networkStatus = NetworkStatus.error;\n this.lastWrite = void 0;\n\n this.reset();\n\n if (error.graphQLErrors) {\n this.graphQLErrors = error.graphQLErrors;\n }\n\n if (error.networkError) {\n this.networkError = error.networkError;\n }\n\n return error;\n }\n}\n\nexport function shouldWriteResult<T>(\n result: FetchResult<T>,\n errorPolicy: ErrorPolicy = \"none\",\n) {\n const ignoreErrors =\n errorPolicy === \"ignore\" ||\n errorPolicy === \"all\";\n let writeWithErrors = !graphQLResultHasError(result);\n if (!writeWithErrors && ignoreErrors && result.data) {\n writeWithErrors = true;\n }\n return writeWithErrors;\n}\n"]}
\No newline at end of file