xansync 1.0.0 → 1.0.1
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/index.d.ts +2 -0
- package/index.js +10 -2
- package/index.js.map +1 -1
- package/index.mjs +10 -2
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/readme.md +12 -12
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -18,7 +18,9 @@ function useSync(asyncFn, deps = []) {
|
|
|
18
18
|
try {
|
|
19
19
|
const result = await asyncFn();
|
|
20
20
|
if (!controller.signal.aborted) {
|
|
21
|
-
|
|
21
|
+
setTimeout(() => {
|
|
22
|
+
setData(result);
|
|
23
|
+
}, 1);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
catch (err) {
|
|
@@ -36,7 +38,13 @@ function useSync(asyncFn, deps = []) {
|
|
|
36
38
|
execute();
|
|
37
39
|
return () => { var _a; return (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
|
|
38
40
|
}, [execute]);
|
|
39
|
-
return {
|
|
41
|
+
return {
|
|
42
|
+
data,
|
|
43
|
+
setData: (d) => setData(d),
|
|
44
|
+
isLoading,
|
|
45
|
+
error,
|
|
46
|
+
reload: execute
|
|
47
|
+
};
|
|
40
48
|
}
|
|
41
49
|
function withAsync(loader, render, fallback, onError) {
|
|
42
50
|
return function AsyncComponent() {
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport interface UseAsyncResult<T> {\n data: T | null;\n isLoading: boolean;\n error: Error | null;\n reload: () => void;\n}\n\nexport function useSync<T>(asyncFn: () => Promise<T>, deps: any[] = []): UseAsyncResult<T> {\n const [data, setData] = useState<T | null>(null);\n const [isLoading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const abortRef = useRef<AbortController | null>(null);\n\n const execute = useCallback(async () => {\n abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n\n setLoading(true);\n setError(null);\n\n try {\n const result = await asyncFn();\n if (!controller.signal.aborted) {\n setData(result);\n }\n } catch (err) {\n if (!controller.signal.aborted) {\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\n }\n } finally {\n if (!controller.signal.aborted) {\n setLoading(false);\n }\n }\n }, deps);\n\n useEffect(() => {\n execute();\n return () => abortRef.current?.abort();\n }, [execute]);\n\n return {
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["\"use client\";\nimport { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from \"react\";\n\nexport interface UseAsyncResult<T> {\n data: T | null;\n setData: Dispatch<SetStateAction<T | null>>\n isLoading: boolean;\n error: Error | null;\n reload: () => void;\n}\n\nexport function useSync<T>(asyncFn: () => Promise<T>, deps: any[] = []): UseAsyncResult<T> {\n const [data, setData] = useState<T | null>(null);\n const [isLoading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const abortRef = useRef<AbortController | null>(null);\n\n const execute = useCallback(async () => {\n abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n\n setLoading(true);\n setError(null);\n\n try {\n const result = await asyncFn();\n if (!controller.signal.aborted) {\n setTimeout(() => {\n setData(result);\n }, 1);\n }\n } catch (err) {\n if (!controller.signal.aborted) {\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\n }\n } finally {\n if (!controller.signal.aborted) {\n setLoading(false);\n }\n }\n }, deps);\n\n useEffect(() => {\n execute();\n return () => abortRef.current?.abort();\n }, [execute]);\n\n return {\n data,\n setData: (d) => setData(d),\n isLoading,\n error,\n reload: execute\n };\n}\n\ntype AsyncRenderer<T> = (data: T) => React.ReactNode;\n\nexport function withAsync<T>(\n loader: () => Promise<T>,\n render: AsyncRenderer<T>,\n fallback?: React.ReactNode,\n onError?: (error: Error) => React.ReactNode\n) {\n return function AsyncComponent() {\n const { data, isLoading, error } = useSync(loader, []);\n\n if (isLoading) return fallback ?? null;\n if (error) return onError ? onError(error) : null;\n if (data == null) return null;\n\n return render(data);\n };\n}\n"],"names":[],"mappings":";;;;;;;;;AAeE;AAEA;;AACE;AACA;AACA;;;;AAME;AACA;;;;AAIC;AACF;AAAC;AACA;AACE;AACD;AACF;AAAS;AACR;;AAEC;AACF;;;AAID;AACA;AACF;;;;;;AAOE;;AAEJ;AAIM;AAMJ;AACE;AAEA;AAAe;AACf;AAAW;;AACO;AAElB;AACF;AACF;;;"}
|
package/index.mjs
CHANGED
|
@@ -16,7 +16,9 @@ function useSync(asyncFn, deps = []) {
|
|
|
16
16
|
try {
|
|
17
17
|
const result = await asyncFn();
|
|
18
18
|
if (!controller.signal.aborted) {
|
|
19
|
-
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
setData(result);
|
|
21
|
+
}, 1);
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
catch (err) {
|
|
@@ -34,7 +36,13 @@ function useSync(asyncFn, deps = []) {
|
|
|
34
36
|
execute();
|
|
35
37
|
return () => { var _a; return (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
|
|
36
38
|
}, [execute]);
|
|
37
|
-
return {
|
|
39
|
+
return {
|
|
40
|
+
data,
|
|
41
|
+
setData: (d) => setData(d),
|
|
42
|
+
isLoading,
|
|
43
|
+
error,
|
|
44
|
+
reload: execute
|
|
45
|
+
};
|
|
38
46
|
}
|
|
39
47
|
function withAsync(loader, render, fallback, onError) {
|
|
40
48
|
return function AsyncComponent() {
|
package/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport interface UseAsyncResult<T> {\n data: T | null;\n isLoading: boolean;\n error: Error | null;\n reload: () => void;\n}\n\nexport function useSync<T>(asyncFn: () => Promise<T>, deps: any[] = []): UseAsyncResult<T> {\n const [data, setData] = useState<T | null>(null);\n const [isLoading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const abortRef = useRef<AbortController | null>(null);\n\n const execute = useCallback(async () => {\n abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n\n setLoading(true);\n setError(null);\n\n try {\n const result = await asyncFn();\n if (!controller.signal.aborted) {\n setData(result);\n }\n } catch (err) {\n if (!controller.signal.aborted) {\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\n }\n } finally {\n if (!controller.signal.aborted) {\n setLoading(false);\n }\n }\n }, deps);\n\n useEffect(() => {\n execute();\n return () => abortRef.current?.abort();\n }, [execute]);\n\n return {
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["\"use client\";\nimport { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from \"react\";\n\nexport interface UseAsyncResult<T> {\n data: T | null;\n setData: Dispatch<SetStateAction<T | null>>\n isLoading: boolean;\n error: Error | null;\n reload: () => void;\n}\n\nexport function useSync<T>(asyncFn: () => Promise<T>, deps: any[] = []): UseAsyncResult<T> {\n const [data, setData] = useState<T | null>(null);\n const [isLoading, setLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const abortRef = useRef<AbortController | null>(null);\n\n const execute = useCallback(async () => {\n abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n\n setLoading(true);\n setError(null);\n\n try {\n const result = await asyncFn();\n if (!controller.signal.aborted) {\n setTimeout(() => {\n setData(result);\n }, 1);\n }\n } catch (err) {\n if (!controller.signal.aborted) {\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\n }\n } finally {\n if (!controller.signal.aborted) {\n setLoading(false);\n }\n }\n }, deps);\n\n useEffect(() => {\n execute();\n return () => abortRef.current?.abort();\n }, [execute]);\n\n return {\n data,\n setData: (d) => setData(d),\n isLoading,\n error,\n reload: execute\n };\n}\n\ntype AsyncRenderer<T> = (data: T) => React.ReactNode;\n\nexport function withAsync<T>(\n loader: () => Promise<T>,\n render: AsyncRenderer<T>,\n fallback?: React.ReactNode,\n onError?: (error: Error) => React.ReactNode\n) {\n return function AsyncComponent() {\n const { data, isLoading, error } = useSync(loader, []);\n\n if (isLoading) return fallback ?? null;\n if (error) return onError ? onError(error) : null;\n if (data == null) return null;\n\n return render(data);\n };\n}\n"],"names":[],"mappings":";;;;;;;AAeE;AAEA;;AACE;AACA;AACA;;;;AAME;AACA;;;;AAIC;AACF;AAAC;AACA;AACE;AACD;AACF;AAAS;AACR;;AAEC;AACF;;;AAID;AACA;AACF;;;;;;AAOE;;AAEJ;AAIM;AAMJ;AACE;AAEA;AAAe;AACf;AAAW;;AACO;AAElB;AACF;AACF;;"}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
Here's the updated documentation with the correct package name:
|
|
2
2
|
|
|
3
|
-
# `
|
|
3
|
+
# `xansync` - React Hook and HOC for Handling Asynchronous Operations
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
`
|
|
7
|
+
`xansync` provides two utility functions for handling asynchronous operations in React: `useSync` and `withAsync`. These hooks simplify the management of asynchronous data fetching, error handling, and reloading logic within React components.
|
|
8
8
|
|
|
9
9
|
### Features
|
|
10
10
|
|
|
11
|
-
- **`
|
|
11
|
+
- **`useSync`**: A hook that handles asynchronous operations, loading states, and error handling within a component.
|
|
12
12
|
- **`withAsync`**: A higher-order component (HOC) that wraps a component to handle asynchronous operations with built-in loading and error fallback UI.
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
@@ -16,17 +16,17 @@ Here's the updated documentation with the correct package name:
|
|
|
16
16
|
To install the package, use npm or yarn:
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
|
-
npm install
|
|
19
|
+
npm install xansync
|
|
20
20
|
# or
|
|
21
|
-
yarn add
|
|
21
|
+
yarn add xansync
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
## API Documentation
|
|
25
25
|
|
|
26
|
-
### `
|
|
26
|
+
### `useSync`
|
|
27
27
|
|
|
28
28
|
```ts
|
|
29
|
-
const { data, isLoading, error, reload } =
|
|
29
|
+
const { data, isLoading, error, reload } = useSync<T>(promise: Promise<T>, dependencies: any[] = []): useSyncResult<T>
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
#### Parameters
|
|
@@ -36,7 +36,7 @@ const { data, isLoading, error, reload } = useAsync<T>(promise: Promise<T>, depe
|
|
|
36
36
|
|
|
37
37
|
#### Returns
|
|
38
38
|
|
|
39
|
-
The `
|
|
39
|
+
The `useSync` hook returns an object with the following properties:
|
|
40
40
|
|
|
41
41
|
- **`data`** (`T | null`): The result of the asynchronous operation. If the operation hasn't completed yet, this will be `null`.
|
|
42
42
|
- **`isLoading`** (`boolean`): A flag indicating whether the asynchronous operation is in progress.
|
|
@@ -47,7 +47,7 @@ The `useAsync` hook returns an object with the following properties:
|
|
|
47
47
|
|
|
48
48
|
```tsx
|
|
49
49
|
const MyComponent = () => {
|
|
50
|
-
const { data, isLoading, error, reload } =
|
|
50
|
+
const { data, isLoading, error, reload } = useSync(fetch(`https://jsonplaceholder.typicode.com/posts/${id}`), []);
|
|
51
51
|
|
|
52
52
|
if (isLoading) return <div>Loading...</div>;
|
|
53
53
|
if (error) return <div>Error: {error.message}</div>;
|
|
@@ -95,11 +95,11 @@ const App = () => {
|
|
|
95
95
|
|
|
96
96
|
## Example Usage
|
|
97
97
|
|
|
98
|
-
### Example 1: Using `
|
|
98
|
+
### Example 1: Using `useSync` in a Component
|
|
99
99
|
|
|
100
100
|
```tsx
|
|
101
101
|
const FetchDataComponent = () => {
|
|
102
|
-
const { data, isLoading, error, reload } =
|
|
102
|
+
const { data, isLoading, error, reload } = useSync(fetch(`https://jsonplaceholder.typicode.com/posts/${id}`), []);
|
|
103
103
|
|
|
104
104
|
if (isLoading) return <div>Loading...</div>;
|
|
105
105
|
if (error) return <div>Error: {error.message}</div>;
|
|
@@ -134,4 +134,4 @@ This package is open-source and available under the MIT License.
|
|
|
134
134
|
|
|
135
135
|
---
|
|
136
136
|
|
|
137
|
-
You can now publish this documentation along with your package `
|
|
137
|
+
You can now publish this documentation along with your package `xansync` on npm!
|