@xyo-network/react-shared 2.55.1 → 2.55.3
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 +31 -72
- package/dist/cjs/hooks/usePromise.js.map +1 -1
- package/dist/docs.json +302 -306
- package/dist/esm/hooks/usePromise.js +34 -73
- package/dist/esm/hooks/usePromise.js.map +1 -1
- package/dist/types/hooks/usePromise.d.ts +1 -2
- package/dist/types/hooks/usePromise.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/hooks/usePromise.ts +29 -82
|
@@ -13,92 +13,51 @@ var State;
|
|
|
13
13
|
* usePromise -
|
|
14
14
|
*/
|
|
15
15
|
const usePromise = (promise, dependencies = [], debug = undefined) => {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const reducer = (_state, action) => {
|
|
22
|
-
switch (action.type) {
|
|
23
|
-
case State.pending:
|
|
24
|
-
return defaultState;
|
|
25
|
-
case State.resolved:
|
|
26
|
-
return {
|
|
27
|
-
error: undefined,
|
|
28
|
-
result: action.payload,
|
|
29
|
-
state: State.resolved,
|
|
30
|
-
};
|
|
31
|
-
case State.rejected:
|
|
32
|
-
return {
|
|
33
|
-
error: action.error,
|
|
34
|
-
result: undefined,
|
|
35
|
-
state: State.rejected,
|
|
36
|
-
};
|
|
37
|
-
default:
|
|
38
|
-
throw Error(`Error parsing action ${JSON.stringify(action, null, 2)}`);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
const [{ error, result, state }, dispatch] = (0, react_1.useReducer)(reducer, defaultState);
|
|
16
|
+
const [result, setResult] = (0, react_1.useState)();
|
|
17
|
+
const [error, setError] = (0, react_1.useState)();
|
|
18
|
+
const [state, setState] = (0, react_1.useState)(State.pending);
|
|
19
|
+
if (debug)
|
|
20
|
+
console.log(`usePromise [${debug}]: started [${typeof promise}]`);
|
|
42
21
|
const promiseMemo = (0, react_1.useMemo)(() => {
|
|
43
22
|
try {
|
|
23
|
+
if (debug)
|
|
24
|
+
console.log(`usePromise [${debug}]: re-memo [${typeof promise}]`);
|
|
25
|
+
setState(State.pending);
|
|
44
26
|
return promise === null || promise === void 0 ? void 0 : promise();
|
|
45
27
|
}
|
|
46
28
|
catch (e) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
29
|
+
if (debug)
|
|
30
|
+
console.log(`usePromise [${debug}]: useMemo rejection [${typeof promise}]`);
|
|
31
|
+
setResult(undefined);
|
|
32
|
+
setError(e);
|
|
33
|
+
setState(State.rejected);
|
|
52
34
|
}
|
|
53
35
|
}, dependencies);
|
|
54
36
|
if (debug)
|
|
55
|
-
console.
|
|
37
|
+
console.log(`usePromise [${debug}] Main Function`);
|
|
56
38
|
(0, react_1.useEffect)(() => {
|
|
57
|
-
dispatch({ type: State.pending });
|
|
58
39
|
if (debug)
|
|
59
|
-
console.
|
|
60
|
-
|
|
61
|
-
promiseMemo
|
|
62
|
-
.then((payload) => {
|
|
63
|
-
if (debug)
|
|
64
|
-
console.debug(`usePromise [${debug}] then`);
|
|
65
|
-
dispatch({
|
|
66
|
-
error: undefined,
|
|
67
|
-
payload,
|
|
68
|
-
type: State.resolved,
|
|
69
|
-
});
|
|
70
|
-
})
|
|
71
|
-
.catch((error) => {
|
|
72
|
-
if (debug)
|
|
73
|
-
console.debug(`usePromise [${debug}] catch`);
|
|
74
|
-
dispatch({
|
|
75
|
-
error: error,
|
|
76
|
-
payload: undefined,
|
|
77
|
-
type: State.rejected,
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
else if (promiseMemo) {
|
|
82
|
-
dispatch({
|
|
83
|
-
error: undefined,
|
|
84
|
-
payload: promiseMemo,
|
|
85
|
-
type: State.resolved,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
40
|
+
console.log(`usePromise [${debug}] useEffect`);
|
|
41
|
+
promiseMemo === null || promiseMemo === void 0 ? void 0 : promiseMemo.then((payload) => {
|
|
89
42
|
if (debug)
|
|
90
|
-
console.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
43
|
+
console.log(`usePromise [${debug}] then`);
|
|
44
|
+
setResult(payload);
|
|
45
|
+
setError(undefined);
|
|
46
|
+
setState(State.resolved);
|
|
47
|
+
}).catch((e) => {
|
|
48
|
+
const error = e;
|
|
49
|
+
console.error(`usePromise: ${error.message}`);
|
|
50
|
+
setResult(undefined);
|
|
51
|
+
setError(error);
|
|
52
|
+
setState(State.rejected);
|
|
53
|
+
});
|
|
97
54
|
return () => {
|
|
98
55
|
if (debug)
|
|
99
|
-
console.
|
|
56
|
+
console.log(`usePromise [${debug}] useEffect callback`);
|
|
100
57
|
};
|
|
101
|
-
}, [promiseMemo]);
|
|
58
|
+
}, [...dependencies, promiseMemo]);
|
|
59
|
+
if (debug)
|
|
60
|
+
console.log(`usePromise [${debug}] returning ${JSON.stringify([result, error, state], null, 2)}`);
|
|
102
61
|
return [result, error, state];
|
|
103
62
|
};
|
|
104
63
|
exports.usePromise = usePromise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePromise.js","sourceRoot":"","sources":["../../../src/hooks/usePromise.ts"],"names":[],"mappings":";AAAA,8DAA8D;;;
|
|
1
|
+
{"version":3,"file":"usePromise.js","sourceRoot":"","sources":["../../../src/hooks/usePromise.ts"],"names":[],"mappings":";AAAA,8DAA8D;;;AAE9D,iCAAoE;AAEpE,IAAY,KAIX;AAJD,WAAY,KAAK;IACf,4BAAmB,CAAA;IACnB,8BAAqB,CAAA;IACrB,8BAAqB,CAAA;AACvB,CAAC,EAJW,KAAK,GAAL,aAAK,KAAL,aAAK,QAIhB;AAED;;GAEG;AACI,MAAM,UAAU,GAAG,CACxB,OAA2C,EAC3C,eAA+B,EAAE,EACjC,QAA4B,SAAS,EACwB,EAAE;I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