{"version":3,"file":"dispose-utils.cjs","names":["promisify","useDispose"],"sources":["../../src/common/dispose-utils.ts"],"sourcesContent":["import type { UseDisposeConfig } from './dispose-defer'\nimport type { DisposerFunction } from './dispose-types'\nimport type { LoggerInterface } from './log/log-base'\nimport { useDispose } from './dispose-defer'\nimport { promisify } from './exec/promise'\n\nexport type TimerExecFunction = () => void | Promise<void>\n\nfunction _createNoopDispose() {\n  const fn = () => {}\n  fn[Symbol.dispose] = fn\n  return fn as unknown as DisposerFunction\n}\n\nconst _noopDispose = _createNoopDispose()\n\n/** Returns a no-op disposer (shared instance, no allocation per call). */\nexport const noopDisposer: () => DisposerFunction = () => _noopDispose\n\n/**\n * Executes a function after a specified timeout and returns a disposer function\n * that can be used to cancel the timeout.\n *\n * @param fn - The function to execute after the timeout.\n * @param timeout - The timeout duration in milliseconds (default: 0).\n * @returns A disposer function that can be used to cancel the timeout.\n */\nexport function useTimeout(fn: TimerExecFunction, timeout = 0): DisposerFunction {\n  let timeoutHandle: any = setTimeout(fn, timeout)\n  const dispose = () => {\n    if (timeoutHandle) {\n      clearTimeout(timeoutHandle)\n      timeoutHandle = undefined\n    }\n  }\n  dispose[Symbol.dispose] = dispose\n  return dispose\n}\n\n/**\n * Executes a function repeatedly at a specified interval and returns a disposer function\n * that can be used to stop the execution.\n *\n * @param fn - The function to be executed at the specified interval.\n * @param interval - The interval (in milliseconds) at which the function should be executed.\n * @returns A disposer function that can be used to stop the execution of the function.\n */\nexport function useInterval(fn: TimerExecFunction, interval: number): DisposerFunction {\n  let intervalHandle: any = setInterval(fn, interval)\n  const dispose = () => {\n    if (intervalHandle) {\n      clearInterval(intervalHandle)\n      intervalHandle = undefined\n    }\n  }\n  dispose[Symbol.dispose] = dispose\n  return dispose\n}\n\n/** The interval starts only, when the function is finished. */\nexport function useIntervalPause(fn: TimerExecFunction, interval: number, immediately = false): DisposerFunction {\n  let intervalHandle: any\n  let stop = false\n\n  async function loop(exec = false) {\n    if (exec)\n      await promisify(fn())\n    if (!stop)\n      intervalHandle = setTimeout(loop, interval, true)\n  }\n\n  void loop(immediately)\n\n  const dispose = () => {\n    if (intervalHandle) {\n      stop = true\n      clearTimeout(intervalHandle) // uses setTimeout internally, so clearTimeout is correct\n      intervalHandle = undefined\n    }\n  }\n  dispose[Symbol.dispose] = dispose\n  return dispose\n}\n\nexport function useEventListener(\n  emitter: any,\n  eventName: string,\n  fn: (ev?: any) => void,\n  ...args: any[]\n): DisposerFunction {\n  if (emitter == null)\n    return noopDisposer()\n  if (emitter.on)\n    emitter.on(eventName, fn, ...args)\n  else if (emitter.addEventListener)\n    emitter.addEventListener(eventName, fn, ...args)\n  const dispose = () => {\n    if (emitter.off)\n      emitter.off(eventName, fn, ...args)\n    else if (emitter.removeEventListener)\n      emitter.removeEventListener(eventName, fn, ...args)\n  }\n  dispose[Symbol.dispose] = dispose\n  return dispose\n}\n\nexport function useEventListenerOnce(\n  emitter: any,\n  eventName: string,\n  fn: (ev?: any) => void,\n  ...args: any[]\n): DisposerFunction {\n  if (emitter == null)\n    return noopDisposer()\n  if (emitter.once)\n    emitter.once(eventName, fn, ...args)\n  else if (emitter.on)\n    emitter.on(eventName, fn, ...args)\n  else if (emitter.addEventListener)\n    emitter.addEventListener(eventName, fn, ...args)\n  const dispose = () => {\n    if (emitter.off)\n      emitter.off(eventName, fn, ...args)\n    else if (emitter.removeEventListener)\n      emitter.removeEventListener(eventName, fn, ...args)\n  }\n  dispose[Symbol.dispose] = dispose\n  return dispose\n}\n\n/** Like useDispose but with shorthands for emitter and timers */\nexport function useDisposeWithUtils(config?: string | UseDisposeConfig | LoggerInterface) {\n  const dispose = useDispose(config)\n  return Object.assign(dispose, {\n    timeout: (fn: TimerExecFunction, timeout = 0) => dispose.add(useTimeout(fn, timeout))!,\n    interval: (fn: TimerExecFunction, interval = 0) => dispose.add(useInterval(fn, interval))!,\n    intervalPause: (fn: TimerExecFunction, interval = 0) => dispose.add(useIntervalPause(fn, interval))!,\n    on: (emitter: any, eventName: string, fn: (ev?: any) => void, ...args: any[]) => dispose.add(useEventListener(emitter, eventName, fn, ...args))!,\n    once: (emitter: any, eventName: string, fn: (ev?: any) => void, ...args: any[]) => dispose.add(useEventListenerOnce(emitter, eventName, fn, ...args))!,\n  })\n}\n\nexport type UseDisposeWithUtils = ReturnType<typeof useDisposeWithUtils>\n"],"mappings":";;;;;AAQA,SAAS,qBAAqB;CAC5B,MAAM,WAAW;AACjB,IAAG,OAAO,WAAW;AACrB,QAAO;;AAGT,MAAM,eAAe,oBAAoB;;AAGzC,MAAa,qBAA6C;;;;;;;;;AAU1D,SAAgB,WAAW,IAAuB,UAAU,GAAqB;CAC/E,IAAI,gBAAqB,WAAW,IAAI,QAAQ;CAChD,MAAM,gBAAgB;AACpB,MAAI,eAAe;AACjB,gBAAa,cAAc;AAC3B,mBAAgB;;;AAGpB,SAAQ,OAAO,WAAW;AAC1B,QAAO;;;;;;;;;;AAWT,SAAgB,YAAY,IAAuB,UAAoC;CACrF,IAAI,iBAAsB,YAAY,IAAI,SAAS;CACnD,MAAM,gBAAgB;AACpB,MAAI,gBAAgB;AAClB,iBAAc,eAAe;AAC7B,oBAAiB;;;AAGrB,SAAQ,OAAO,WAAW;AAC1B,QAAO;;;AAIT,SAAgB,iBAAiB,IAAuB,UAAkB,cAAc,OAAyB;CAC/G,IAAI;CACJ,IAAI,OAAO;CAEX,eAAe,KAAK,OAAO,OAAO;AAChC,MAAI,KACF,OAAMA,sCAAU,IAAI,CAAC;AACvB,MAAI,CAAC,KACH,kBAAiB,WAAW,MAAM,UAAU,KAAK;;AAGrD,CAAK,KAAK,YAAY;CAEtB,MAAM,gBAAgB;AACpB,MAAI,gBAAgB;AAClB,UAAO;AACP,gBAAa,eAAe;AAC5B,oBAAiB;;;AAGrB,SAAQ,OAAO,WAAW;AAC1B,QAAO;;AAGT,SAAgB,iBACd,SACA,WACA,IACA,GAAG,MACe;AAClB,KAAI,WAAW,KACb,QAAO,cAAc;AACvB,KAAI,QAAQ,GACV,SAAQ,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