@xyo-network/react-shared 2.54.1 → 2.55.0
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/cjs/hooks/usePromise.js +19 -13
- package/dist/cjs/hooks/usePromise.js.map +1 -1
- package/dist/docs.json +290 -290
- package/dist/esm/hooks/usePromise.js +31 -25
- package/dist/esm/hooks/usePromise.js.map +1 -1
- package/dist/types/hooks/usePromise.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/hooks/usePromise.ts +31 -26
|
@@ -10,7 +10,6 @@ export var State;
|
|
|
10
10
|
* usePromise -
|
|
11
11
|
*/
|
|
12
12
|
export const usePromise = (promise, dependencies = [], debug = undefined) => {
|
|
13
|
-
const promiseMemo = useMemo(() => promise?.(), dependencies);
|
|
14
13
|
const defaultState = {
|
|
15
14
|
error: undefined,
|
|
16
15
|
result: undefined,
|
|
@@ -37,34 +36,43 @@ export const usePromise = (promise, dependencies = [], debug = undefined) => {
|
|
|
37
36
|
}
|
|
38
37
|
};
|
|
39
38
|
const [{ error, result, state }, dispatch] = useReducer(reducer, defaultState);
|
|
39
|
+
const promiseMemo = useMemo(() => {
|
|
40
|
+
try {
|
|
41
|
+
return promise?.();
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
dispatch({
|
|
45
|
+
error: e,
|
|
46
|
+
payload: undefined,
|
|
47
|
+
type: State.rejected,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}, dependencies);
|
|
40
51
|
if (debug)
|
|
41
52
|
console.debug(`usePromise [${debug}] Main Function`);
|
|
42
53
|
useEffect(() => {
|
|
43
|
-
let cancelled = false;
|
|
44
54
|
dispatch({ type: State.pending });
|
|
45
55
|
if (debug)
|
|
46
|
-
console.debug(`usePromise [${debug}] useEffect
|
|
56
|
+
console.debug(`usePromise [${debug}] useEffect`);
|
|
47
57
|
if (promiseMemo instanceof Promise) {
|
|
48
58
|
promiseMemo
|
|
49
59
|
.then((payload) => {
|
|
50
60
|
if (debug)
|
|
51
|
-
console.debug(`usePromise [${debug}] then
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
61
|
+
console.debug(`usePromise [${debug}] then`);
|
|
62
|
+
dispatch({
|
|
63
|
+
error: undefined,
|
|
64
|
+
payload,
|
|
65
|
+
type: State.resolved,
|
|
66
|
+
});
|
|
58
67
|
})
|
|
59
68
|
.catch((error) => {
|
|
60
69
|
if (debug)
|
|
61
|
-
console.debug(`usePromise [${debug}] catch
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
});
|
|
70
|
+
console.debug(`usePromise [${debug}] catch`);
|
|
71
|
+
dispatch({
|
|
72
|
+
error: error,
|
|
73
|
+
payload: undefined,
|
|
74
|
+
type: State.rejected,
|
|
75
|
+
});
|
|
68
76
|
});
|
|
69
77
|
}
|
|
70
78
|
else if (promiseMemo) {
|
|
@@ -76,18 +84,16 @@ export const usePromise = (promise, dependencies = [], debug = undefined) => {
|
|
|
76
84
|
}
|
|
77
85
|
else {
|
|
78
86
|
if (debug)
|
|
79
|
-
console.debug(`usePromise [${debug}] no-promise
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
87
|
+
console.debug(`usePromise [${debug}] no-promise`);
|
|
88
|
+
dispatch({
|
|
89
|
+
error: undefined,
|
|
90
|
+
payload: undefined,
|
|
91
|
+
type: State.resolved,
|
|
92
|
+
});
|
|
86
93
|
}
|
|
87
94
|
return () => {
|
|
88
95
|
if (debug)
|
|
89
96
|
console.debug(`usePromise [${debug}] useEffect callback`);
|
|
90
|
-
cancelled = true;
|
|
91
97
|
};
|
|
92
98
|
}, [promiseMemo]);
|
|
93
99
|
return [result, error, state];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePromise.js","sourceRoot":"","sources":["../../../src/hooks/usePromise.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAG9D,OAAO,EAA2B,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAE/E,MAAM,CAAN,IAAY,KAIX;AAJD,WAAY,KAAK;IACf,4BAAmB,CAAA;IACnB,8BAAqB,CAAA;IACrB,8BAAqB,CAAA;AACvB,CAAC,EAJW,KAAK,KAAL,KAAK,QAIhB;AAUD;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,OAA8C,EAC9C,eAA+B,EAAE,EACjC,QAA4B,SAAS,EACwB,EAAE;IAC/D,MAAM,
|
|
1
|
+
{"version":3,"file":"usePromise.js","sourceRoot":"","sources":["../../../src/hooks/usePromise.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAG9D,OAAO,EAA2B,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAE/E,MAAM,CAAN,IAAY,KAIX;AAJD,WAAY,KAAK;IACf,4BAAmB,CAAA;IACnB,8BAAqB,CAAA;IACrB,8BAAqB,CAAA;AACvB,CAAC,EAJW,KAAK,KAAL,KAAK,QAIhB;AAUD;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,OAA8C,EAC9C,eAA+B,EAAE,EACjC,QAA4B,SAAS,EACwB,EAAE;IAC/D,MAAM,YAAY,GAA0B;QAC1C,KAAK,EAAE,SAAS;QAChB,MAAM,EAAE,SAAS;QACjB,KAAK,EAAE,KAAK,CAAC,OAAO;KACrB,CAAA;IAED,MAAM,OAAO,GAAoD,CAAC,MAA6B,EAAE,MAAuB,EAAE,EAAE;QAC1H,QAAQ,MAAM,CAAC,IAAI,EAAE;YACnB,KAAK,KAAK,CAAC,OAAO;gBAChB,OAAO,YAAY,CAAA;YAErB,KAAK,KAAK,CAAC,QAAQ;gBACjB,OAAO;oBACL,KAAK,EAAE,SAAS;oBAChB,MAAM,EAAE,MAAM,CAAC,OAAO;oBACtB,K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,YAAY;IACnB,QAAQ,aAAa;IACrB,QAAQ,aAAa;CACtB;AAUD;;GAEG;AACH,eAAO,MAAM,UAAU,0GAGd,MAAM,GAAG,SAAS,
|
|
1
|
+
{"version":3,"file":"usePromise.d.ts","sourceRoot":"","sources":["../../../src/hooks/usePromise.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,cAAc,EAA2C,MAAM,OAAO,CAAA;AAE/E,oBAAY,KAAK;IACf,OAAO,YAAY;IACnB,QAAQ,aAAa;IACrB,QAAQ,aAAa;CACtB;AAUD;;GAEG;AACH,eAAO,MAAM,UAAU,0GAGd,MAAM,GAAG,SAAS,gEAyF1B,CAAA"}
|
package/package.json
CHANGED
|
@@ -12,21 +12,20 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@xylabs/pixel": "^1.4.13",
|
|
14
14
|
"@xylabs/react-appbar": "^2.17.3",
|
|
15
|
-
"@xylabs/react-async-effect": "^2.17.3",
|
|
16
15
|
"@xylabs/react-button": "^2.17.3",
|
|
17
16
|
"@xylabs/react-flexbox": "^2.17.3",
|
|
18
17
|
"@xylabs/react-link": "^2.17.3",
|
|
19
18
|
"@xylabs/react-shared": "^2.17.3",
|
|
20
|
-
"@xyo-network/core": "^2.
|
|
21
|
-
"@xyo-network/payload-model": "^2.
|
|
22
|
-
"@xyo-network/promise": "^2.
|
|
23
|
-
"@xyo-network/react-event": "^2.
|
|
19
|
+
"@xyo-network/core": "^2.63.1",
|
|
20
|
+
"@xyo-network/payload-model": "^2.63.1",
|
|
21
|
+
"@xyo-network/promise": "^2.63.1",
|
|
22
|
+
"@xyo-network/react-event": "^2.55.0"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
26
|
-
"@storybook/react": "^7.0.
|
|
25
|
+
"@storybook/react": "^7.0.23",
|
|
27
26
|
"@xylabs/react-quick-tip-button": "^2.17.3",
|
|
28
|
-
"@xylabs/ts-scripts-yarn3": "^2.17.
|
|
29
|
-
"@xylabs/tsconfig-react": "^2.17.
|
|
27
|
+
"@xylabs/ts-scripts-yarn3": "^2.17.17",
|
|
28
|
+
"@xylabs/tsconfig-react": "^2.17.17",
|
|
30
29
|
"typescript": "^5.1.3"
|
|
31
30
|
},
|
|
32
31
|
"peerDependencies": {
|
|
@@ -81,5 +80,5 @@
|
|
|
81
80
|
},
|
|
82
81
|
"sideEffects": false,
|
|
83
82
|
"types": "dist/types/index.d.ts",
|
|
84
|
-
"version": "2.
|
|
83
|
+
"version": "2.55.0"
|
|
85
84
|
}
|
package/src/hooks/usePromise.ts
CHANGED
|
@@ -25,8 +25,6 @@ export const usePromise = <TResult>(
|
|
|
25
25
|
dependencies: DependencyList = [],
|
|
26
26
|
debug: string | undefined = undefined,
|
|
27
27
|
): [TResult | undefined, Error | undefined, State | undefined] => {
|
|
28
|
-
const promiseMemo = useMemo(() => promise?.(), dependencies)
|
|
29
|
-
|
|
30
28
|
const defaultState: PromiseState<TResult> = {
|
|
31
29
|
error: undefined,
|
|
32
30
|
result: undefined,
|
|
@@ -59,31 +57,40 @@ export const usePromise = <TResult>(
|
|
|
59
57
|
|
|
60
58
|
const [{ error, result, state }, dispatch] = useReducer(reducer, defaultState)
|
|
61
59
|
|
|
60
|
+
const promiseMemo = useMemo(() => {
|
|
61
|
+
try {
|
|
62
|
+
return promise?.()
|
|
63
|
+
} catch (e) {
|
|
64
|
+
dispatch({
|
|
65
|
+
error: e as Error,
|
|
66
|
+
payload: undefined,
|
|
67
|
+
type: State.rejected,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}, dependencies)
|
|
71
|
+
|
|
62
72
|
if (debug) console.debug(`usePromise [${debug}] Main Function`)
|
|
63
73
|
|
|
64
74
|
useEffect(() => {
|
|
65
|
-
let cancelled = false
|
|
66
75
|
dispatch({ type: State.pending })
|
|
67
|
-
if (debug) console.debug(`usePromise [${debug}] useEffect
|
|
76
|
+
if (debug) console.debug(`usePromise [${debug}] useEffect`)
|
|
68
77
|
if (promiseMemo instanceof Promise) {
|
|
69
78
|
promiseMemo
|
|
70
79
|
.then((payload) => {
|
|
71
|
-
if (debug) console.debug(`usePromise [${debug}] then
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
})
|
|
80
|
+
if (debug) console.debug(`usePromise [${debug}] then`)
|
|
81
|
+
dispatch({
|
|
82
|
+
error: undefined,
|
|
83
|
+
payload,
|
|
84
|
+
type: State.resolved,
|
|
85
|
+
})
|
|
78
86
|
})
|
|
79
87
|
.catch((error) => {
|
|
80
|
-
if (debug) console.debug(`usePromise [${debug}] catch
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
})
|
|
88
|
+
if (debug) console.debug(`usePromise [${debug}] catch`)
|
|
89
|
+
dispatch({
|
|
90
|
+
error: error as Error,
|
|
91
|
+
payload: undefined,
|
|
92
|
+
type: State.rejected,
|
|
93
|
+
})
|
|
87
94
|
})
|
|
88
95
|
} else if (promiseMemo) {
|
|
89
96
|
dispatch({
|
|
@@ -92,17 +99,15 @@ export const usePromise = <TResult>(
|
|
|
92
99
|
type: State.resolved,
|
|
93
100
|
})
|
|
94
101
|
} else {
|
|
95
|
-
if (debug) console.debug(`usePromise [${debug}] no-promise
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})
|
|
102
|
+
if (debug) console.debug(`usePromise [${debug}] no-promise`)
|
|
103
|
+
dispatch({
|
|
104
|
+
error: undefined,
|
|
105
|
+
payload: undefined,
|
|
106
|
+
type: State.resolved,
|
|
107
|
+
})
|
|
102
108
|
}
|
|
103
109
|
return () => {
|
|
104
110
|
if (debug) console.debug(`usePromise [${debug}] useEffect callback`)
|
|
105
|
-
cancelled = true
|
|
106
111
|
}
|
|
107
112
|
}, [promiseMemo])
|
|
108
113
|
|