{"version":3,"file":"index.mjs","names":[],"sources":["../src/use-store.ts","../src/inject-stopwatch.ts"],"sourcesContent":["import type { Store } from '@watchstop/core'\nimport { DestroyRef, inject, signal, type Signal } from '@angular/core'\n\nexport function useStore<T>(store: Store<T>): Signal<T> {\n  const latestValue = signal(store.get())\n  const unsubscribe = store.subscribe((value) => {\n    latestValue.set(value)\n  })\n  inject(DestroyRef).onDestroy(unsubscribe)\n  return latestValue.asReadonly()\n}\n","import { Stopwatch } from '@watchstop/core'\nimport type { Clock } from '@watchstop/core'\nimport { DestroyRef, inject, signal, type Signal } from '@angular/core'\nimport { useStore } from './use-store.js'\n\nexport type InjectStopwatchOptions =\n  | {\n      clock?: Clock\n      precisionMs?: number\n      stopwatch?: undefined\n    }\n  | {\n      stopwatch: Stopwatch\n      clock?: never\n      precisionMs?: never\n    }\n\nexport type StopwatchBinding = {\n  elapsed: Signal<number>\n  running: Signal<boolean>\n  start: () => void\n  stop: () => void\n  reset: () => void\n  stopwatch: Stopwatch\n}\n\nfunction resolveStopwatch(options?: InjectStopwatchOptions): {\n  stopwatch: Stopwatch\n  ownsInstance: boolean\n} {\n  if (options?.stopwatch !== undefined) {\n    return { stopwatch: options.stopwatch, ownsInstance: false }\n  }\n  return {\n    stopwatch: new Stopwatch(options?.clock, {\n      precisionMs: options?.precisionMs,\n    }),\n    ownsInstance: true,\n  }\n}\n\nexport function injectStopwatch(\n  options?: InjectStopwatchOptions,\n): StopwatchBinding {\n  const { stopwatch, ownsInstance } = resolveStopwatch(options)\n  const elapsed = useStore(stopwatch)\n  const runningValue = signal(stopwatch.running)\n  const unsubscribeRunning = stopwatch.subscribe(() => {\n    runningValue.set(stopwatch.running)\n  })\n\n  inject(DestroyRef).onDestroy(unsubscribeRunning)\n  if (ownsInstance) {\n    inject(DestroyRef).onDestroy(() => {\n      stopwatch.destroy()\n    })\n  }\n\n  return {\n    elapsed,\n    running: runningValue.asReadonly(),\n    start: (): void => {\n      stopwatch.start()\n    },\n    stop: (): void => {\n      stopwatch.stop()\n    },\n    reset: (): void => {\n      stopwatch.reset()\n    },\n    stopwatch,\n  }\n}\n"],"mappings":";;;AAGA,SAAgB,SAAY,OAA4B;CACtD,MAAM,cAAc,OAAO,MAAM,IAAI,CAAC;CACtC,MAAM,cAAc,MAAM,WAAW,UAAU;EAC7C,YAAY,IAAI,KAAK;CACvB,CAAC;CACD,OAAO,UAAU,CAAC,CAAC,UAAU,WAAW;CACxC,OAAO,YAAY,WAAW;AAChC;;;ACgBA,SAAS,iBAAiB,SAGxB;CACA,IAAI,SAAS,cAAc,KAAA,GACzB,OAAO;EAAE,WAAW,QAAQ;EAAW,cAAc;CAAM;CAE7D,OAAO;EACL,WAAW,IAAI,UAAU,SAAS,OAAO,EACvC,aAAa,SAAS,YACxB,CAAC;EACD,cAAc;CAChB;AACF;AAEA,SAAgB,gBACd,SACkB;CAClB,MAAM,EAAE,WAAW,iBAAiB,iBAAiB,OAAO;CAC5D,MAAM,UAAU,SAAS,SAAS;CAClC,MAAM,eAAe,OAAO,UAAU,OAAO;CAC7C,MAAM,qBAAqB,UAAU,gBAAgB;EACnD,aAAa,IAAI,UAAU,OAAO;CACpC,CAAC;CAED,OAAO,UAAU,CAAC,CAAC,UAAU,kBAAkB;CAC/C,IAAI,cACF,OAAO,UAAU,CAAC,CAAC,gBAAgB;EACjC,UAAU,QAAQ;CACpB,CAAC;CAGH,OAAO;EACL;EACA,SAAS,aAAa,WAAW;EACjC,aAAmB;GACjB,UAAU,MAAM;EAClB;EACA,YAAkB;GAChB,UAAU,KAAK;EACjB;EACA,aAAmB;GACjB,UAAU,MAAM;EAClB;EACA;CACF;AACF"}