@sohanemon/utils 4.0.16 → 4.0.17
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/dist/hooks/index.d.ts +16 -0
- package/dist/hooks/index.js +41 -3
- package/package.json +4 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -10,4 +10,20 @@ export declare function useWindowEvent<K extends string = keyof WindowEventMap>(
|
|
|
10
10
|
type LocalStorageValue<T> = [T, Dispatch<SetStateAction<T>>];
|
|
11
11
|
export declare const useLocalStorage: <T extends Record<string, any>>(key: string, defaultValue: T) => LocalStorageValue<T>;
|
|
12
12
|
export declare const useUrlParams: <T extends string | number | boolean>(key: string, defaultValue: T) => [T, (value: T) => void];
|
|
13
|
+
interface UseAsyncOptions<T extends (...args: any) => any> {
|
|
14
|
+
initialArgs?: Parameters<T>[0];
|
|
15
|
+
callback?: {
|
|
16
|
+
onSuccess?: (result: T) => void;
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
onExecute?: () => void;
|
|
19
|
+
onSettle?: () => void;
|
|
20
|
+
};
|
|
21
|
+
mode?: 'onLoad' | 'onTrigger';
|
|
22
|
+
}
|
|
23
|
+
export declare const useAsync: <T extends (...args: any) => any>(fn: T, opts?: UseAsyncOptions<T>) => {
|
|
24
|
+
execute: any;
|
|
25
|
+
isLoading: any;
|
|
26
|
+
result: any;
|
|
27
|
+
error: any;
|
|
28
|
+
};
|
|
13
29
|
export {};
|
package/dist/hooks/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
2
|
+
import { useEffect, useLayoutEffect, useMemo, useTransition, useCallback, useRef, useState, } from 'react';
|
|
3
3
|
export const useClickOutside = (callback = () => alert('clicked outside')) => {
|
|
4
4
|
const ref = useRef(null);
|
|
5
5
|
const listener = (e) => {
|
|
@@ -51,12 +51,10 @@ export function useMediaQuery(tailwindBreakpoint) {
|
|
|
51
51
|
return () => {
|
|
52
52
|
matchMedia.removeEventListener('change', handleChange);
|
|
53
53
|
};
|
|
54
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
54
|
}, [parsedQuery]);
|
|
56
55
|
return matches;
|
|
57
56
|
}
|
|
58
57
|
export function useEffectOnce(effect) {
|
|
59
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
60
58
|
useEffect(effect, []);
|
|
61
59
|
}
|
|
62
60
|
export function useUpdateEffect(effect, deps) {
|
|
@@ -146,3 +144,43 @@ export const useUrlParams = (key, defaultValue) => {
|
|
|
146
144
|
};
|
|
147
145
|
return [value, updateValue];
|
|
148
146
|
};
|
|
147
|
+
export const useAsync = (fn, opts = {}) => {
|
|
148
|
+
const { initialArgs, callback = {}, mode = "onTrigger" } = opts;
|
|
149
|
+
const { onSuccess, onError, onExecute, onSettle } = callback;
|
|
150
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
151
|
+
const [result, setValue] = useState(null);
|
|
152
|
+
const [error, setError] = useState(null);
|
|
153
|
+
const [isPending, startTransition] = useTransition();
|
|
154
|
+
const execute = useCallback(async (args) => {
|
|
155
|
+
setIsLoading(true);
|
|
156
|
+
setValue(null);
|
|
157
|
+
setError(null);
|
|
158
|
+
onExecute?.();
|
|
159
|
+
try {
|
|
160
|
+
startTransition(async () => {
|
|
161
|
+
const response = await fn(args);
|
|
162
|
+
setValue(response);
|
|
163
|
+
onSuccess?.(response);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
setError(error);
|
|
168
|
+
onError?.(error);
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
setIsLoading(false);
|
|
172
|
+
onSettle?.();
|
|
173
|
+
}
|
|
174
|
+
}, [fn, onExecute, onSuccess, onError, onSettle]);
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
if (mode === 'onLoad') {
|
|
177
|
+
execute(initialArgs);
|
|
178
|
+
}
|
|
179
|
+
}, []);
|
|
180
|
+
return {
|
|
181
|
+
execute,
|
|
182
|
+
isLoading: isLoading || isPending,
|
|
183
|
+
result,
|
|
184
|
+
error,
|
|
185
|
+
};
|
|
186
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sohanemon/utils",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.17",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
},
|
|
15
15
|
"typesVersions": {
|
|
16
16
|
"*": {
|
|
17
|
+
"core": [
|
|
18
|
+
"dist/index.d.ts"
|
|
19
|
+
],
|
|
17
20
|
"hooks": [
|
|
18
21
|
"dist/hooks/index.d.ts"
|
|
19
22
|
],
|