@tanstack/db
Version:
A reactive client store for building super fast apps on sync
1 lines • 3.39 kB
Source Map (JSON)
{"version":3,"file":"subset-dedupe.cjs","names":["getLoadSubsetDemandKey","inflight","options"],"sources":["../../../src/query/subset-dedupe.ts"],"sourcesContent":["import { getLoadSubsetDemandKey } from './ir-stable-identity.js'\nimport type { LoadSubsetFn, LoadSubsetOptions } from '../types.js'\n\n/**\n * Deduplicates exact canonical demands without inferring broader coverage.\n * Requests follow the immutable LoadSubsetOptions contract; no copies are made.\n */\nexport class DeduplicatedLoadSubset {\n private readonly completed = new Set<string | undefined>()\n private readonly inflight = new Map<string | undefined, Promise<void>>()\n private generation = 0\n\n constructor(\n private readonly options: {\n loadSubset: LoadSubsetFn\n onDeduplicate?: (options: LoadSubsetOptions) => void\n },\n ) {}\n\n loadSubset = (options: LoadSubsetOptions): true | Promise<void> => {\n const key = getLoadSubsetDemandKey(options)\n if (!options.refetch && this.completed.has(key)) {\n this.options.onDeduplicate?.(options)\n return true\n }\n\n // Requests with independent cancellation own independent transports.\n // Unabortable requests can share without an ownership protocol.\n const existing =\n options.signal || options.refetch ? undefined : this.inflight.get(key)\n if (existing) {\n // Observer failures must not reject a detached promise after success.\n void existing\n .then(() => this.options.onDeduplicate?.(options))\n .catch(() => {})\n return existing\n }\n\n const generation = this.generation\n const result = this.options.loadSubset(options)\n\n if (result === true) {\n if (generation === this.generation && !options.signal?.aborted) {\n this.completed.add(key)\n }\n return true\n }\n\n const promise = result\n .then((value) => {\n if (generation === this.generation && !options.signal?.aborted) {\n this.completed.add(key)\n }\n return value\n })\n .finally(() => {\n if (this.inflight.get(key) === promise) this.inflight.delete(key)\n })\n if (!options.signal && generation === this.generation) {\n this.inflight.set(key, promise)\n }\n return promise\n }\n\n reset(): void {\n this.completed.clear()\n this.inflight.clear()\n this.generation++\n }\n}\n"],"mappings":";AAOA,IAAa,yBAAb,MAAoC;CAKlC,YACE,SAIA;EAJiB,KAAA,UAAA;EALU,KAAA,4BAAA,IAAI,IAAwB;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