@bouko/react 2.3.9 → 2.4.0

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.
@@ -1 +1,8 @@
1
- export default function useInterval(action: () => Promise<boolean | void>, delay: number, deps?: unknown[]): void;
1
+ declare type Props = {
2
+ action: () => Promise<boolean | void>;
3
+ duration: number;
4
+ deps: unknown[];
5
+ oops?: (x: string) => void;
6
+ };
7
+ export default function useInterval({ action, duration, deps, oops }: Props): void;
8
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { useEffect, useRef } from "react";
3
- export default function useInterval(action, delay, deps = []) {
3
+ export default function useInterval({ action, duration, deps = [], oops }) {
4
4
  const ref = useRef(async () => false);
5
5
  useEffect(() => {
6
6
  ref.current = action;
@@ -9,12 +9,17 @@ export default function useInterval(action, delay, deps = []) {
9
9
  if (deps.some(v => v === undefined || v === null))
10
10
  return;
11
11
  const tick = async () => {
12
- const stop = await ref.current();
13
- if (stop && id)
14
- clearInterval(id);
12
+ try {
13
+ const stop = await ref.current();
14
+ if (stop && id)
15
+ clearInterval(id);
16
+ }
17
+ catch (err) {
18
+ oops?.(err.message);
19
+ }
15
20
  };
16
- const id = setInterval(tick, delay);
21
+ const id = setInterval(tick, duration);
17
22
  tick();
18
23
  return () => clearInterval(id);
19
- }, [delay, ...deps]);
24
+ }, [duration, ...deps]);
20
25
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouko/react",
4
- "version": "2.3.9",
4
+ "version": "2.4.0",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",