@pushwoosh/kit-helpers 4.6.3 → 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.
@@ -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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/kit-helpers",
3
- "version": "4.6.3",
3
+ "version": "4.6.4",
4
4
  "description": "React component for build Pushwoosh products",
5
5
  "main": "index.js",
6
6
  "module": "index.js",