@pushwoosh/kit-helpers 4.6.2 → 4.6.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/hooks/useAsync.js +3 -1
- package/hooks/useAsyncFunction.d.ts +29 -0
- package/hooks/useAsyncFunction.js +44 -0
- package/index.d.ts +1 -0
- package/index.js +2 -1
- package/package.json +1 -1
package/hooks/useAsync.js
CHANGED
|
@@ -6,7 +6,9 @@ const getInitialState = () => ({
|
|
|
6
6
|
export default function useAsync(fn, deps = []) {
|
|
7
7
|
const [state, callback] = useAsyncFn(fn, deps, getInitialState);
|
|
8
8
|
useEffect(() => {
|
|
9
|
-
callback()
|
|
9
|
+
callback().catch(error => {
|
|
10
|
+
console.error('Handled in useAsync', error);
|
|
11
|
+
});
|
|
10
12
|
}, [callback]);
|
|
11
13
|
return state;
|
|
12
14
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { FunctionReturningPromise, PromiseType } from './types';
|
|
2
|
+
export type AsyncFunctionState<T> = {
|
|
3
|
+
loading: boolean;
|
|
4
|
+
count: number;
|
|
5
|
+
error?: undefined;
|
|
6
|
+
value?: undefined;
|
|
7
|
+
} | {
|
|
8
|
+
loading: true;
|
|
9
|
+
count: number;
|
|
10
|
+
error?: Error | undefined;
|
|
11
|
+
value?: T;
|
|
12
|
+
} | {
|
|
13
|
+
loading: false;
|
|
14
|
+
count: number;
|
|
15
|
+
error: Error;
|
|
16
|
+
value?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
loading: false;
|
|
19
|
+
count: number;
|
|
20
|
+
error?: undefined;
|
|
21
|
+
value: T;
|
|
22
|
+
};
|
|
23
|
+
type StateFromFunctionReturningPromise<T extends FunctionReturningPromise> = AsyncFunctionState<PromiseType<ReturnType<T>>>;
|
|
24
|
+
export type AsyncFunctionReturn<T extends FunctionReturningPromise = FunctionReturningPromise> = [
|
|
25
|
+
StateFromFunctionReturningPromise<T>,
|
|
26
|
+
T
|
|
27
|
+
];
|
|
28
|
+
export default function useAsyncFunction<T extends FunctionReturningPromise>(fn: T): AsyncFunctionReturn<T>;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useRef, useState } from 'react';
|
|
2
|
+
import useMountedState from './useMountedState';
|
|
3
|
+
import usePersistentFunction from './usePersistentFunction';
|
|
4
|
+
const getDefaultState = () => ({
|
|
5
|
+
loading: false,
|
|
6
|
+
count: 0
|
|
7
|
+
});
|
|
8
|
+
export default function useAsyncFunction(fn) {
|
|
9
|
+
const countRef = useRef(0);
|
|
10
|
+
const isMounted = useMountedState();
|
|
11
|
+
const [state, setState] = useState(getDefaultState);
|
|
12
|
+
const execute = usePersistentFunction((...args) => {
|
|
13
|
+
// eslint-disable-next-line no-plusplus
|
|
14
|
+
const count = ++countRef.current;
|
|
15
|
+
const canSet = () => isMounted() && count === countRef.current;
|
|
16
|
+
if (canSet()) {
|
|
17
|
+
setState(prevState => ({
|
|
18
|
+
...prevState,
|
|
19
|
+
count,
|
|
20
|
+
loading: true
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
return fn(...args).then(value => {
|
|
24
|
+
if (canSet()) {
|
|
25
|
+
setState(prevState => ({
|
|
26
|
+
...prevState,
|
|
27
|
+
value,
|
|
28
|
+
loading: false
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}, error => {
|
|
33
|
+
if (canSet()) {
|
|
34
|
+
setState(prevState => ({
|
|
35
|
+
...prevState,
|
|
36
|
+
error,
|
|
37
|
+
loading: false
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
return [state, execute];
|
|
44
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -10,3 +10,4 @@ export { default as useAsync } from './hooks/useAsync';
|
|
|
10
10
|
export { default as useAsyncFn } from './hooks/useAsyncFn';
|
|
11
11
|
export { default as useDelayedValue } from './hooks/useDelayedValue';
|
|
12
12
|
export { default as useDelayedEffect } from './hooks/useDelayedEffect';
|
|
13
|
+
export { default as useAsyncFunction } from './hooks/useAsyncFunction';
|
package/index.js
CHANGED
|
@@ -9,4 +9,5 @@ export { default as useUnmount } from './hooks/useUnmount';
|
|
|
9
9
|
export { default as useAsync } from './hooks/useAsync';
|
|
10
10
|
export { default as useAsyncFn } from './hooks/useAsyncFn';
|
|
11
11
|
export { default as useDelayedValue } from './hooks/useDelayedValue';
|
|
12
|
-
export { default as useDelayedEffect } from './hooks/useDelayedEffect';
|
|
12
|
+
export { default as useDelayedEffect } from './hooks/useDelayedEffect';
|
|
13
|
+
export { default as useAsyncFunction } from './hooks/useAsyncFunction';
|