@prtcl/plonk-hooks 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -19,13 +19,13 @@ var __copyProps = (to, from, except, desc) => {
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
20
 
21
21
  // src/index.ts
22
- var src_exports = {};
23
- __export(src_exports, {
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
24
  useFrames: () => useFrames,
25
25
  useMetro: () => useMetro,
26
26
  usePrevious: () => usePrevious
27
27
  });
28
- module.exports = __toCommonJS(src_exports);
28
+ module.exports = __toCommonJS(index_exports);
29
29
  var import_client_only = require("client-only");
30
30
 
31
31
  // src/hooks/useFrames.ts
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/hooks/useFrames.ts","../src/hooks/usePrevious.ts","../src/hooks/useMetro.ts"],"sourcesContent":["'use client';\n\nimport 'client-only';\n\nexport { useFrames, type UseFramesOptions } from './hooks/useFrames';\nexport { useMetro, type UseMetroOptions } from './hooks/useMetro';\nexport { usePrevious } from './hooks/usePrevious';\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Frames, type FramesOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseFramesOptions = FramesOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Frames which provides an animation loop with variable frame rate\n */\nexport const useFrames = (\n callback: TimerCallback<Frames>,\n opts?: UseFramesOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Frames>>(callback);\n const optsRef = useRef<UseFramesOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const frames = useMemo<Frames>(() => {\n return new Frames((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.fps !== prevOpts.fps) {\n frames.setFPS(opts.fps);\n }\n }, [opts, prevOpts, frames]);\n\n useEffect(() => {\n if (autostart) {\n frames.run();\n }\n\n return () => {\n frames.stop();\n };\n }, [frames]);\n\n return frames;\n};\n","import { useRef } from 'react';\n\ntype ValueCache<Value> = {\n value: Value;\n prev: Value | undefined;\n};\n\n/** Returns the previous value without causing additional updates. */\nexport const usePrevious = <Value>(value: Value): Value | undefined => {\n const ref = useRef<ValueCache<Value>>({\n value,\n prev: undefined,\n });\n\n const current = ref.current.value;\n\n if (value !== current) {\n ref.current = {\n value,\n prev: current,\n };\n }\n\n return ref.current.prev;\n};\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Metro, type MetroOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseMetroOptions = MetroOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Metro which provides a timer loop with variable interval.\n */\nexport const useMetro = (\n callback: TimerCallback<Metro>,\n opts?: UseMetroOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Metro>>(callback);\n const optsRef = useRef<UseMetroOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const metro = useMemo<Metro>(() => {\n return new Metro((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.time !== prevOpts.time) {\n metro.setTime(opts.time);\n }\n }, [opts, prevOpts, metro]);\n\n useEffect(() => {\n if (autostart) {\n metro.run();\n }\n\n return () => {\n metro.stop();\n };\n }, [metro]);\n\n return metro;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAO;;;ACFP,IAAAA,gBAA2C;AAC3C,mBAA+D;;;ACD/D,mBAAuB;AAQhB,IAAM,cAAc,CAAQ,UAAoC;AACrE,QAAM,UAAM,qBAA0B;AAAA,IACpC;AAAA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Q,OAAO;AAAA,EACpB,GAAG,CAAC,CAAC;AAEL,+BAAU,MAAM;AACd,QAAI,QAAQ,YAAY,KAAK,SAAS,SAAS,MAAM;AACnD,YAAM,QAAQ,KAAK,IAAI;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,KAAK,CAAC;AAE1B,+BAAU,MAAM;AACd,QAAI,WAAW;AACb,YAAM,IAAI;AAAA,IACZ;AAEA,WAAO,MAAM;AACX,YAAM,KAAK;AAAA,IACb;AAAA,EACF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":["import_react","import_react","import_plonk"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/hooks/useFrames.ts","../src/hooks/usePrevious.ts","../src/hooks/useMetro.ts"],"sourcesContent":["'use client';\n\nimport 'client-only';\n\nexport { useFrames, type UseFramesOptions } from './hooks/useFrames';\nexport { useMetro, type UseMetroOptions } from './hooks/useMetro';\nexport { usePrevious } from './hooks/usePrevious';\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Frames, type FramesOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseFramesOptions = FramesOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Frames, autostart begins on mount and stops on unmount.\n * @param callback - {@link TimerCallback} called on each frame tick.\n * @param opts - {@link UseFramesOptions} for configuring frame rate and autostart.\n * @returns The underlying Frames instance.\n */\nexport const useFrames = (\n callback: TimerCallback<Frames>,\n opts?: UseFramesOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Frames>>(callback);\n const optsRef = useRef<UseFramesOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const frames = useMemo<Frames>(() => {\n return new Frames((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.fps !== prevOpts.fps) {\n frames.setFPS(opts.fps);\n }\n }, [opts, prevOpts, frames]);\n\n useEffect(() => {\n if (autostart) {\n frames.run();\n }\n\n return () => {\n frames.stop();\n };\n }, [frames]);\n\n return frames;\n};\n","import { useRef } from 'react';\n\ntype ValueCache<Value> = {\n value: Value;\n prev: Value | undefined;\n};\n\n/**\n * Returns the previous value of a variable without causing additional renders.\n * @param value - The current value to track.\n * @returns The value from the previous render, or undefined on the first render.\n */\nexport const usePrevious = <Value>(value: Value): Value | undefined => {\n const ref = useRef<ValueCache<Value>>({\n value,\n prev: undefined,\n });\n\n const current = ref.current.value;\n\n if (value !== current) {\n ref.current = {\n value,\n prev: current,\n };\n }\n\n return ref.current.prev;\n};\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Metro, type MetroOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseMetroOptions = MetroOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Metro, autostart begins on mount and stops on unmount.\n * @param callback - {@link TimerCallback} called on each tick.\n * @param opts - {@link UseMetroOptions} for configuring the timer and autostart.\n * @returns The underlying Metro instance.\n */\nexport const useMetro = (\n callback: TimerCallback<Metro>,\n opts?: UseMetroOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Metro>>(callback);\n const optsRef = useRef<UseMetroOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const metro = useMemo<Metro>(() => {\n return new Metro((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.time !== prevOpts.time) {\n metro.setTime(opts.time);\n }\n }, [opts, prevOpts, metro]);\n\n useEffect(() => {\n if (autostart) {\n metro.run();\n }\n\n return () => {\n metro.stop();\n };\n }, [metro]);\n\n return metro;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAO;;;ACFP,IAAAA,gBAA2C;AAC3C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AQ,uBAAe,MAAM;AACjC,WAAO,IAAI,oBAAM,CAAC,MAAM;AACtB,kBAAY,QAAQ,CAAC;AAAA,IACvB,GAAG,QAAQ,OAAO;AAAA,EACpB,GAAG,CAAC,CAAC;AAEL,+BAAU,MAAM;AACd,QAAI,QAAQ,YAAY,KAAK,SAAS,SAAS,MAAM;AACnD,YAAM,QAAQ,KAAK,IAAI;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,KAAK,CAAC;AAE1B,+BAAU,MAAM;AACd,QAAI,WAAW;AACb,YAAM,IAAI;AAAA,IACZ;AAEA,WAAO,MAAM;AACX,YAAM,KAAK;AAAA,IACb;AAAA,EACF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":["import_react","import_react","import_plonk"]}
package/dist/index.d.cts CHANGED
@@ -4,7 +4,10 @@ type UseFramesOptions = FramesOptions & {
4
4
  autostart?: boolean;
5
5
  };
6
6
  /**
7
- * Hook wrapper for Frames which provides an animation loop with variable frame rate
7
+ * Hook wrapper for Frames, autostart begins on mount and stops on unmount.
8
+ * @param callback - {@link TimerCallback} called on each frame tick.
9
+ * @param opts - {@link UseFramesOptions} for configuring frame rate and autostart.
10
+ * @returns The underlying Frames instance.
8
11
  */
9
12
  declare const useFrames: (callback: TimerCallback<Frames>, opts?: UseFramesOptions) => Frames;
10
13
 
@@ -12,11 +15,18 @@ type UseMetroOptions = MetroOptions & {
12
15
  autostart?: boolean;
13
16
  };
14
17
  /**
15
- * Hook wrapper for Metro which provides a timer loop with variable interval.
18
+ * Hook wrapper for Metro, autostart begins on mount and stops on unmount.
19
+ * @param callback - {@link TimerCallback} called on each tick.
20
+ * @param opts - {@link UseMetroOptions} for configuring the timer and autostart.
21
+ * @returns The underlying Metro instance.
16
22
  */
17
23
  declare const useMetro: (callback: TimerCallback<Metro>, opts?: UseMetroOptions) => Metro;
18
24
 
19
- /** Returns the previous value without causing additional updates. */
25
+ /**
26
+ * Returns the previous value of a variable without causing additional renders.
27
+ * @param value - The current value to track.
28
+ * @returns The value from the previous render, or undefined on the first render.
29
+ */
20
30
  declare const usePrevious: <Value>(value: Value) => Value | undefined;
21
31
 
22
32
  export { type UseFramesOptions, type UseMetroOptions, useFrames, useMetro, usePrevious };
package/dist/index.d.ts CHANGED
@@ -4,7 +4,10 @@ type UseFramesOptions = FramesOptions & {
4
4
  autostart?: boolean;
5
5
  };
6
6
  /**
7
- * Hook wrapper for Frames which provides an animation loop with variable frame rate
7
+ * Hook wrapper for Frames, autostart begins on mount and stops on unmount.
8
+ * @param callback - {@link TimerCallback} called on each frame tick.
9
+ * @param opts - {@link UseFramesOptions} for configuring frame rate and autostart.
10
+ * @returns The underlying Frames instance.
8
11
  */
9
12
  declare const useFrames: (callback: TimerCallback<Frames>, opts?: UseFramesOptions) => Frames;
10
13
 
@@ -12,11 +15,18 @@ type UseMetroOptions = MetroOptions & {
12
15
  autostart?: boolean;
13
16
  };
14
17
  /**
15
- * Hook wrapper for Metro which provides a timer loop with variable interval.
18
+ * Hook wrapper for Metro, autostart begins on mount and stops on unmount.
19
+ * @param callback - {@link TimerCallback} called on each tick.
20
+ * @param opts - {@link UseMetroOptions} for configuring the timer and autostart.
21
+ * @returns The underlying Metro instance.
16
22
  */
17
23
  declare const useMetro: (callback: TimerCallback<Metro>, opts?: UseMetroOptions) => Metro;
18
24
 
19
- /** Returns the previous value without causing additional updates. */
25
+ /**
26
+ * Returns the previous value of a variable without causing additional renders.
27
+ * @param value - The current value to track.
28
+ * @returns The value from the previous render, or undefined on the first render.
29
+ */
20
30
  declare const usePrevious: <Value>(value: Value) => Value | undefined;
21
31
 
22
32
  export { type UseFramesOptions, type UseMetroOptions, useFrames, useMetro, usePrevious };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/hooks/useFrames.ts","../src/hooks/usePrevious.ts","../src/hooks/useMetro.ts"],"sourcesContent":["'use client';\n\nimport 'client-only';\n\nexport { useFrames, type UseFramesOptions } from './hooks/useFrames';\nexport { useMetro, type UseMetroOptions } from './hooks/useMetro';\nexport { usePrevious } from './hooks/usePrevious';\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Frames, type FramesOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseFramesOptions = FramesOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Frames which provides an animation loop with variable frame rate\n */\nexport const useFrames = (\n callback: TimerCallback<Frames>,\n opts?: UseFramesOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Frames>>(callback);\n const optsRef = useRef<UseFramesOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const frames = useMemo<Frames>(() => {\n return new Frames((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.fps !== prevOpts.fps) {\n frames.setFPS(opts.fps);\n }\n }, [opts, prevOpts, frames]);\n\n useEffect(() => {\n if (autostart) {\n frames.run();\n }\n\n return () => {\n frames.stop();\n };\n }, [frames]);\n\n return frames;\n};\n","import { useRef } from 'react';\n\ntype ValueCache<Value> = {\n value: Value;\n prev: Value | undefined;\n};\n\n/** Returns the previous value without causing additional updates. */\nexport const usePrevious = <Value>(value: Value): Value | undefined => {\n const ref = useRef<ValueCache<Value>>({\n value,\n prev: undefined,\n });\n\n const current = ref.current.value;\n\n if (value !== current) {\n ref.current = {\n value,\n prev: current,\n };\n }\n\n return ref.current.prev;\n};\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Metro, type MetroOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseMetroOptions = MetroOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Metro which provides a timer loop with variable interval.\n */\nexport const useMetro = (\n callback: TimerCallback<Metro>,\n opts?: UseMetroOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Metro>>(callback);\n const optsRef = useRef<UseMetroOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const metro = useMemo<Metro>(() => {\n return new Metro((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.time !== prevOpts.time) {\n metro.setTime(opts.time);\n }\n }, [opts, prevOpts, metro]);\n\n useEffect(() => {\n if (autostart) {\n metro.run();\n }\n\n return () => {\n metro.stop();\n };\n }, [metro]);\n\n return metro;\n};\n"],"mappings":";;;AAEA,OAAO;;;ACFP,SAAS,WAAW,SAAS,UAAAA,eAAc;AAC3C,SAAS,cAAsD;;;ACD/D,SAAS,cAAc;AAQhB,IAAM,cAAc,CAAQ,UAAoC;AACrE,QAAM,MAAM,OAA0B;AAAA,IACpC;AAAA,IACA,MAAM;AAAA,EACR,CAAC;AAED,QAAM,UAAU,IAAI,QAAQ;AAE5B,MAAI,UAAU,SAAS;AACrB,QAAI,UAAU;AAAA,MACZ;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF;AAEA,SAAO,IAAI,QAAQ;AACrB;;;ADbO,IAAM,YAAY,CACvB,UACA,SACG;AACH,QAAM,EAAE,YAAY,KAAK,IAAI,QAAQ,CAAC;AACtC,QAAM,cAAcC,QAA8B,QAAQ;AAC1D,QAAM,UAAUA,QAAqC,IAAI;AACzD,QAAM,WAAW,YAAY,IAAI;AAEjC,cAAY,UAAU;AAEtB,QAAM,SAAS,QAAgB,MAAM;AACnC,WAAO,IAAI,OAAO,CAAC,MAAM;AACvB,kBAAY,QAAQ,CAAC;AAAA,IACvB,GAAG,QAAQ,OAAO;AAAA,EACpB,GAAG,CAAC,CAAC;AAEL,YAAU,MAAM;AACd,QAAI,QAAQ,YAAY,KAAK,QAAQ,SAAS,KAAK;AACjD,aAAO,OAAO,KAAK,GAAG;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,MAAM,CAAC;AAE3B,YAAU,MAAM;AACd,QAAI,WAAW;AACb,aAAO,IAAI;AAAA,IACb;AAEA,WAAO,MAAM;AACX,aAAO,KAAK;AAAA,IACd;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO;AACT;;;AE7CA,SAAS,aAAAC,YAAW,WAAAC,UAAS,UAAAC,eAAc;AAC3C,SAAS,aAAoD;AAUtD,IAAM,WAAW,CACtB,UACA,SACG;AACH,QAAM,EAAE,YAAY,KAAK,IAAI,QAAQ,CAAC;AACtC,QAAM,cAAcC,QAA6B,QAAQ;AACzD,QAAM,UAAUA,QAAoC,IAAI;AACxD,QAAM,WAAW,YAAY,IAAI;AAEjC,cAAY,UAAU;AAEtB,QAAM,QAAQC,SAAe,MAAM;AACjC,WAAO,IAAI,MAAM,CAAC,MAAM;AACtB,kBAAY,QAAQ,CAAC;AAAA,IACvB,GAAG,QAAQ,OAAO;AAAA,EACpB,GAAG,CAAC,CAAC;AAEL,EAAAC,WAAU,MAAM;AACd,QAAI,QAAQ,YAAY,KAAK,SAAS,SAAS,MAAM;AACnD,YAAM,QAAQ,KAAK,IAAI;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,KAAK,CAAC;AAE1B,EAAAA,WAAU,MAAM;AACd,QAAI,WAAW;AACb,YAAM,IAAI;AAAA,IACZ;AAEA,WAAO,MAAM;AACX,YAAM,KAAK;AAAA,IACb;AAAA,EACF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":["useRef","useRef","useEffect","useMemo","useRef","useRef","useMemo","useEffect"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/hooks/useFrames.ts","../src/hooks/usePrevious.ts","../src/hooks/useMetro.ts"],"sourcesContent":["'use client';\n\nimport 'client-only';\n\nexport { useFrames, type UseFramesOptions } from './hooks/useFrames';\nexport { useMetro, type UseMetroOptions } from './hooks/useMetro';\nexport { usePrevious } from './hooks/usePrevious';\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Frames, type FramesOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseFramesOptions = FramesOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Frames, autostart begins on mount and stops on unmount.\n * @param callback - {@link TimerCallback} called on each frame tick.\n * @param opts - {@link UseFramesOptions} for configuring frame rate and autostart.\n * @returns The underlying Frames instance.\n */\nexport const useFrames = (\n callback: TimerCallback<Frames>,\n opts?: UseFramesOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Frames>>(callback);\n const optsRef = useRef<UseFramesOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const frames = useMemo<Frames>(() => {\n return new Frames((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.fps !== prevOpts.fps) {\n frames.setFPS(opts.fps);\n }\n }, [opts, prevOpts, frames]);\n\n useEffect(() => {\n if (autostart) {\n frames.run();\n }\n\n return () => {\n frames.stop();\n };\n }, [frames]);\n\n return frames;\n};\n","import { useRef } from 'react';\n\ntype ValueCache<Value> = {\n value: Value;\n prev: Value | undefined;\n};\n\n/**\n * Returns the previous value of a variable without causing additional renders.\n * @param value - The current value to track.\n * @returns The value from the previous render, or undefined on the first render.\n */\nexport const usePrevious = <Value>(value: Value): Value | undefined => {\n const ref = useRef<ValueCache<Value>>({\n value,\n prev: undefined,\n });\n\n const current = ref.current.value;\n\n if (value !== current) {\n ref.current = {\n value,\n prev: current,\n };\n }\n\n return ref.current.prev;\n};\n","import { useEffect, useMemo, useRef } from 'react';\nimport { Metro, type MetroOptions, type TimerCallback } from '@prtcl/plonk';\nimport { usePrevious } from './usePrevious';\n\nexport type UseMetroOptions = MetroOptions & {\n autostart?: boolean;\n};\n\n/**\n * Hook wrapper for Metro, autostart begins on mount and stops on unmount.\n * @param callback - {@link TimerCallback} called on each tick.\n * @param opts - {@link UseMetroOptions} for configuring the timer and autostart.\n * @returns The underlying Metro instance.\n */\nexport const useMetro = (\n callback: TimerCallback<Metro>,\n opts?: UseMetroOptions,\n) => {\n const { autostart = true } = opts || {};\n const callbackRef = useRef<TimerCallback<Metro>>(callback);\n const optsRef = useRef<UseMetroOptions | undefined>(opts);\n const prevOpts = usePrevious(opts);\n\n callbackRef.current = callback;\n\n const metro = useMemo<Metro>(() => {\n return new Metro((m) => {\n callbackRef.current(m);\n }, optsRef.current);\n }, []);\n\n useEffect(() => {\n if (opts && prevOpts && opts.time !== prevOpts.time) {\n metro.setTime(opts.time);\n }\n }, [opts, prevOpts, metro]);\n\n useEffect(() => {\n if (autostart) {\n metro.run();\n }\n\n return () => {\n metro.stop();\n };\n }, [metro]);\n\n return metro;\n};\n"],"mappings":";;;AAEA,OAAO;;;ACFP,SAAS,WAAW,SAAS,UAAAA,eAAc;AAC3C,SAAS,cAAsD;;;ACD/D,SAAS,cAAc;AAYhB,IAAM,cAAc,CAAQ,UAAoC;AACrE,QAAM,MAAM,OAA0B;AAAA,IACpC;AAAA,IACA,MAAM;AAAA,EACR,CAAC;AAED,QAAM,UAAU,IAAI,QAAQ;AAE5B,MAAI,UAAU,SAAS;AACrB,QAAI,UAAU;AAAA,MACZ;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF;AAEA,SAAO,IAAI,QAAQ;AACrB;;;ADdO,IAAM,YAAY,CACvB,UACA,SACG;AACH,QAAM,EAAE,YAAY,KAAK,IAAI,QAAQ,CAAC;AACtC,QAAM,cAAcC,QAA8B,QAAQ;AAC1D,QAAM,UAAUA,QAAqC,IAAI;AACzD,QAAM,WAAW,YAAY,IAAI;AAEjC,cAAY,UAAU;AAEtB,QAAM,SAAS,QAAgB,MAAM;AACnC,WAAO,IAAI,OAAO,CAAC,MAAM;AACvB,kBAAY,QAAQ,CAAC;AAAA,IACvB,GAAG,QAAQ,OAAO;AAAA,EACpB,GAAG,CAAC,CAAC;AAEL,YAAU,MAAM;AACd,QAAI,QAAQ,YAAY,KAAK,QAAQ,SAAS,KAAK;AACjD,aAAO,OAAO,KAAK,GAAG;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,MAAM,CAAC;AAE3B,YAAU,MAAM;AACd,QAAI,WAAW;AACb,aAAO,IAAI;AAAA,IACb;AAEA,WAAO,MAAM;AACX,aAAO,KAAK;AAAA,IACd;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO;AACT;;;AEhDA,SAAS,aAAAC,YAAW,WAAAC,UAAS,UAAAC,eAAc;AAC3C,SAAS,aAAoD;AAatD,IAAM,WAAW,CACtB,UACA,SACG;AACH,QAAM,EAAE,YAAY,KAAK,IAAI,QAAQ,CAAC;AACtC,QAAM,cAAcC,QAA6B,QAAQ;AACzD,QAAM,UAAUA,QAAoC,IAAI;AACxD,QAAM,WAAW,YAAY,IAAI;AAEjC,cAAY,UAAU;AAEtB,QAAM,QAAQC,SAAe,MAAM;AACjC,WAAO,IAAI,MAAM,CAAC,MAAM;AACtB,kBAAY,QAAQ,CAAC;AAAA,IACvB,GAAG,QAAQ,OAAO;AAAA,EACpB,GAAG,CAAC,CAAC;AAEL,EAAAC,WAAU,MAAM;AACd,QAAI,QAAQ,YAAY,KAAK,SAAS,SAAS,MAAM;AACnD,YAAM,QAAQ,KAAK,IAAI;AAAA,IACzB;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,KAAK,CAAC;AAE1B,EAAAA,WAAU,MAAM;AACd,QAAI,WAAW;AACb,YAAM,IAAI;AAAA,IACZ;AAEA,WAAO,MAAM;AACX,YAAM,KAAK;AAAA,IACb;AAAA,EACF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":["useRef","useRef","useEffect","useMemo","useRef","useRef","useMemo","useEffect"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prtcl/plonk-hooks",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "React hook wrappers for plonk",
5
5
  "author": "Cory O'Brien <cory@prtcl.cc>",
6
6
  "license": "MIT",
@@ -8,9 +8,9 @@
8
8
  "main": "./dist/index.js",
9
9
  "exports": {
10
10
  ".": {
11
+ "types": "./dist/index.d.ts",
11
12
  "import": "./dist/index.js",
12
- "require": "./dist/index.cjs",
13
- "types": "./dist/index.d.ts"
13
+ "require": "./dist/index.cjs"
14
14
  }
15
15
  },
16
16
  "repository": {
@@ -23,7 +23,7 @@
23
23
  "test": "vitest --browser.name=chrome --browser.headless --watch=false"
24
24
  },
25
25
  "dependencies": {
26
- "@prtcl/plonk": "1.0.2",
26
+ "@prtcl/plonk": "1.0.4",
27
27
  "client-only": "^0.0.1"
28
28
  },
29
29
  "peerDependencies": {