{"version":3,"file":"abstractLifeCycle.mjs","names":[],"sources":["../../src/tools/abstractLifeCycle.ts"],"sourcesContent":["import type { Subscription } from './observable'\n\n/**\n * Type helper to extract event types that have \"void\" data. This allows to call `notify` without a\n * second argument. Ex:\n *\n * ```\n * interface EventMap {\n *   foo: void\n * }\n * const LifeCycle = AbstractLifeCycle<EventMap>\n * new LifeCycle().notify('foo')\n * ```\n */\ntype EventTypesWithoutData<EventMap> = {\n  [K in keyof EventMap]: EventMap[K] extends void ? K : never\n}[keyof EventMap]\n\n// eslint-disable-next-line no-restricted-syntax\nexport class AbstractLifeCycle<EventMap> {\n  private callbacks: { [key in keyof EventMap]?: Array<(data: any) => void> } = {}\n\n  notify<EventType extends EventTypesWithoutData<EventMap>>(eventType: EventType): void\n  notify<EventType extends keyof EventMap>(eventType: EventType, data: EventMap[EventType]): void\n  notify(eventType: keyof EventMap, data?: unknown) {\n    const eventCallbacks = this.callbacks[eventType]\n    if (eventCallbacks) {\n      eventCallbacks.forEach((callback) => callback(data))\n    }\n  }\n\n  subscribe<EventType extends keyof EventMap>(\n    eventType: EventType,\n    callback: (data: EventMap[EventType]) => void\n  ): Subscription {\n    if (!this.callbacks[eventType]) {\n      this.callbacks[eventType] = []\n    }\n    this.callbacks[eventType]!.push(callback)\n    return {\n      unsubscribe: () => {\n        this.callbacks[eventType] = this.callbacks[eventType]!.filter((other) => callback !== other)\n      },\n    }\n  }\n}\n"],"mappings":";AAmBA,IAAa,oBAAb,MAAyC;;mBACuC,CAAC;;CAI/E,OAAO,WAA2B,MAAgB;EAChD,MAAM,iBAAiB,KAAK,UAAU;EACtC,IAAI,gBACF,eAAe,SAAS,aAAa,SAAS,IAAI,CAAC;CAEvD;CAEA,UACE,WACA,UACc;EACd,IAAI,CAAC,KAAK,UAAU,YAClB,KAAK,UAAU,aAAa,CAAC;EAE/B,KAAK,UAAU,UAAU,CAAE,KAAK,QAAQ;EACxC,OAAO,EACL,mBAAmB;GACjB,KAAK,UAAU,aAAa,KAAK,UAAU,UAAU,CAAE,QAAQ,UAAU,aAAa,KAAK;EAC7F,EACF;CACF;AACF"}