UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

79 lines (78 loc) 3.57 kB
import { CollectionSubscription } from './subscription.js'; import { StandardSchemaV1 } from '@standard-schema/spec'; import { ChangeMessage, SubscribeChangesOptions } from '../types.js'; import { CollectionLifecycleManager } from './lifecycle.js'; import { CollectionSyncManager } from './sync.js'; import { CollectionEventsManager } from './events.js'; import { CollectionImpl } from './index.js'; import { CollectionStateManager } from './state.js'; import { WithVirtualProps } from '../virtual-props.js'; export declare class CollectionChangesManager<TOutput extends object = Record<string, unknown>, TKey extends string | number = string | number, TSchema extends StandardSchemaV1 = StandardSchemaV1, TInput extends object = TOutput> { private lifecycle; private sync; private events; private collection; private state; activeSubscribersCount: number; changeSubscriptions: Set<CollectionSubscription>; batchedEvents: Array<ChangeMessage<TOutput, TKey>>; shouldBatchEvents: boolean; private layoutChangeListeners; /** * Monotonic revision of the collection's visible state, advanced once per * committed batch of changes — including while nothing is subscribed. * Lets consumers (the live-query observer) cheaply detect "did the data * change" without subscribing, and stays untouched by subscription * bootstrap replays, which do not go through emitEvents. */ stateRevision: number; /** * Monotonic revision advanced only for explicit layout-only publications. * Observers use it to detect reordered rows whose values did not change. */ layoutRevision: number; /** * Creates a new CollectionChangesManager instance */ constructor(); setDeps(deps: { lifecycle: CollectionLifecycleManager<TOutput, TKey, TSchema, TInput>; sync: CollectionSyncManager<TOutput, TKey, TSchema, TInput>; events: CollectionEventsManager; collection: CollectionImpl<TOutput, TKey, any, TSchema, TInput>; state: CollectionStateManager<TOutput, TKey, TSchema, TInput>; }): void; /** * Emit an empty ready event to notify subscribers that the collection is ready * This bypasses the normal empty array check in emitEvents */ emitEmptyReadyEvent(): void; /** * Enriches a change message with virtual properties ($synced, $origin, $key, $collectionId). * Uses the "add-if-missing" pattern to preserve virtual properties from upstream collections. */ private enrichChangeWithVirtualProps; /** * Emit events either immediately or batch them for later emission */ emitEvents(changes: Array<ChangeMessage<TOutput, TKey>>, forceEmit?: boolean, layoutChanged?: boolean): void; /** Subscribe to layout-only publications. Internal observer channel. */ subscribeLayoutChanges(listener: () => void): () => void; /** * Subscribe to changes in the collection */ subscribeChanges(callback: (changes: Array<ChangeMessage<WithVirtualProps<TOutput, TKey>>>) => void, options?: SubscribeChangesOptions<TOutput, TKey>): CollectionSubscription; /** * Increment the active subscribers count and start sync if needed */ private addSubscriber; /** * Decrement the active subscribers count and start GC timer if needed */ private removeSubscriber; /** * Clean up the collection by stopping sync and clearing data * This can be called manually or automatically by garbage collection */ cleanup(): void; }