@tanstack/db
Version:
A reactive client store for building super fast apps on sync
1 lines • 28 kB
Source Map (JSON)
{"version":3,"file":"live-query-observer.cjs","sources":["../../src/live-query-observer.ts"],"sourcesContent":["import { LiveQueryObserverDisposedError } from './errors.js'\nimport {\n getLiveQueryStatusFlags,\n isSingleResultCollection,\n} from './live-query-adapter.js'\nimport type { Collection } from './collection/index.js'\nimport type { ChangeMessage, CollectionStatus } from './types.js'\n\n/**\n * The canonical, adapter-agnostic view of a live query at a point in time.\n *\n * `getSnapshot()` returns a stable object identity that only changes when the\n * query changes, so `useSyncExternalStore`-style consumers can compare by\n * reference. Each snapshot owns a captured view of `state`/`data`, so reading\n * an older snapshot cannot expose rows from a later revision.\n */\nexport interface LiveQuerySnapshot<\n T extends object,\n TKey extends string | number,\n> {\n /** Keyed results, or `undefined` for a disabled query. */\n state: ReadonlyMap<TKey, T> | undefined\n /** Ordered results (single row for `findOne`), or `undefined` when disabled. */\n data: T | ReadonlyArray<T> | undefined\n /** The underlying collection, or `undefined` when disabled. */\n collection: Collection<T, TKey, any> | undefined\n /**\n * Monotonic counter bumped whenever the visible layout (the ordered key\n * sequence) changes — membership, ordering, or an order-only move. Lets\n * consumers detect a reorder that changed no row value (which `data`/`state`\n * identity alone can't express once row values are structurally shared).\n *\n * It is NOT in lockstep with snapshot identity: a value-only update produces a\n * new snapshot while `layoutRevision` stays put. A `layoutRevision` change\n * always accompanies a new snapshot, but not vice versa.\n */\n layoutRevision: number\n status: CollectionStatus | `disabled`\n isLoading: boolean\n isReady: boolean\n isIdle: boolean\n isError: boolean\n isCleanedUp: boolean\n isEnabled: boolean\n}\n\n/**\n * Listener payload: changes, `[]` for an internal layout-only publication, or\n * `undefined` for a synthetic status/ready notification.\n */\nexport type LiveQueryObserverListener<\n T extends object,\n TKey extends string | number,\n> = (changes: Array<ChangeMessage<T, TKey>> | undefined) => void\n\n/**\n * Wraps a resolved live-query `Collection` (or `null` for a disabled query) with\n * the shared lifecycle every framework adapter needs: start sync on first\n * subscribe, subscribe to changes and status transitions, expose a stable\n * snapshot for wholesale consumers, and deliver the raw change set for\n * granular consumers.\n *\n * Input resolution (query fn / config / collection / disabled) stays in the\n * adapter — it is framework-reactive. The observer owns everything after the\n * input is resolved to a concrete collection.\n *\n * @internal Unstable contract for TanStack DB's official framework adapters —\n * not a public extension point yet; may change in any release.\n */\nexport interface LiveQueryObserver<\n T extends object,\n TKey extends string | number,\n> {\n /** Stable per-revision snapshot for wholesale materialization. */\n getSnapshot: () => LiveQuerySnapshot<T, TKey>\n /**\n * Subscribe to changes. The listener receives the change set (or `undefined`\n * for the synthetic notify a ready collection emits on attach). Granular\n * adapters apply the changes; wholesale adapters can ignore them and re-read\n * `getSnapshot()`. Returns an unsubscribe function.\n */\n subscribe: (listener: LiveQueryObserverListener<T, TKey>) => () => void\n /** Resolve once the collection has loaded its first data. */\n preload: () => Promise<void>\n /** Idempotent teardown. */\n dispose: () => void\n}\n\n/**\n * One logical subscription. Records — not raw callbacks — identify\n * subscriptions, so the same listener function can be subscribed twice and\n * each subscription tears down independently.\n */\ninterface SubscriptionRecord<T extends object, TKey extends string | number> {\n listener: LiveQueryObserverListener<T, TKey>\n active: boolean\n}\n\ninterface Publication<T extends object, TKey extends string | number> {\n changes: Array<ChangeMessage<T, TKey>> | undefined\n targets: Array<SubscriptionRecord<T, TKey>>\n entries?: Array<[TKey, T]>\n status: CollectionStatus\n collectionRevision?: number\n collectionLayoutRevision?: number\n layoutChanged: boolean\n}\n\nconst DISABLED_SNAPSHOT: LiveQuerySnapshot<any, any> = {\n state: undefined,\n data: undefined,\n collection: undefined,\n layoutRevision: 0,\n status: `disabled`,\n isLoading: false,\n isReady: true,\n isIdle: false,\n isError: false,\n isCleanedUp: false,\n isEnabled: false,\n}\n\nclass LiveQueryObserverImpl<\n T extends object,\n TKey extends string | number,\n> implements LiveQueryObserver<T, TKey> {\n private readonly collection: Collection<T, TKey, any> | null\n private readonly wholesale: boolean\n private visibleStatus: CollectionStatus | undefined\n private cachedEntries: Array<[TKey, T]> | undefined\n private cachedCollectionRevision: number | undefined\n private cachedCollectionLayoutRevision: number | undefined\n private snapshotDirty = true\n private cachedSnapshot: LiveQuerySnapshot<T, TKey> = DISABLED_SNAPSHOT\n private layoutRevision = 0\n private lastLayoutKeys: Array<TKey> | undefined\n private deliveredLayoutRevision: number | undefined\n private readonly subscriptions = new Set<SubscriptionRecord<T, TKey>>()\n // Publications are dispatched FIFO: an emit that happens while another\n // publication is being delivered (a listener mutating the collection\n // synchronously) is queued, never delivered reentrantly.\n private readonly publicationQueue: Array<Publication<T, TKey>> = []\n private dispatching = false\n private blockDelivery = false\n private attached = false\n private collectionUnsub: (() => void) | null = null\n private disposed = false\n\n // Construction is side-effect-free: sync activation belongs to the first\n // subscription (attach), so building an observer — e.g. in a React render\n // that may be abandoned — cannot activate resources on its own.\n constructor(collection: Collection<T, TKey, any> | null, wholesale: boolean) {\n this.collection = collection\n this.wholesale = wholesale\n }\n\n getSnapshot(): LiveQuerySnapshot<T, TKey> {\n const collection = this.collection\n if (!collection) return DISABLED_SNAPSHOT\n\n if (!this.attached) this.refreshDetachedState(collection)\n\n if (this.snapshotDirty) {\n const entries =\n this.cachedEntries ?? this.captureEntries(collection).entries\n const state = new Map(entries)\n const data = entries.map(([, value]) => value)\n const singleResult = isSingleResultCollection(collection)\n const status = this.visibleStatus ?? collection.status\n\n // Bump the layout revision when the ordered key sequence changes\n // (membership, ordering, or an order-only move). Compare the key sequence\n // directly rather than via a serialized signature: a joined-with-separator\n // signature can collide when a key value equals the concatenation of\n // neighboring keys around the separator. Comparing keys also avoids\n // materializing a large string on every rebuild; a new key array is only\n // allocated when the layout actually moved.\n const prevKeys = this.lastLayoutKeys\n let layoutChanged =\n prevKeys === undefined || prevKeys.length !== entries.length\n if (!layoutChanged) {\n for (let i = 0; i < entries.length; i++) {\n if (prevKeys![i] !== entries[i]![0]) {\n layoutChanged = true\n break\n }\n }\n }\n if (layoutChanged) {\n this.lastLayoutKeys = entries.map(([key]) => key)\n this.layoutRevision++\n }\n\n this.cachedSnapshot = {\n state,\n data: singleResult ? data[0] : data,\n collection,\n layoutRevision: this.layoutRevision,\n status,\n ...getLiveQueryStatusFlags(status),\n isEnabled: true,\n }\n this.snapshotDirty = false\n }\n return this.cachedSnapshot\n }\n\n private getCollectionRevision(\n collection: Collection<T, TKey, any>,\n ): number | undefined {\n const revision = (collection as { _stateRevision?: unknown })._stateRevision\n return typeof revision === `number` ? revision : undefined\n }\n\n private getCollectionLayoutRevision(\n collection: Collection<T, TKey, any>,\n ): number | undefined {\n const revision = (collection as { _layoutRevision?: unknown })\n ._layoutRevision\n return typeof revision === `number` ? revision : undefined\n }\n\n private readEntries(collection: Collection<T, TKey, any>): {\n entries: Array<[TKey, T]>\n revision?: number\n } {\n const entries = Array.from(collection.entries()) as Array<[TKey, T]>\n const revision = this.getCollectionRevision(collection)\n return { entries, revision }\n }\n\n private captureEntries(collection: Collection<T, TKey, any>): {\n entries: Array<[TKey, T]>\n revision?: number\n } {\n const { entries, revision } = this.readEntries(collection)\n this.updateCachedEntries(entries, revision)\n return { entries, revision }\n }\n\n private updateCachedEntries(\n entries: Array<[TKey, T]>,\n revision: number | undefined,\n ): void {\n const changed =\n revision !== undefined\n ? this.cachedEntries === undefined ||\n revision !== this.cachedCollectionRevision\n : !this.entriesEqual(this.cachedEntries, entries)\n\n this.cachedEntries = entries\n this.cachedCollectionRevision = revision\n if (changed) this.snapshotDirty = true\n }\n\n private entriesEqual(\n left: Array<[TKey, T]> | undefined,\n right: Array<[TKey, T]>,\n ): boolean {\n if (!left || left.length !== right.length) return false\n return left.every(\n ([key, value], index) =>\n right[index]![0] === key && right[index]![1] === value,\n )\n }\n\n /**\n * While detached there is no delivered-publication clock, so fall back to\n * the collection revision. Compatible cross-copy collections that predate\n * `_stateRevision` are compared structurally instead.\n */\n private refreshDetachedState(collection: Collection<T, TKey, any>): void {\n const status = collection.status\n const revision = this.getCollectionRevision(collection)\n const layoutRevision = this.getCollectionLayoutRevision(collection)\n\n if (revision !== undefined) {\n if (\n this.cachedEntries === undefined ||\n revision !== this.cachedCollectionRevision ||\n layoutRevision !== this.cachedCollectionLayoutRevision\n ) {\n this.captureEntries(collection)\n this.cachedCollectionLayoutRevision = layoutRevision\n this.snapshotDirty = true\n }\n } else {\n const entries = Array.from(collection.entries()) as Array<[TKey, T]>\n this.updateCachedEntries(entries, undefined)\n }\n\n if (this.visibleStatus !== status) {\n this.visibleStatus = status\n this.snapshotDirty = true\n }\n }\n\n subscribe(listener: LiveQueryObserverListener<T, TKey>): () => void {\n if (this.disposed) throw new LiveQueryObserverDisposedError()\n\n const record: SubscriptionRecord<T, TKey> = { listener, active: true }\n this.subscriptions.add(record)\n if (this.subscriptions.size === 1) {\n this.attach()\n } else {\n // The initial-state replay only happens on attach, so a granular\n // subscriber that arrives while already attached is seeded with the\n // current rows — delivered to this subscription alone, without advancing\n // the observer's revision (the collection state did not change).\n // Wholesale consumers read getSnapshot() instead and need no seed.\n if (!this.wholesale) this.seed(record)\n }\n\n return () => {\n if (!record.active) return\n record.active = false\n this.subscriptions.delete(record)\n if (this.subscriptions.size === 0) this.detach()\n }\n }\n\n /** Deliver the collection's current rows to one late subscription as inserts. */\n private seed(record: SubscriptionRecord<T, TKey>): void {\n const collection = this.collection\n if (!collection) return\n\n const seedChanges: Array<ChangeMessage<T, TKey>> = []\n for (const [key, value] of collection.entries() as IterableIterator<\n [TKey, T]\n >) {\n seedChanges.push({ type: `insert`, key, value })\n }\n if (seedChanges.length === 0) return\n\n this.emit(seedChanges, [record])\n }\n\n private attach(): void {\n const collection = this.collection\n if (!collection || this.disposed) return\n this.refreshDetachedState(collection)\n this.attached = true\n this.visibleStatus ??= collection.status\n this.deliveredLayoutRevision = this.getCollectionLayoutRevision(collection)\n this.blockDelivery = this.wholesale\n\n // Sync activation happens inside subscribeChanges (addSubscriber starts\n // an idle/cleaned-up collection) — the same startSync path the old\n // constructor-time startSyncImmediate() took, but now owned by the first\n // committed subscription and observed by the status listener below.\n\n // Granular consumers subscribe with initial state so they receive the\n // current rows as inserts followed by deltas through one consistent\n // channel (the collection's per-subscriber change stream requires this to\n // align deltas). Wholesale consumers subscribe WITHOUT initial state —\n // preserving their pre-observer loading policy: no snapshot request means\n // no unfiltered loadSubset({ where: undefined }) against on-demand\n // collections. The explicit `false` marks all state as seen so deletes\n // still flow through as notifies.\n const notify = (\n changes: Array<ChangeMessage<T, TKey>> | undefined,\n status: CollectionStatus = collection.status,\n explicitLayoutChange = false,\n ) => {\n if (this.disposed || this.subscriptions.size === 0) return\n const layoutRevision = this.getCollectionLayoutRevision(collection)\n let layoutChanged = explicitLayoutChange\n if (\n !explicitLayoutChange &&\n changes !== undefined &&\n changes.length === 0\n ) {\n // Empty ready events predate the explicit layout signal and share its\n // empty-array payload. Only forward an empty batch when the collection\n // confirms that a new layout-only publication occurred.\n if (\n layoutRevision === undefined ||\n layoutRevision === this.deliveredLayoutRevision\n ) {\n return\n }\n layoutChanged = true\n }\n if (changes !== undefined && layoutRevision !== undefined) {\n this.deliveredLayoutRevision = layoutRevision\n }\n const captured =\n changes !== undefined\n ? this.readEntries(collection)\n : status === `cleaned-up`\n ? this.readEntries(collection)\n : undefined\n this.emit(\n changes,\n undefined,\n captured?.entries,\n status,\n captured?.revision,\n layoutRevision,\n layoutChanged,\n )\n }\n\n // Status transitions that carry no change events (loading→ready with no\n // rows, error, cleaned-up) are part of the canonical publication path:\n // any status change publishes a synthetic notify so consumers re-read the\n // snapshot. Unlike onFirstReady, `on` returns a real unsubscribe, so a\n // detached attachment leaves nothing behind.\n const statusUnsub = collection.on(`status:change`, ({ status }) =>\n notify(undefined, status),\n )\n const subscribeLayoutChanges = (\n collection as Collection<T, TKey, any> & {\n _subscribeLayoutChanges?: (listener: () => void) => () => void\n }\n )._subscribeLayoutChanges\n const layoutUnsub =\n typeof subscribeLayoutChanges === `function`\n ? subscribeLayoutChanges.call(collection, () =>\n notify([], collection.status, true),\n )\n : () => {}\n\n // `subscribeChanges` delivers the initial state synchronously, so a\n // listener can dispose the observer while the collection subscription is\n // still being created. Register the release hook up front; if detach()\n // ran during that replay (collectionUnsub no longer points at our hook),\n // undo the subscription as soon as the call returns.\n let subscription: { unsubscribe: () => void } | null = null\n const release = () => {\n statusUnsub()\n layoutUnsub()\n subscription?.unsubscribe()\n }\n this.collectionUnsub = release\n subscription = collection.subscribeChanges(\n (changes) => notify(changes as Array<ChangeMessage<T, TKey>>),\n { includeInitialState: !this.wholesale },\n )\n this.blockDelivery = false\n if (this.collectionUnsub !== release) {\n subscription.unsubscribe()\n return\n }\n if (this.wholesale) {\n // Publications raised while subscribeChanges starts sync are part of the\n // subscribe handshake. Apply their final snapshot state now, but suppress\n // listener delivery: useSyncExternalStore performs its consistency read\n // immediately after subscribe returns.\n this.flushPublications(false)\n const { entries, revision } = this.readEntries(collection)\n this.updateCachedEntries(entries, revision)\n }\n }\n\n private detach(): void {\n this.collectionUnsub?.()\n this.collectionUnsub = null\n this.attached = false\n this.blockDelivery = false\n this.publicationQueue.length = 0\n }\n\n private emit(\n changes: Array<ChangeMessage<T, TKey>> | undefined,\n targets = Array.from(this.subscriptions),\n entries?: Array<[TKey, T]>,\n status = this.collection?.status ?? `cleaned-up`,\n collectionRevision?: number,\n collectionLayoutRevision?: number,\n layoutChanged = false,\n ): void {\n this.publicationQueue.push({\n changes,\n targets,\n entries,\n status,\n collectionRevision,\n collectionLayoutRevision,\n layoutChanged,\n })\n if (this.dispatching || this.blockDelivery) return\n\n this.flushPublications()\n }\n\n private flushPublications(deliver = true): void {\n if (this.dispatching) return\n\n this.dispatching = true\n try {\n // A dispose() during dispatch empties the queue, ending this loop.\n while (this.publicationQueue.length > 0) {\n const publication = this.publicationQueue.shift()!\n if (publication.entries) {\n this.updateCachedEntries(\n publication.entries,\n publication.collectionRevision,\n )\n }\n if (publication.collectionLayoutRevision !== undefined) {\n this.cachedCollectionLayoutRevision =\n publication.collectionLayoutRevision\n }\n if (publication.layoutChanged) {\n this.snapshotDirty = true\n }\n if (this.visibleStatus !== publication.status) {\n this.visibleStatus = publication.status\n this.snapshotDirty = true\n }\n // Targets are captured when the publication is queued: a subscription\n // removed mid-delivery still receives the in-flight publication, and\n // one added later does not. Late-subscriber seeds use the same queue.\n if (deliver) {\n for (const subRecord of publication.targets) {\n if (this.disposed) return\n subRecord.listener(publication.changes)\n }\n }\n }\n } finally {\n this.dispatching = false\n }\n }\n\n async preload(): Promise<void> {\n await this.collection?.preload()\n }\n\n dispose(): void {\n if (this.disposed) return\n this.disposed = true\n this.detach()\n for (const subRecord of this.subscriptions) subRecord.active = false\n this.subscriptions.clear()\n this.publicationQueue.length = 0\n }\n}\n\nexport interface CreateLiveQueryObserverOptions {\n /**\n * How subscribers consume the observer:\n *\n * - `granular` (default): subscribers apply the delivered `ChangeMessage[]`\n * deltas to their own keyed state (Vue/Svelte/Solid). The observer\n * subscribes with initial state and seeds late subscribers, so every\n * subscriber converges from deltas alone.\n * - `wholesale`: subscribers treat notifications as a wake-up and re-read\n * `getSnapshot()` (React/Angular). The observer subscribes WITHOUT initial\n * state, preserving those adapters' loading policy — no snapshot request,\n * so no unfiltered `loadSubset` against on-demand collections. Nothing is\n * delivered synchronously during `subscribe`, which keeps\n * `useSyncExternalStore`-style consumers safe by construction.\n */\n mode?: `granular` | `wholesale`\n}\n\n/**\n * Create a {@link LiveQueryObserver} for a resolved live-query collection, or a\n * disabled observer when `collection` is `null`/`undefined`.\n *\n * @internal This is an unstable contract shared by TanStack DB's official\n * framework adapters. It is exported so the adapter packages can use it, but\n * it is not a public extension point yet: its API may change in any release\n * without a semver major.\n */\nexport function createLiveQueryObserver<\n T extends object,\n TKey extends string | number,\n>(\n collection: Collection<T, TKey, any> | null | undefined,\n options: CreateLiveQueryObserverOptions = {},\n): LiveQueryObserver<T, TKey> {\n return new LiveQueryObserverImpl<T, TKey>(\n collection ?? null,\n options.mode === `wholesale`,\n )\n}\n"],"names":["isSingleResultCollection","getLiveQueryStatusFlags","LiveQueryObserverDisposedError"],"mappings":";;;;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