@sohanemon/utils 5.2.4 → 5.2.5

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.
@@ -26,5 +26,5 @@ interface UseAsyncReturn<TData, TError extends Error = Error> {
26
26
  * @param options - Configuration options including callbacks
27
27
  * @returns Object with data, error, status and helper methods
28
28
  */
29
- export declare function useAsync<TData, TError extends Error = Error>(asyncFn: () => Promise<TData>, options?: UseAsyncOptions<TData, TError>): UseAsyncReturn<TData, TError>;
29
+ export declare function useAsync<TData, TError extends Error = Error>(asyncFn: (signal: AbortSignal) => Promise<TData>, options?: UseAsyncOptions<TData, TError>): UseAsyncReturn<TData, TError>;
30
30
  export {};
@@ -11,7 +11,7 @@ export function useAsync(asyncFn, options = {}) {
11
11
  const [data, setData] = React.useState(undefined);
12
12
  const [error, setError] = React.useState(undefined);
13
13
  const [status, setStatus] = React.useState('idle');
14
- const isMountedRef = React.useRef(true);
14
+ const abortControllerRef = React.useRef(null);
15
15
  const memoizedOnSuccess = React.useCallback(onSuccess || (() => { }), [
16
16
  onSuccess,
17
17
  ]);
@@ -20,11 +20,16 @@ export function useAsync(asyncFn, options = {}) {
20
20
  onSettled,
21
21
  ]);
22
22
  const execute = React.useCallback(async () => {
23
+ if (abortControllerRef.current) {
24
+ abortControllerRef.current.abort();
25
+ }
26
+ abortControllerRef.current = new AbortController();
27
+ const signal = abortControllerRef.current.signal;
23
28
  setStatus('pending');
24
29
  setError(undefined);
25
30
  try {
26
- const result = await asyncFn();
27
- if (isMountedRef.current) {
31
+ const result = await asyncFn(signal);
32
+ if (!signal.aborted) {
28
33
  setData(result);
29
34
  setStatus('success');
30
35
  await memoizedOnSuccess(result);
@@ -33,9 +38,12 @@ export function useAsync(asyncFn, options = {}) {
33
38
  return result;
34
39
  }
35
40
  catch (err) {
41
+ if (err instanceof Error && err.name === 'AbortError') {
42
+ return undefined;
43
+ }
36
44
  const error = err instanceof Error ? err : new Error(String(err));
37
45
  const typedError = error;
38
- if (isMountedRef.current) {
46
+ if (!signal.aborted) {
39
47
  setError(typedError);
40
48
  setStatus('error');
41
49
  await memoizedOnError(typedError);
@@ -45,20 +53,22 @@ export function useAsync(asyncFn, options = {}) {
45
53
  }
46
54
  }, [asyncFn, memoizedOnSuccess, memoizedOnError, memoizedOnSettled]);
47
55
  React.useEffect(() => {
48
- if (mode === 'auto') {
56
+ if (mode === 'auto' && !deps) {
49
57
  execute();
50
58
  }
51
- }, [asyncFn, mode, execute]);
52
- React.useEffect(() => {
53
- return () => {
54
- isMountedRef.current = false;
55
- };
56
- }, []);
59
+ }, [asyncFn, mode, execute, deps]);
57
60
  React.useEffect(() => {
58
61
  if (deps && mode === 'auto') {
59
62
  execute();
60
63
  }
61
64
  }, deps ? deps : []);
65
+ React.useEffect(() => {
66
+ return () => {
67
+ if (abortControllerRef.current) {
68
+ abortControllerRef.current.abort();
69
+ }
70
+ };
71
+ }, []);
62
72
  return {
63
73
  data,
64
74
  error,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.2.4",
3
+ "version": "5.2.5",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",