xansync 1.0.4 → 1.0.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.
package/main.tsx ADDED
@@ -0,0 +1,71 @@
1
+ import React, { useState } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+ import { useSync, withSync } from "./src";
4
+
5
+ const Async = () => {
6
+ const [id, setId] = useState(1);
7
+ const { data, isLoading, error, reload } = useSync(async () => {
8
+ return fetch(`https://jsonplaceholder.typicode.com/posts/${id}`).then(
9
+ (res) => res.json(),
10
+ );
11
+ }, [id]);
12
+
13
+ console.log(data, isLoading);
14
+
15
+ if (isLoading) return <>Loading...</>;
16
+ if (error) return <>Error: {error.message}</>;
17
+
18
+ return (
19
+ <>
20
+ <h2>{data.title}</h2>
21
+ <p>{data.body}</p>
22
+ <button onClick={() => setId(id + 1)}>Random Post</button>
23
+ </>
24
+ );
25
+ };
26
+
27
+ const AsyncPost = withSync<{ id: number }>(async ({ id }) => {
28
+ const response = await fetch(
29
+ `https://jsonplaceholder.typicode.com/posts/${id}`,
30
+ );
31
+ if (!response.ok) {
32
+ throw new Error(`Request failed (${response.status})`);
33
+ }
34
+
35
+ const post = (await response.json()) as { title: string; body: string };
36
+ return (
37
+ <article>
38
+ <h2>{post.title}</h2>
39
+ <p>{post.body}</p>
40
+ </article>
41
+ );
42
+ });
43
+
44
+ const App = () => {
45
+ const [postId, setPostId] = useState(1);
46
+ return (
47
+ <div
48
+ style={{
49
+ fontFamily: "monospace,math, sans-serif",
50
+ textAlign: "center",
51
+ marginTop: "50px",
52
+ }}
53
+ >
54
+ <div style={{ marginTop: "50px" }}>
55
+ <Async />
56
+
57
+ <h1>withSync Post</h1>
58
+ <button onClick={() => setPostId((id) => id + 1)}>
59
+ Load next post
60
+ </button>
61
+
62
+ <AsyncPost id={postId} />
63
+ </div>
64
+ </div>
65
+ );
66
+ };
67
+ const rootEle = document.getElementById("root");
68
+ if (rootEle) {
69
+ const root = createRoot(rootEle);
70
+ root.render(<App />);
71
+ }
package/package.json CHANGED
@@ -1,27 +1,40 @@
1
- {
2
- "name": "xansync",
3
- "version": "1.0.4",
4
- "main": "./index.js",
5
- "module": "./index.mjs",
6
- "types": "./index.d.ts",
7
- "homepage": "https://xansync.devnax.com",
8
- "description": "Lightweight React utilities for managing asynchronous data, loading states, and errors.",
9
- "keywords": [
10
- "react",
11
- "react-hooks",
12
- "async",
13
- "data-fetching",
14
- "useSync",
15
- "withAsync"
16
- ],
17
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/devnax/xansync.git"
20
- },
21
- "bugs": {
22
- "url": "https://github.com/devnax/xansync/issues"
23
- },
24
- "peerDependencies": {
25
- "react": ">=16.8.0"
26
- }
27
- }
1
+ {
2
+ "name": "xansync",
3
+ "version": "1.0.5",
4
+ "main": "./index.js",
5
+ "module": "./index.mjs",
6
+ "types": "./index.d.ts",
7
+ "homepage": "https://xansync.devnax.com",
8
+ "description": "Lightweight React utilities for managing asynchronous data, loading states, and errors.",
9
+ "keywords": [
10
+ "react",
11
+ "react-hooks",
12
+ "async",
13
+ "data-fetching",
14
+ "useSync",
15
+ "withAsync"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/devnax/xansync.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/devnax/xansync/issues"
23
+ },
24
+ "scripts": {
25
+ "start": "makepack start",
26
+ "build": "makepack build",
27
+ "release": "makepack release"
28
+ },
29
+ "peerDependencies": {
30
+ "react": ">=16.8.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/react": "^19.0.2",
34
+ "@types/react-dom": "^19.0.2",
35
+ "makepack": "^1.7.24",
36
+ "react": "^19.0.0",
37
+ "react-dom": "^19.0.0",
38
+ "typescript": "^4.4.2"
39
+ }
40
+ }
package/readme.md CHANGED
@@ -128,12 +128,3 @@ withSync<P extends object>(
128
128
 
129
129
  The wrapped component starts the async function after mounting and runs it again when the props object changes. It renders nothing while loading. If the promise rejects, the error is thrown during rendering so the nearest React error boundary can handle it. When props change or the component unmounts, results from the previous request are ignored; the underlying promise is not cancelled.
130
130
 
131
- ## Development
132
-
133
- ```bash
134
- npm run start
135
- npm run build
136
- npm run release
137
- ```
138
-
139
- These scripts run the corresponding `makepack` commands.
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { useSync } from "./useSync";
2
+ export { withSync } from "./withSync";
package/src/useSync.ts ADDED
@@ -0,0 +1,61 @@
1
+ "use client";
2
+ import {
3
+ Dispatch,
4
+ SetStateAction,
5
+ useCallback,
6
+ useEffect,
7
+ useRef,
8
+ useState,
9
+ } from "react";
10
+
11
+ export interface UseAsyncResult<T> {
12
+ data: T | null;
13
+ setData: Dispatch<SetStateAction<T | null>>;
14
+ isLoading: boolean;
15
+ error: Error | null;
16
+ reload: () => void;
17
+ }
18
+
19
+ export function useSync<T>(
20
+ asyncFn: () => Promise<T>,
21
+ deps: any[] = [],
22
+ ): UseAsyncResult<T> {
23
+ const [data, setData] = useState<T | null>(null);
24
+ const [isLoading, setLoading] = useState(true);
25
+ const [error, setError] = useState<Error | null>(null);
26
+ const abortRef = useRef<AbortController | null>(null);
27
+
28
+ const execute = useCallback(async () => {
29
+ abortRef.current?.abort();
30
+ const controller = new AbortController();
31
+ abortRef.current = controller;
32
+
33
+ try {
34
+ setLoading(true);
35
+ setError(null);
36
+ const result = await asyncFn();
37
+ if (!controller.signal.aborted) {
38
+ setData(result);
39
+ setLoading(false);
40
+ }
41
+ } catch (err) {
42
+ if (!controller.signal.aborted) {
43
+ setError(err instanceof Error ? err : new Error("Unknown error"));
44
+ setLoading(false);
45
+ }
46
+ }
47
+ }, deps);
48
+
49
+ useEffect(() => {
50
+ execute();
51
+ return () => abortRef.current?.abort();
52
+ }, [execute]);
53
+
54
+ return {
55
+ data,
56
+ setData: (d) => setData(d),
57
+ isLoading,
58
+ error,
59
+ reload: execute,
60
+ };
61
+ }
@@ -0,0 +1,47 @@
1
+ import { useEffect, useState } from "react";
2
+
3
+ type AsyncComponent<P> = (props: P) => Promise<React.ReactNode>;
4
+
5
+ export function withSync<P extends object>(Component: AsyncComponent<P>) {
6
+ return function SyncComponent(props: P) {
7
+ const [content, setContent] = useState<React.ReactNode>(null);
8
+ const [isLoading, setLoading] = useState(true);
9
+ const [error, setError] = useState<Error | null>(null);
10
+
11
+ useEffect(() => {
12
+ let active = true;
13
+
14
+ async function execute() {
15
+ setLoading(true);
16
+ setError(null);
17
+
18
+ try {
19
+ const result = await Component(props);
20
+
21
+ if (active) {
22
+ setContent(result);
23
+ }
24
+ } catch (err) {
25
+ if (active) {
26
+ setError(err instanceof Error ? err : new Error("Unknown error"));
27
+ }
28
+ } finally {
29
+ if (active) {
30
+ setLoading(false);
31
+ }
32
+ }
33
+ }
34
+
35
+ execute();
36
+
37
+ return () => {
38
+ active = false;
39
+ };
40
+ }, [props]);
41
+
42
+ if (isLoading) return null;
43
+ if (error) throw error;
44
+
45
+ return content;
46
+ };
47
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "lib": [
5
+ "dom",
6
+ "dom.iterable",
7
+ "esnext"
8
+ ],
9
+ "allowJs": true,
10
+ "skipLibCheck": true,
11
+ "esModuleInterop": true,
12
+ "allowSyntheticDefaultImports": true,
13
+ "strict": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "module": "esnext",
16
+ "moduleResolution": "node",
17
+ "resolveJsonModule": true,
18
+ "isolatedModules": true,
19
+ "noEmit": true,
20
+ "jsx": "react"
21
+ },
22
+ "include": [
23
+ "src"
24
+ ],
25
+ "exclude": [
26
+ "node_modules",
27
+ "build"
28
+ ]
29
+ }
package/index.cjs DELETED
@@ -1,10 +0,0 @@
1
- 'use strict';
2
-
3
- var useSync = require('./useSync.cjs');
4
- var withSync = require('./withSync.cjs');
5
-
6
-
7
-
8
- exports.useSync = useSync.useSync;
9
- exports.withSync = withSync.withSync;
10
- //# sourceMappingURL=index.cjs.map
package/index.cjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
package/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { useSync } from './useSync.js';
2
- export { withSync } from './withSync.js';
package/index.js DELETED
@@ -1,3 +0,0 @@
1
- export { useSync } from './useSync.js';
2
- export { withSync } from './withSync.js';
3
- //# sourceMappingURL=index.js.map
package/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
package/useSync.cjs DELETED
@@ -1,46 +0,0 @@
1
- "use client";
2
- 'use strict';
3
-
4
- var react = require('react');
5
-
6
- function useSync(asyncFn, deps = []) {
7
- const [data, setData] = react.useState(null);
8
- const [isLoading, setLoading] = react.useState(true);
9
- const [error, setError] = react.useState(null);
10
- const abortRef = react.useRef(null);
11
- const execute = react.useCallback(async () => {
12
- var _a;
13
- (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
14
- const controller = new AbortController();
15
- abortRef.current = controller;
16
- try {
17
- setLoading(true);
18
- setError(null);
19
- const result = await asyncFn();
20
- if (!controller.signal.aborted) {
21
- setData(result);
22
- setLoading(false);
23
- }
24
- }
25
- catch (err) {
26
- if (!controller.signal.aborted) {
27
- setError(err instanceof Error ? err : new Error("Unknown error"));
28
- setLoading(false);
29
- }
30
- }
31
- }, deps);
32
- react.useEffect(() => {
33
- execute();
34
- return () => { var _a; return (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
35
- }, [execute]);
36
- return {
37
- data,
38
- setData: (d) => setData(d),
39
- isLoading,
40
- error,
41
- reload: execute,
42
- };
43
- }
44
-
45
- exports.useSync = useSync;
46
- //# sourceMappingURL=useSync.cjs.map
package/useSync.cjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"useSync.cjs","sources":["../src/useSync.ts"],"sourcesContent":["\"use client\";\r\nimport {\r\n Dispatch,\r\n SetStateAction,\r\n useCallback,\r\n useEffect,\r\n useRef,\r\n useState,\r\n} from \"react\";\r\n\r\nexport interface UseAsyncResult<T> {\r\n data: T | null;\r\n setData: Dispatch<SetStateAction<T | null>>;\r\n isLoading: boolean;\r\n error: Error | null;\r\n reload: () => void;\r\n}\r\n\r\nexport function useSync<T>(\r\n asyncFn: () => Promise<T>,\r\n deps: any[] = [],\r\n): UseAsyncResult<T> {\r\n const [data, setData] = useState<T | null>(null);\r\n const [isLoading, setLoading] = useState(true);\r\n const [error, setError] = useState<Error | null>(null);\r\n const abortRef = useRef<AbortController | null>(null);\r\n\r\n const execute = useCallback(async () => {\r\n abortRef.current?.abort();\r\n const controller = new AbortController();\r\n abortRef.current = controller;\r\n\r\n try {\r\n setLoading(true);\r\n setError(null);\r\n const result = await asyncFn();\r\n if (!controller.signal.aborted) {\r\n setData(result);\r\n setLoading(false);\r\n }\r\n } catch (err) {\r\n if (!controller.signal.aborted) {\r\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\r\n setLoading(false);\r\n }\r\n }\r\n }, deps);\r\n\r\n useEffect(() => {\r\n execute();\r\n return () => abortRef.current?.abort();\r\n }, [execute]);\r\n\r\n return {\r\n data,\r\n setData: (d) => setData(d),\r\n isLoading,\r\n error,\r\n reload: execute,\r\n };\r\n}\r\n"],"names":[],"mappings":";;;;;;;;;AAyBE;AAEA;;AACE;AACA;AACA;;;;AAKE;AACA;;;AAGC;AACF;AAAC;AACA;AACE;;AAED;AACF;;;AAID;AACA;AACF;;;;;;AAOE;;AAEJ;;"}
package/useSync.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { Dispatch, SetStateAction } from 'react';
2
-
3
- interface UseAsyncResult<T> {
4
- data: T | null;
5
- setData: Dispatch<SetStateAction<T | null>>;
6
- isLoading: boolean;
7
- error: Error | null;
8
- reload: () => void;
9
- }
10
- declare function useSync<T>(asyncFn: () => Promise<T>, deps?: any[]): UseAsyncResult<T>;
11
-
12
- export { useSync };
13
- export type { UseAsyncResult };
package/useSync.js DELETED
@@ -1,44 +0,0 @@
1
- "use client";
2
- import { useState, useRef, useCallback, useEffect } from 'react';
3
-
4
- function useSync(asyncFn, deps = []) {
5
- const [data, setData] = useState(null);
6
- const [isLoading, setLoading] = useState(true);
7
- const [error, setError] = useState(null);
8
- const abortRef = useRef(null);
9
- const execute = useCallback(async () => {
10
- var _a;
11
- (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
12
- const controller = new AbortController();
13
- abortRef.current = controller;
14
- try {
15
- setLoading(true);
16
- setError(null);
17
- const result = await asyncFn();
18
- if (!controller.signal.aborted) {
19
- setData(result);
20
- setLoading(false);
21
- }
22
- }
23
- catch (err) {
24
- if (!controller.signal.aborted) {
25
- setError(err instanceof Error ? err : new Error("Unknown error"));
26
- setLoading(false);
27
- }
28
- }
29
- }, deps);
30
- useEffect(() => {
31
- execute();
32
- return () => { var _a; return (_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort(); };
33
- }, [execute]);
34
- return {
35
- data,
36
- setData: (d) => setData(d),
37
- isLoading,
38
- error,
39
- reload: execute,
40
- };
41
- }
42
-
43
- export { useSync };
44
- //# sourceMappingURL=useSync.js.map
package/useSync.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"useSync.js","sources":["../src/useSync.ts"],"sourcesContent":["\"use client\";\r\nimport {\r\n Dispatch,\r\n SetStateAction,\r\n useCallback,\r\n useEffect,\r\n useRef,\r\n useState,\r\n} from \"react\";\r\n\r\nexport interface UseAsyncResult<T> {\r\n data: T | null;\r\n setData: Dispatch<SetStateAction<T | null>>;\r\n isLoading: boolean;\r\n error: Error | null;\r\n reload: () => void;\r\n}\r\n\r\nexport function useSync<T>(\r\n asyncFn: () => Promise<T>,\r\n deps: any[] = [],\r\n): UseAsyncResult<T> {\r\n const [data, setData] = useState<T | null>(null);\r\n const [isLoading, setLoading] = useState(true);\r\n const [error, setError] = useState<Error | null>(null);\r\n const abortRef = useRef<AbortController | null>(null);\r\n\r\n const execute = useCallback(async () => {\r\n abortRef.current?.abort();\r\n const controller = new AbortController();\r\n abortRef.current = controller;\r\n\r\n try {\r\n setLoading(true);\r\n setError(null);\r\n const result = await asyncFn();\r\n if (!controller.signal.aborted) {\r\n setData(result);\r\n setLoading(false);\r\n }\r\n } catch (err) {\r\n if (!controller.signal.aborted) {\r\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\r\n setLoading(false);\r\n }\r\n }\r\n }, deps);\r\n\r\n useEffect(() => {\r\n execute();\r\n return () => abortRef.current?.abort();\r\n }, [execute]);\r\n\r\n return {\r\n data,\r\n setData: (d) => setData(d),\r\n isLoading,\r\n error,\r\n reload: execute,\r\n };\r\n}\r\n"],"names":[],"mappings":";;;;;;;AAyBE;AAEA;;AACE;AACA;AACA;;;;AAKE;AACA;;;AAGC;AACF;AAAC;AACA;AACE;;AAED;AACF;;;AAID;AACA;AACF;;;;;;AAOE;;AAEJ;;"}
package/withSync.cjs DELETED
@@ -1,46 +0,0 @@
1
- 'use strict';
2
-
3
- var react = require('react');
4
-
5
- function withSync(Component) {
6
- return function SyncComponent(props) {
7
- const [content, setContent] = react.useState(null);
8
- const [isLoading, setLoading] = react.useState(true);
9
- const [error, setError] = react.useState(null);
10
- react.useEffect(() => {
11
- let active = true;
12
- async function execute() {
13
- setLoading(true);
14
- setError(null);
15
- try {
16
- const result = await Component(props);
17
- if (active) {
18
- setContent(result);
19
- }
20
- }
21
- catch (err) {
22
- if (active) {
23
- setError(err instanceof Error ? err : new Error("Unknown error"));
24
- }
25
- }
26
- finally {
27
- if (active) {
28
- setLoading(false);
29
- }
30
- }
31
- }
32
- execute();
33
- return () => {
34
- active = false;
35
- };
36
- }, [props]);
37
- if (isLoading)
38
- return null;
39
- if (error)
40
- throw error;
41
- return content;
42
- };
43
- }
44
-
45
- exports.withSync = withSync;
46
- //# sourceMappingURL=withSync.cjs.map
package/withSync.cjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"withSync.cjs","sources":["../src/withSync.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\r\n\r\ntype AsyncComponent<P> = (props: P) => Promise<React.ReactNode>;\r\n\r\nexport function withSync<P extends object>(Component: AsyncComponent<P>) {\r\n return function SyncComponent(props: P) {\r\n const [content, setContent] = useState<React.ReactNode>(null);\r\n const [isLoading, setLoading] = useState(true);\r\n const [error, setError] = useState<Error | null>(null);\r\n\r\n useEffect(() => {\r\n let active = true;\r\n\r\n async function execute() {\r\n setLoading(true);\r\n setError(null);\r\n\r\n try {\r\n const result = await Component(props);\r\n\r\n if (active) {\r\n setContent(result);\r\n }\r\n } catch (err) {\r\n if (active) {\r\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\r\n }\r\n } finally {\r\n if (active) {\r\n setLoading(false);\r\n }\r\n }\r\n }\r\n\r\n execute();\r\n\r\n return () => {\r\n active = false;\r\n };\r\n }, [props]);\r\n\r\n if (isLoading) return null;\r\n if (error) throw error;\r\n\r\n return content;\r\n };\r\n}\r\n"],"names":["useState","useEffect"],"mappings":";;;;AAIM,SAAU,QAAQ,CAAmB,SAA4B,EAAA;IACrE,OAAO,SAAS,aAAa,CAAC,KAAQ,EAAA;QACpC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAGA,cAAQ,CAAkB,IAAI,CAAC,CAAC;QAC9D,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAGA,cAAQ,CAAC,IAAI,CAAC,CAAC;QAC/C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAGA,cAAQ,CAAe,IAAI,CAAC,CAAC;QAEvDC,eAAS,CAAC,MAAK;YACb,IAAI,MAAM,GAAG,IAAI,CAAC;AAElB,YAAA,eAAe,OAAO,GAAA;gBACpB,UAAU,CAAC,IAAI,CAAC,CAAC;gBACjB,QAAQ,CAAC,IAAI,CAAC,CAAC;gBAEf,IAAI;AACF,oBAAA,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,KAAK,CAAC,CAAC;AAEtC,oBAAA,IAAI,MAAM,EAAE;wBACV,UAAU,CAAC,MAAM,CAAC,CAAC;AACpB,oBAAA,CAAA;AACF,gBAAA,CAAA;AAAC,gBAAA,OAAO,GAAG,EAAE;AACZ,oBAAA,IAAI,MAAM,EAAE;AACV,wBAAA,QAAQ,CAAC,GAAG,YAAY,KAAK,GAAG,GAAG,GAAG,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;AACnE,oBAAA,CAAA;AACF,gBAAA,CAAA;AAAS,wBAAA;AACR,oBAAA,IAAI,MAAM,EAAE;wBACV,UAAU,CAAC,KAAK,CAAC,CAAC;AACnB,oBAAA,CAAA;AACF,gBAAA,CAAA;YACH,CAAC;AAED,YAAA,OAAO,EAAE,CAAC;AAEV,YAAA,OAAO,MAAK;gBACV,MAAM,GAAG,KAAK,CAAC;AACjB,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AAEZ,QAAA,IAAI,SAAS;AAAE,YAAA,OAAO,IAAI,CAAC;AAC3B,QAAA,IAAI,KAAK;AAAE,YAAA,MAAM,KAAK,CAAC;AAEvB,QAAA,OAAO,OAAO,CAAC;AACjB,IAAA,CAAC,CAAC;AACJ;;"}
package/withSync.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import * as react from 'react';
2
-
3
- type AsyncComponent<P> = (props: P) => Promise<React.ReactNode>;
4
- declare function withSync<P extends object>(Component: AsyncComponent<P>): (props: P) => react.ReactNode;
5
-
6
- export { withSync };
package/withSync.js DELETED
@@ -1,44 +0,0 @@
1
- import { useState, useEffect } from 'react';
2
-
3
- function withSync(Component) {
4
- return function SyncComponent(props) {
5
- const [content, setContent] = useState(null);
6
- const [isLoading, setLoading] = useState(true);
7
- const [error, setError] = useState(null);
8
- useEffect(() => {
9
- let active = true;
10
- async function execute() {
11
- setLoading(true);
12
- setError(null);
13
- try {
14
- const result = await Component(props);
15
- if (active) {
16
- setContent(result);
17
- }
18
- }
19
- catch (err) {
20
- if (active) {
21
- setError(err instanceof Error ? err : new Error("Unknown error"));
22
- }
23
- }
24
- finally {
25
- if (active) {
26
- setLoading(false);
27
- }
28
- }
29
- }
30
- execute();
31
- return () => {
32
- active = false;
33
- };
34
- }, [props]);
35
- if (isLoading)
36
- return null;
37
- if (error)
38
- throw error;
39
- return content;
40
- };
41
- }
42
-
43
- export { withSync };
44
- //# sourceMappingURL=withSync.js.map
package/withSync.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"withSync.js","sources":["../src/withSync.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\r\n\r\ntype AsyncComponent<P> = (props: P) => Promise<React.ReactNode>;\r\n\r\nexport function withSync<P extends object>(Component: AsyncComponent<P>) {\r\n return function SyncComponent(props: P) {\r\n const [content, setContent] = useState<React.ReactNode>(null);\r\n const [isLoading, setLoading] = useState(true);\r\n const [error, setError] = useState<Error | null>(null);\r\n\r\n useEffect(() => {\r\n let active = true;\r\n\r\n async function execute() {\r\n setLoading(true);\r\n setError(null);\r\n\r\n try {\r\n const result = await Component(props);\r\n\r\n if (active) {\r\n setContent(result);\r\n }\r\n } catch (err) {\r\n if (active) {\r\n setError(err instanceof Error ? err : new Error(\"Unknown error\"));\r\n }\r\n } finally {\r\n if (active) {\r\n setLoading(false);\r\n }\r\n }\r\n }\r\n\r\n execute();\r\n\r\n return () => {\r\n active = false;\r\n };\r\n }, [props]);\r\n\r\n if (isLoading) return null;\r\n if (error) throw error;\r\n\r\n return content;\r\n };\r\n}\r\n"],"names":[],"mappings":";;AAIM,SAAU,QAAQ,CAAmB,SAA4B,EAAA;IACrE,OAAO,SAAS,aAAa,CAAC,KAAQ,EAAA;QACpC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAkB,IAAI,CAAC,CAAC;QAC9D,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC/C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;QAEvD,SAAS,CAAC,MAAK;YACb,IAAI,MAAM,GAAG,IAAI,CAAC;AAElB,YAAA,eAAe,OAAO,GAAA;gBACpB,UAAU,CAAC,IAAI,CAAC,CAAC;gBACjB,QAAQ,CAAC,IAAI,CAAC,CAAC;gBAEf,IAAI;AACF,oBAAA,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,KAAK,CAAC,CAAC;AAEtC,oBAAA,IAAI,MAAM,EAAE;wBACV,UAAU,CAAC,MAAM,CAAC,CAAC;AACpB,oBAAA,CAAA;AACF,gBAAA,CAAA;AAAC,gBAAA,OAAO,GAAG,EAAE;AACZ,oBAAA,IAAI,MAAM,EAAE;AACV,wBAAA,QAAQ,CAAC,GAAG,YAAY,KAAK,GAAG,GAAG,GAAG,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;AACnE,oBAAA,CAAA;AACF,gBAAA,CAAA;AAAS,wBAAA;AACR,oBAAA,IAAI,MAAM,EAAE;wBACV,UAAU,CAAC,KAAK,CAAC,CAAC;AACnB,oBAAA,CAAA;AACF,gBAAA,CAAA;YACH,CAAC;AAED,YAAA,OAAO,EAAE,CAAC;AAEV,YAAA,OAAO,MAAK;gBACV,MAAM,GAAG,KAAK,CAAC;AACjB,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AAEZ,QAAA,IAAI,SAAS;AAAE,YAAA,OAAO,IAAI,CAAC;AAC3B,QAAA,IAAI,KAAK;AAAE,YAAA,MAAM,KAAK,CAAC;AAEvB,QAAA,OAAO,OAAO,CAAC;AACjB,IAAA,CAAC,CAAC;AACJ;;"}