@8ms/helpers 2.4.12 → 2.4.13
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.
|
@@ -2,22 +2,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
require("../../_virtual/_rolldown/runtime.cjs");
|
|
3
3
|
let react = require("react");
|
|
4
4
|
//#region src/nextJs/client/useAsyncEffect.ts
|
|
5
|
-
const useAsyncEffect = (effect,
|
|
6
|
-
const hasDestroy =
|
|
7
|
-
|
|
5
|
+
const useAsyncEffect = (effect, destroyOrInputs, inputs) => {
|
|
6
|
+
const hasDestroy = "function" === typeof destroyOrInputs;
|
|
7
|
+
const destroy = hasDestroy ? destroyOrInputs : void 0;
|
|
8
|
+
(0, react.useEffect)(() => {
|
|
8
9
|
let result;
|
|
9
10
|
let mounted = true;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
});
|
|
13
|
-
Promise.resolve(maybePromise).then(function(value) {
|
|
11
|
+
const maybePromise = effect(() => mounted);
|
|
12
|
+
Promise.resolve(maybePromise).then((value) => {
|
|
14
13
|
result = value;
|
|
15
14
|
});
|
|
16
|
-
return
|
|
15
|
+
return () => {
|
|
17
16
|
mounted = false;
|
|
18
|
-
if (hasDestroy) destroy(result);
|
|
17
|
+
if (hasDestroy && destroy) destroy(result);
|
|
19
18
|
};
|
|
20
|
-
}, hasDestroy ? inputs :
|
|
19
|
+
}, hasDestroy ? inputs : destroyOrInputs);
|
|
21
20
|
};
|
|
22
21
|
//#endregion
|
|
23
22
|
exports.useAsyncEffect = useAsyncEffect;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
//#region src/nextJs/client/useAsyncEffect.d.ts
|
|
2
|
-
|
|
2
|
+
type UseAsyncEffect = {
|
|
3
|
+
(effect: (isMounted: () => boolean) => unknown | Promise<unknown>, inputs?: any[]): void;
|
|
4
|
+
<T>(effect: (isMounted: () => boolean) => T | Promise<T>, destroy?: (result?: T) => void, inputs?: any[]): void;
|
|
5
|
+
};
|
|
6
|
+
declare const useAsyncEffect: UseAsyncEffect;
|
|
3
7
|
//#endregion
|
|
4
8
|
export { useAsyncEffect };
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
//#region src/nextJs/client/useAsyncEffect.d.ts
|
|
2
|
-
|
|
2
|
+
type UseAsyncEffect = {
|
|
3
|
+
(effect: (isMounted: () => boolean) => unknown | Promise<unknown>, inputs?: any[]): void;
|
|
4
|
+
<T>(effect: (isMounted: () => boolean) => T | Promise<T>, destroy?: (result?: T) => void, inputs?: any[]): void;
|
|
5
|
+
};
|
|
6
|
+
declare const useAsyncEffect: UseAsyncEffect;
|
|
3
7
|
//#endregion
|
|
4
8
|
export { useAsyncEffect };
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { useEffect } from "react";
|
|
2
2
|
//#region src/nextJs/client/useAsyncEffect.ts
|
|
3
|
-
const useAsyncEffect = (effect,
|
|
4
|
-
const hasDestroy =
|
|
5
|
-
|
|
3
|
+
const useAsyncEffect = (effect, destroyOrInputs, inputs) => {
|
|
4
|
+
const hasDestroy = "function" === typeof destroyOrInputs;
|
|
5
|
+
const destroy = hasDestroy ? destroyOrInputs : void 0;
|
|
6
|
+
useEffect(() => {
|
|
6
7
|
let result;
|
|
7
8
|
let mounted = true;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
});
|
|
11
|
-
Promise.resolve(maybePromise).then(function(value) {
|
|
9
|
+
const maybePromise = effect(() => mounted);
|
|
10
|
+
Promise.resolve(maybePromise).then((value) => {
|
|
12
11
|
result = value;
|
|
13
12
|
});
|
|
14
|
-
return
|
|
13
|
+
return () => {
|
|
15
14
|
mounted = false;
|
|
16
|
-
if (hasDestroy) destroy(result);
|
|
15
|
+
if (hasDestroy && destroy) destroy(result);
|
|
17
16
|
};
|
|
18
|
-
}, hasDestroy ? inputs :
|
|
17
|
+
}, hasDestroy ? inputs : destroyOrInputs);
|
|
19
18
|
};
|
|
20
19
|
//#endregion
|
|
21
20
|
export { useAsyncEffect };
|