{"version":3,"file":"events.cjs","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 is truncated (all data cleared)\n */\nexport interface CollectionTruncateEvent {\n  type: `truncate`\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  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  truncate: CollectionTruncateEvent\n  'index:added': CollectionIndexAddedEvent\n  'index:removed': CollectionIndexRemovedEvent\n} & {\n  [K in CollectionStatus as `status:${K}`]: CollectionStatusEvent<K>\n}\n\nexport type CollectionEvent =\n  | AllCollectionEvents[keyof AllCollectionEvents]\n  | CollectionStatusChangeEvent\n  | CollectionSubscribersChangeEvent\n  | CollectionLoadingSubsetChangeEvent\n  | CollectionTruncateEvent\n  | CollectionIndexAddedEvent\n  | CollectionIndexRemovedEvent\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  ) {\n    this.emit(`status:change`, {\n      type: `status:change`,\n      collection: this.collection,\n      previousStatus,\n      status,\n    })\n\n    // Emit specific status event using type assertion\n    const eventKey: `status:${T}` = `status:${status}`\n    this.emit(eventKey, {\n      type: eventKey,\n      collection: this.collection,\n      previousStatus,\n      status,\n    } as AllCollectionEvents[`status:${T}`])\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"],"names":["EventEmitter"],"mappings":";;;AA0HO,MAAM,gCAAgCA,aAAAA,aAAkC;AAAA,EAG7E,cAAc;AACZ,UAAA;AAAA,EACF;AAAA,EAEA,QAAQ,MAA2D;AACjE,SAAK,aAAa,KAAK;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KACE,OACA,cACM;AACN,SAAK,UAAU,OAAO,YAAY;AAAA,EACpC;AAAA,EAEA,iBACE,QACA,gBACA;AACA,SAAK,KAAK,iBAAiB;AAAA,MACzB,MAAM;AAAA,MACN,YAAY,KAAK;AAAA,MACjB;AAAA,MACA;AAAA,IAAA,CACD;AAGD,UAAM,WAA0B,UAAU,MAAM;AAChD,SAAK,KAAK,UAAU;AAAA,MAClB,MAAM;AAAA,MACN,YAAY,KAAK;AAAA,MACjB;AAAA,MACA;AAAA,IAAA,CACqC;AAAA,EACzC;AAAA,EAEA,sBACE,iBACA,yBACA;AACA,SAAK,KAAK,sBAAsB;AAAA,MAC9B,MAAM;AAAA,MACN,YAAY,KAAK;AAAA,MACjB;AAAA,MACA;AAAA,IAAA,CACD;AAAA,EACH;AAAA,EAEA,eAAe,OAAgC;AAC7C,SAAK,KAAK,eAAe;AAAA,MACvB,MAAM;AAAA,MACN,YAAY,KAAK;AAAA,MACjB;AAAA,IAAA,CACD;AAAA,EACH;AAAA,EAEA,iBAAiB,OAAgC;AAC/C,SAAK,KAAK,iBAAiB;AAAA,MACzB,MAAM;AAAA,MACN,YAAY,KAAK;AAAA,MACjB;AAAA,IAAA,CACD;AAAA,EACH;AAAA,EAEA,UAAU;AACR,SAAK,eAAA;AAAA,EACP;AACF;;"}