UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

1 lines • 7.11 kB
{"version":3,"file":"events.cjs","names":["EventEmitter"],"sources":["../../../src/collection/events.ts"],"sourcesContent":["import { EventEmitter } from '../event-emitter.js'\nimport type { Collection } from './index.js'\nimport type { CollectionStatus } from '../types.js'\nimport type { BasicExpression } from '../query/ir.js'\n\n/**\n * Event emitted when the collection status changes\n */\nexport interface CollectionStatusChangeEvent {\n type: `status:change`\n collection: Collection\n previousStatus: CollectionStatus\n status: CollectionStatus\n}\n\n/**\n * Event emitted when the collection status changes to a specific status\n */\nexport interface CollectionStatusEvent<T extends CollectionStatus> {\n type: `status:${T}`\n collection: Collection\n previousStatus: CollectionStatus\n status: T\n}\n\n/**\n * Event emitted when the number of subscribers to the collection changes\n */\nexport interface CollectionSubscribersChangeEvent {\n type: `subscribers:change`\n collection: Collection\n previousSubscriberCount: number\n subscriberCount: number\n}\n\n/**\n * Event emitted when the collection's loading more state changes\n */\nexport interface CollectionLoadingSubsetChangeEvent {\n type: `loadingSubset:change`\n collection: Collection<any, any, any, any, any>\n isLoadingSubset: boolean\n previousIsLoadingSubset: boolean\n loadingSubsetTransition: `start` | `end`\n}\n\n/**\n * Event emitted when the Collection's health tree may have changed: its own\n * status or problems, or for a live query, those of a Collection it depends\n * on. Read `collection.health` for the current tree.\n */\nexport interface CollectionHealthChangeEvent {\n type: `health:change`\n collection: Collection<any, any, any, any, any>\n}\n\n/**\n * Event emitted when the collection is truncated (all data cleared)\n */\nexport interface CollectionTruncateEvent {\n type: `truncate`\n collection: Collection<any, any, any, any, any>\n}\n\n/**\n * Event emitted when the sync adapter reports that rows it serves for loaded\n * subsets may have shifted between served pages. Ordered live queries then\n * re-establish their windows with a bounded repair.\n */\nexport interface CollectionLoadedSubsetsInvalidatedEvent {\n type: `loadedSubsets:invalidated`\n collection: Collection<any, any, any, any, any>\n}\n\nexport type CollectionIndexSerializableValue =\n | string\n | number\n | boolean\n | null\n | Array<CollectionIndexSerializableValue>\n | {\n [key: string]: CollectionIndexSerializableValue\n }\n\nexport interface CollectionIndexResolverMetadata {\n kind: `constructor` | `async`\n /** Stable for built-in indexes; diagnostic and build-dependent for custom constructors. */\n name?: string\n}\n\nexport interface CollectionIndexMetadata {\n /**\n * Version for the signature serialization contract.\n */\n signatureVersion: 1\n /**\n * Stable signature derived from expression + serializable options.\n * Non-serializable option fields are intentionally omitted.\n */\n signature: string\n indexId: number\n name?: string\n expression: BasicExpression\n resolver: CollectionIndexResolverMetadata\n options?: CollectionIndexSerializableValue\n}\n\nexport interface CollectionIndexAddedEvent {\n type: `index:added`\n collection: Collection<any, any, any, any, any>\n index: CollectionIndexMetadata\n}\n\nexport interface CollectionIndexRemovedEvent {\n type: `index:removed`\n collection: Collection<any, any, any, any, any>\n index: CollectionIndexMetadata\n}\n\nexport type AllCollectionEvents = {\n 'status:change': CollectionStatusChangeEvent\n 'subscribers:change': CollectionSubscribersChangeEvent\n 'loadingSubset:change': CollectionLoadingSubsetChangeEvent\n 'health:change': CollectionHealthChangeEvent\n truncate: CollectionTruncateEvent\n 'loadedSubsets:invalidated': CollectionLoadedSubsetsInvalidatedEvent\n 'index:added': CollectionIndexAddedEvent\n 'index:removed': CollectionIndexRemovedEvent\n} & {\n [K in CollectionStatus as `status:${K}`]: CollectionStatusEvent<K>\n}\n\nexport type CollectionEventHandler<T extends keyof AllCollectionEvents> = (\n event: AllCollectionEvents[T],\n) => void\n\nexport class CollectionEventsManager extends EventEmitter<AllCollectionEvents> {\n private collection!: Collection<any, any, any, any, any>\n\n constructor() {\n super()\n }\n\n setDeps(deps: { collection: Collection<any, any, any, any, any> }) {\n this.collection = deps.collection\n }\n\n /**\n * Emit an event to all listeners\n * Public API for emitting collection events\n */\n emit<T extends keyof AllCollectionEvents>(\n event: T,\n eventPayload: AllCollectionEvents[T],\n ): void {\n this.emitInner(event, eventPayload)\n }\n\n emitStatusChange<T extends CollectionStatus>(\n status: T,\n previousStatus: CollectionStatus,\n isCurrent: () => boolean,\n ) {\n this.emitInnerWhile(\n `status:change`,\n {\n type: `status:change`,\n collection: this.collection,\n previousStatus,\n status,\n },\n isCurrent,\n )\n if (!isCurrent()) return\n\n // Emit specific status event using type assertion\n const eventKey: `status:${T}` = `status:${status}`\n this.emitInnerWhile(\n eventKey,\n {\n type: eventKey,\n collection: this.collection,\n previousStatus,\n status,\n } as AllCollectionEvents[`status:${T}`],\n isCurrent,\n )\n }\n\n emitHealthChange() {\n this.emit(`health:change`, {\n type: `health:change`,\n collection: this.collection,\n })\n }\n\n emitSubscribersChange(\n subscriberCount: number,\n previousSubscriberCount: number,\n ) {\n this.emit(`subscribers:change`, {\n type: `subscribers:change`,\n collection: this.collection,\n previousSubscriberCount,\n subscriberCount,\n })\n }\n\n emitIndexAdded(index: CollectionIndexMetadata) {\n this.emit(`index:added`, {\n type: `index:added`,\n collection: this.collection,\n index,\n })\n }\n\n emitIndexRemoved(index: CollectionIndexMetadata) {\n this.emit(`index:removed`, {\n type: `index:removed`,\n collection: this.collection,\n index,\n })\n }\n\n cleanup() {\n this.clearListeners()\n }\n}\n"],"mappings":";AAwIA,IAAa,0BAAb,cAA6CA,sBAAAA,aAAkC;CAG7E,cAAc;EACZ,MAAM;CACR;CAEA,QAAQ,MAA2D;EACjE,KAAK,aAAa,KAAK;CACzB;;;;;CAMA,KACE,OACA,cACM;EACN,KAAK,UAAU,OAAO,YAAY;CACpC;CAEA,iBACE,QACA,gBACA,WACA;EACA,KAAK,eACH,iBACA;GACE,MAAM;GACN,YAAY,KAAK;GACjB;GACA;EACF,GACA,SACF;EACA,IAAI,CAAC,UAAU,GAAG;EAGlB,MAAM,WAA0B,UAAU;EAC1C,KAAK,eACH,UACA;GACE,MAAM;GACN,YAAY,KAAK;GACjB;GACA;EACF,GACA,SACF;CACF;CAEA,mBAAmB;EACjB,KAAK,KAAK,iBAAiB;GACzB,MAAM;GACN,YAAY,KAAK;EACnB,CAAC;CACH;CAEA,sBACE,iBACA,yBACA;EACA,KAAK,KAAK,sBAAsB;GAC9B,MAAM;GACN,YAAY,KAAK;GACjB;GACA;EACF,CAAC;CACH;CAEA,eAAe,OAAgC;EAC7C,KAAK,KAAK,eAAe;GACvB,MAAM;GACN,YAAY,KAAK;GACjB;EACF,CAAC;CACH;CAEA,iBAAiB,OAAgC;EAC/C,KAAK,KAAK,iBAAiB;GACzB,MAAM;GACN,YAAY,KAAK;GACjB;EACF,CAAC;CACH;CAEA,UAAU;EACR,KAAK,eAAe;CACtB;AACF"}