{"version":3,"file":"ngxtension-on-event.mjs","sources":["../../../../libs/ngxtension/on-event/src/on-event.ts","../../../../libs/ngxtension/on-event/src/ngxtension-on-event.ts"],"sourcesContent":["import { DestroyRef, Injector, Signal, signal } from '@angular/core';\nimport { assertInjector } from 'ngxtension/assert-injector';\n\nexport type OnEventOptions = {\n\tonce?: boolean;\n\tcapture?: boolean;\n\tpassive?: boolean;\n} & (\n\t| {\n\t\t\tdestroyRef?: DestroyRef;\n\t\t\tinjector?: never;\n\t\t\tmanualCleanup?: false | null | undefined;\n\t  }\n\t| {\n\t\t\tdestroyRef?: never;\n\t\t\tinjector?: Injector;\n\t\t\tmanualCleanup?: false | null | undefined;\n\t  }\n\t| {\n\t\t\tdestroyRef?: never;\n\t\t\tinjector?: never;\n\t\t\tmanualCleanup?: true;\n\t  }\n);\n\nconst DEFAULT_ON_EVENT_OPTIONS: OnEventOptions = {\n\tonce: false,\n\tcapture: false,\n\tpassive: false,\n\tmanualCleanup: false,\n};\n\n/** Result of the `onEvent()` function. Contains the `destroy()` function to remove the listener and the `active` signal to check if the listener is still active.*/\nexport type OnEventRef = {\n\tdestroy: () => void;\n\tactive: Signal<boolean>;\n};\n\nfunction getDestroyRef(options?: OnEventOptions): DestroyRef | null {\n\tif (options?.manualCleanup) return null;\n\tif (options?.destroyRef) return options.destroyRef;\n\n\tconst injector = assertInjector(onEvent, options?.injector);\n\treturn injector.get(DestroyRef);\n}\n\nexport function onEvent<\n\tK extends string,\n\tE extends K extends keyof GlobalEventHandlersEventMap\n\t\t? GlobalEventHandlersEventMap[K]\n\t\t: Event,\n>(\n\ttarget: EventTarget,\n\teventKey: K,\n\tlistener: (event: E, abort: () => void) => void,\n\toptions?: OnEventOptions,\n): OnEventRef;\n\nexport function onEvent<E extends Event>(\n\ttarget: EventTarget,\n\teventKey: string,\n\tlistener: (event: E, abort: () => void) => void,\n\toptions?: OnEventOptions,\n): OnEventRef;\n\nexport function onEvent<E extends Event>(\n\ttarget: EventTarget,\n\teventKey: string,\n\tlistener: (event: E, abort: () => void) => void,\n\toptions: OnEventOptions = DEFAULT_ON_EVENT_OPTIONS,\n): OnEventRef {\n\tconst destroyRef = getDestroyRef(options);\n\tconst abortController = new AbortController();\n\tconst listenerActive = signal(true);\n\n\tconst abort = () => {\n\t\tabortController.abort();\n\t\tlistenerActive.set(false);\n\t\t// Prevent memory leak: Remove the onDestroy listener if aborted manually\n\t\tunregisterDestroyCallback?.();\n\t};\n\n\t// Register cleanup and store the unregister function\n\tconst unregisterDestroyCallback = destroyRef?.onDestroy(() => {\n\t\tabort();\n\t});\n\n\tconst eventListener = (event: Event) => {\n\t\tif (options?.once) {\n\t\t\tlistenerActive.set(false);\n\t\t\t// Also cleanup the destroyRef listener since we don't need it anymore\n\t\t\tunregisterDestroyCallback?.();\n\t\t}\n\n\t\tlistener(event as E, abort);\n\t};\n\n\ttarget.addEventListener(eventKey, eventListener, {\n\t\tsignal: abortController.signal,\n\t\tonce: options?.once,\n\t\tcapture: options?.capture,\n\t\tpassive: options?.passive,\n\t});\n\n\treturn {\n\t\tdestroy: () => abort(),\n\t\tactive: listenerActive.asReadonly(),\n\t};\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAyBA,MAAM,wBAAwB,GAAmB;AAChD,IAAA,IAAI,EAAE,KAAK;AACX,IAAA,OAAO,EAAE,KAAK;AACd,IAAA,OAAO,EAAE,KAAK;AACd,IAAA,aAAa,EAAE,KAAK;CACpB,CAAC;AAQF,SAAS,aAAa,CAAC,OAAwB,EAAA;IAC9C,IAAI,OAAO,EAAE,aAAa;AAAE,QAAA,OAAO,IAAI,CAAC;IACxC,IAAI,OAAO,EAAE,UAAU;QAAE,OAAO,OAAO,CAAC,UAAU,CAAC;IAEnD,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC5D,IAAA,OAAO,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,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