xansync 1.0.0 → 1.0.2

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 CHANGED
@@ -1,7 +1,9 @@
1
1
  import * as react from 'react';
2
+ import { Dispatch, SetStateAction } from 'react';
2
3
 
3
4
  interface UseAsyncResult<T> {
4
5
  data: T | null;
6
+ setData: Dispatch<SetStateAction<T | null>>;
5
7
  isLoading: boolean;
6
8
  error: Error | null;
7
9
  reload: () => void;
package/index.js CHANGED
@@ -5,7 +5,7 @@ var react = require('react');
5
5
 
6
6
  function useSync(asyncFn, deps = []) {
7
7
  const [data, setData] = react.useState(null);
8
- const [isLoading, setLoading] = react.useState(false);
8
+ const [isLoading, setLoading] = react.useState(true);
9
9
  const [error, setError] = react.useState(null);
10
10
  const abortRef = react.useRef(null);
11
11
  const execute = react.useCallback(async () => {
@@ -13,12 +13,13 @@ function useSync(asyncFn, deps = []) {
13
13
  (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
14
14
  const controller = new AbortController();
15
15
  abortRef.current = controller;
16
- setLoading(true);
17
- setError(null);
18
16
  try {
17
+ setLoading(true);
18
+ setError(null);
19
19
  const result = await asyncFn();
20
20
  if (!controller.signal.aborted) {
21
21
  setData(result);
22
+ setLoading(false);
22
23
  }
23
24
  }
24
25
  catch (err) {
@@ -26,17 +27,18 @@ function useSync(asyncFn, deps = []) {
26
27
  setError(err instanceof Error ? err : new Error("Unknown error"));
27
28
  }
28
29
  }
29
- finally {
30
- if (!controller.signal.aborted) {
31
- setLoading(false);
32
- }
33
- }
34
30
  }, deps);
35
31
  react.useEffect(() => {
36
32
  execute();
37
33
  return () => { var _a; return (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
38
34
  }, [execute]);
39
- return { data, isLoading, error, reload: execute };
35
+ return {
36
+ data,
37
+ setData: (d) => setData(d),
38
+ isLoading,
39
+ error,
40
+ reload: execute
41
+ };
40
42
  }
41
43
  function withAsync(loader, render, fallback, onError) {
42
44
  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 { data, isLoading, error, reload: execute };\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":";;;;;;;;;AAcE;AAEA;;AACE;AACA;AACA;;;;AAME;AACA;;AAEC;AACF;AAAC;AACA;AACE;AACD;AACF;AAAS;AACR;;AAEC;AACF;;;AAID;AACA;AACF;;AAGF;AAIM;AAMJ;AACE;AAEA;AAAe;AACf;AAAW;;AACO;AAElB;AACF;AACF;;;"}
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(true);\n const [error, setError] = useState<Error | null>(null);\n const abortRef = useRef<AbortController | null>(null);\n\n const execute = useCallback(async () => {\n\n abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n\n try {\n setLoading(true);\n setError(null);\n const result = await asyncFn();\n if (!controller.signal.aborted) {\n setData(result);\n setLoading(false);\n }\n } catch (err) {\n if (!controller.signal.aborted) {\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\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;;AAEE;AACA;AACA;;;;AAKE;AACA;;;AAGC;AACF;AAAC;AACA;AACE;AACD;AACF;;;AAID;AACA;AACF;;;;;;AAOE;;AAEJ;AAIM;AAMJ;AACE;AAEA;AAAe;AACf;AAAW;;AACO;AAElB;AACF;AACF;;;"}
package/index.mjs CHANGED
@@ -3,7 +3,7 @@ import { useState, useRef, useCallback, useEffect } from 'react';
3
3
 
4
4
  function useSync(asyncFn, deps = []) {
5
5
  const [data, setData] = useState(null);
6
- const [isLoading, setLoading] = useState(false);
6
+ const [isLoading, setLoading] = useState(true);
7
7
  const [error, setError] = useState(null);
8
8
  const abortRef = useRef(null);
9
9
  const execute = useCallback(async () => {
@@ -11,12 +11,13 @@ function useSync(asyncFn, deps = []) {
11
11
  (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
12
12
  const controller = new AbortController();
13
13
  abortRef.current = controller;
14
- setLoading(true);
15
- setError(null);
16
14
  try {
15
+ setLoading(true);
16
+ setError(null);
17
17
  const result = await asyncFn();
18
18
  if (!controller.signal.aborted) {
19
19
  setData(result);
20
+ setLoading(false);
20
21
  }
21
22
  }
22
23
  catch (err) {
@@ -24,17 +25,18 @@ function useSync(asyncFn, deps = []) {
24
25
  setError(err instanceof Error ? err : new Error("Unknown error"));
25
26
  }
26
27
  }
27
- finally {
28
- if (!controller.signal.aborted) {
29
- setLoading(false);
30
- }
31
- }
32
28
  }, deps);
33
29
  useEffect(() => {
34
30
  execute();
35
31
  return () => { var _a; return (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
36
32
  }, [execute]);
37
- return { data, isLoading, error, reload: execute };
33
+ return {
34
+ data,
35
+ setData: (d) => setData(d),
36
+ isLoading,
37
+ error,
38
+ reload: execute
39
+ };
38
40
  }
39
41
  function withAsync(loader, render, fallback, onError) {
40
42
  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 { data, isLoading, error, reload: execute };\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":";;;;;;;AAcE;AAEA;;AACE;AACA;AACA;;;;AAME;AACA;;AAEC;AACF;AAAC;AACA;AACE;AACD;AACF;AAAS;AACR;;AAEC;AACF;;;AAID;AACA;AACF;;AAGF;AAIM;AAMJ;AACE;AAEA;AAAe;AACf;AAAW;;AACO;AAElB;AACF;AACF;;"}
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(true);\n const [error, setError] = useState<Error | null>(null);\n const abortRef = useRef<AbortController | null>(null);\n\n const execute = useCallback(async () => {\n\n abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n\n try {\n setLoading(true);\n setError(null);\n const result = await asyncFn();\n if (!controller.signal.aborted) {\n setData(result);\n setLoading(false);\n }\n } catch (err) {\n if (!controller.signal.aborted) {\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\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;;AAEE;AACA;AACA;;;;AAKE;AACA;;;AAGC;AACF;AAAC;AACA;AACE;AACD;AACF;;;AAID;AACA;AACF;;;;;;AAOE;;AAEJ;AAIM;AAMJ;AACE;AAEA;AAAe;AACf;AAAW;;AACO;AAElB;AACF;AACF;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xansync",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "./index.js",
5
5
  "module": "./index.mjs",
6
6
  "types": "./index.d.ts",
package/readme.md CHANGED
@@ -1,14 +1,14 @@
1
1
  Here's the updated documentation with the correct package name:
2
2
 
3
- # `react-tsync` - React Hook and HOC for Handling Asynchronous Operations
3
+ # `xansync` - React Hook and HOC for Handling Asynchronous Operations
4
4
 
5
5
  ## Overview
6
6
 
7
- `react-tsync` provides two utility functions for handling asynchronous operations in React: `useAsync` and `withAsync`. These hooks simplify the management of asynchronous data fetching, error handling, and reloading logic within React components.
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
- - **`useAsync`**: A hook that handles asynchronous operations, loading states, and error handling within a component.
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 react-tsync
19
+ npm install xansync
20
20
  # or
21
- yarn add react-tsync
21
+ yarn add xansync
22
22
  ```
23
23
 
24
24
  ## API Documentation
25
25
 
26
- ### `useAsync`
26
+ ### `useSync`
27
27
 
28
28
  ```ts
29
- const { data, isLoading, error, reload } = useAsync<T>(promise: Promise<T>, dependencies: any[] = []): UseAsyncResult<T>
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 `useAsync` hook returns an object with the following properties:
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 } = useAsync(fetch(`https://jsonplaceholder.typicode.com/posts/${id}`), []);
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 `useAsync` in a Component
98
+ ### Example 1: Using `useSync` in a Component
99
99
 
100
100
  ```tsx
101
101
  const FetchDataComponent = () => {
102
- const { data, isLoading, error, reload } = useAsync(fetch(`https://jsonplaceholder.typicode.com/posts/${id}`), []);
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 `react-tsync` on npm!
137
+ You can now publish this documentation along with your package `xansync` on npm!