@bouku/core 0.2.3 → 0.2.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.
@@ -1,5 +1,5 @@
1
1
  export { default as AbsoluteBox } from "./absolute";
2
2
  export { default as FadeBox } from "./fade";
3
- export { default as Rnd } from "./rnd";
3
+ export { default as RnD } from "./rnd";
4
4
  export { default as ScrollBox } from "./scroll";
5
5
  export * from "./flex";
@@ -17,13 +17,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.ScrollBox = exports.Rnd = exports.FadeBox = exports.AbsoluteBox = void 0;
20
+ exports.ScrollBox = exports.RnD = exports.FadeBox = exports.AbsoluteBox = void 0;
21
21
  var absolute_1 = require("./absolute");
22
22
  Object.defineProperty(exports, "AbsoluteBox", { enumerable: true, get: function () { return __importDefault(absolute_1).default; } });
23
23
  var fade_1 = require("./fade");
24
24
  Object.defineProperty(exports, "FadeBox", { enumerable: true, get: function () { return __importDefault(fade_1).default; } });
25
25
  var rnd_1 = require("./rnd");
26
- Object.defineProperty(exports, "Rnd", { enumerable: true, get: function () { return __importDefault(rnd_1).default; } });
26
+ Object.defineProperty(exports, "RnD", { enumerable: true, get: function () { return __importDefault(rnd_1).default; } });
27
27
  var scroll_1 = require("./scroll");
28
28
  Object.defineProperty(exports, "ScrollBox", { enumerable: true, get: function () { return __importDefault(scroll_1).default; } });
29
29
  __exportStar(require("./flex"), exports);
@@ -1,4 +1,9 @@
1
- export declare function useValue<T>(getValue: () => Promise<T>): T | null;
1
+ interface GetValueOptions<T> {
2
+ fetcher: () => Promise<T>;
3
+ callback?: () => void;
4
+ interval?: number;
5
+ }
6
+ export declare function useValue<T>({ fetcher, callback, interval }: GetValueOptions<T>): readonly [T | null | undefined, boolean];
2
7
  export declare function useList<T extends {
3
8
  id?: string;
4
9
  }>(initial?: T[]): {
@@ -4,13 +4,42 @@ exports.useValue = useValue;
4
4
  exports.useList = useList;
5
5
  exports.useMap = useMap;
6
6
  const react_1 = require("react");
7
- function useValue(getValue) {
8
- const [value, setValue] = (0, react_1.useState)(null);
7
+ function useValue({ fetcher, callback, interval }) {
8
+ const [value, setValue] = (0, react_1.useState)();
9
+ const [isLoading, setLoading] = (0, react_1.useState)(true);
10
+ const fetchValue = async (showLoading = false) => {
11
+ if (showLoading)
12
+ setLoading(true);
13
+ try {
14
+ console.log("fetching");
15
+ const result = await fetcher();
16
+ const nextValue = result ?? null;
17
+ setValue(current => Object.is(current, nextValue)
18
+ ? current
19
+ : nextValue);
20
+ }
21
+ catch (err) {
22
+ console.error(err);
23
+ }
24
+ finally {
25
+ if (showLoading)
26
+ setLoading(false);
27
+ }
28
+ };
29
+ (0, react_1.useEffect)(() => {
30
+ fetchValue(true);
31
+ }, []);
32
+ (0, react_1.useEffect)(() => {
33
+ if (!interval || value)
34
+ return;
35
+ const id = setInterval(() => fetchValue(), interval * 1000);
36
+ return () => clearInterval(id);
37
+ }, [interval, value]);
9
38
  (0, react_1.useEffect)(() => {
10
- if (!value)
11
- getValue().then(setValue);
12
- }, [value]);
13
- return value;
39
+ if (value && callback)
40
+ callback();
41
+ }, [value, callback]);
42
+ return [value, isLoading];
14
43
  }
15
44
  function useList(initial = []) {
16
45
  const [items, setItems] = (0, react_1.useState)(initial);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouku/core",
4
- "version": "0.2.3",
4
+ "version": "0.2.4",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",