UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

63 lines (62 loc) 2.77 kB
import { CollectionSubscription } from './subscription.js'; import { StandardSchemaV1 } from '@standard-schema/spec'; import { ChangeMessage, SubscribeChangesOptions } from '../types.cjs'; 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; /** * 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): 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; }