@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.
|
@@ -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.
|
|
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, "
|
|
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
|
-
|
|
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(
|
|
8
|
-
const [value, setValue] = (0, react_1.useState)(
|
|
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 (
|
|
11
|
-
|
|
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);
|