{"version":3,"file":"index.mjs","names":[],"sources":["../src/use-store.ts","../src/use-stopwatch.ts"],"sourcesContent":["import type { Store } from '@watchstop/core'\nimport { onScopeDispose, shallowRef, type ShallowRef } from 'vue'\n\nexport function useStore<T>(store: Store<T>): Readonly<ShallowRef<T>> {\n  const latestValue = shallowRef(store.get())\n  const unsubscribe = store.subscribe((value) => {\n    latestValue.value = value\n  })\n  onScopeDispose(unsubscribe)\n  return latestValue\n}\n","import { Stopwatch } from '@watchstop/core'\nimport type { Clock } from '@watchstop/core'\nimport { onScopeDispose, shallowRef, type ShallowRef } from 'vue'\nimport { useStore } from './use-store.js'\n\nexport type UseStopwatchOptions =\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: Readonly<ShallowRef<number>>\n  running: Readonly<ShallowRef<boolean>>\n  start: () => void\n  stop: () => void\n  reset: () => void\n  stopwatch: Stopwatch\n}\n\nfunction resolveStopwatch(options?: UseStopwatchOptions): {\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 useStopwatch(options?: UseStopwatchOptions): StopwatchBinding {\n  const { stopwatch, ownsInstance } = resolveStopwatch(options)\n  const elapsed = useStore(stopwatch)\n  const running = shallowRef(stopwatch.running)\n  const unsubscribeRunning = stopwatch.subscribe(() => {\n    running.value = stopwatch.running\n  })\n\n  onScopeDispose(unsubscribeRunning)\n  if (ownsInstance) {\n    onScopeDispose(() => {\n      stopwatch.destroy()\n    })\n  }\n\n  return {\n    elapsed,\n    running,\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,OAA0C;CACpE,MAAM,cAAc,WAAW,MAAM,IAAI,CAAC;CAI1C,eAHoB,MAAM,WAAW,UAAU;EAC7C,YAAY,QAAQ;CACtB,CACyB,CAAC;CAC1B,OAAO;AACT;;;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,aAAa,SAAiD;CAC5E,MAAM,EAAE,WAAW,iBAAiB,iBAAiB,OAAO;CAC5D,MAAM,UAAU,SAAS,SAAS;CAClC,MAAM,UAAU,WAAW,UAAU,OAAO;CAK5C,eAJ2B,UAAU,gBAAgB;EACnD,QAAQ,QAAQ,UAAU;CAC5B,CAEgC,CAAC;CACjC,IAAI,cACF,qBAAqB;EACnB,UAAU,QAAQ;CACpB,CAAC;CAGH,OAAO;EACL;EACA;EACA,aAAmB;GACjB,UAAU,MAAM;EAClB;EACA,YAAkB;GAChB,UAAU,KAAK;EACjB;EACA,aAAmB;GACjB,UAAU,MAAM;EAClB;EACA;CACF;AACF"}