@xyo-network/react-shared 2.40.1 → 2.40.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.
@@ -2,5 +2,6 @@ export * from './GradientStyles';
2
2
  export * from './useDataState';
3
3
  export * from './useIsMobile';
4
4
  export * from './useMediaQuery';
5
+ export * from './usePromise';
5
6
  export * from './useShareForwardRef';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,sBAAsB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,sBAAsB,CAAA"}
@@ -5,5 +5,6 @@ tslib_1.__exportStar(require("./GradientStyles"), exports);
5
5
  tslib_1.__exportStar(require("./useDataState"), exports);
6
6
  tslib_1.__exportStar(require("./useIsMobile"), exports);
7
7
  tslib_1.__exportStar(require("./useMediaQuery"), exports);
8
+ tslib_1.__exportStar(require("./usePromise"), exports);
8
9
  tslib_1.__exportStar(require("./useShareForwardRef"), exports);
9
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,2DAAgC;AAChC,yDAA8B;AAC9B,wDAA6B;AAC7B,0DAA+B;AAC/B,+DAAoC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,2DAAgC;AAChC,yDAA8B;AAC9B,wDAA6B;AAC7B,0DAA+B;AAC/B,uDAA4B;AAC5B,+DAAoC"}
@@ -0,0 +1,4 @@
1
+ declare type PromiseOrFunction<T> = Promise<T> | (() => Promise<T>);
2
+ export declare const usePromise: <T>(promiseArg: PromiseOrFunction<T>, inputs?: unknown[]) => any[];
3
+ export {};
4
+ //# sourceMappingURL=usePromise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePromise.d.ts","sourceRoot":"","sources":["../../../src/hooks/usePromise.tsx"],"names":[],"mappings":"AAIA,aAAK,iBAAiB,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AAwD3D,eAAO,MAAM,UAAU,iDAAkD,OAAO,EAAE,UAuCjF,CAAA"}
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ // Inspired from https://github.com/bsonntag/react-use-promise
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.usePromise = void 0;
5
+ const react_1 = require("react");
6
+ const resolvePromise = (promise) => {
7
+ if (typeof promise === 'function') {
8
+ return promise();
9
+ }
10
+ return promise;
11
+ };
12
+ var State;
13
+ (function (State) {
14
+ State["pending"] = "pending";
15
+ State["rejected"] = "rejected";
16
+ State["resolved"] = "resolved";
17
+ })(State || (State = {}));
18
+ const defaultState = {
19
+ error: undefined,
20
+ result: undefined,
21
+ state: State.pending,
22
+ };
23
+ const reducer = (_state, action) => {
24
+ switch (action.type) {
25
+ case State.pending:
26
+ return defaultState;
27
+ case State.resolved:
28
+ return {
29
+ error: undefined,
30
+ result: action.payload,
31
+ state: State.resolved,
32
+ };
33
+ case State.rejected:
34
+ return {
35
+ error: action.payload,
36
+ result: undefined,
37
+ state: State.rejected,
38
+ };
39
+ default:
40
+ throw Error(`Error parsing action ${JSON.stringify(action, null, 2)}`);
41
+ }
42
+ };
43
+ const usePromise = (promiseArg, inputs = []) => {
44
+ const [{ error, result, state }, dispatch] = (0, react_1.useReducer)(reducer, defaultState);
45
+ const dependencies = (0, react_1.useMemo)(() => [promiseArg, ...inputs], [inputs, promiseArg]);
46
+ (0, react_1.useEffect)(() => {
47
+ const promise = resolvePromise(promiseArg);
48
+ if (!promise) {
49
+ return;
50
+ }
51
+ let canceled = false;
52
+ dispatch({ type: State.pending });
53
+ promise.then((result) => !canceled &&
54
+ dispatch({
55
+ payload: result,
56
+ type: State.resolved,
57
+ }), (error) => !canceled &&
58
+ dispatch({
59
+ payload: error,
60
+ type: State.rejected,
61
+ }));
62
+ return () => {
63
+ canceled = true;
64
+ };
65
+ // eslint can't inspect the array to verify dependencies are missing
66
+ // eslint-disable-next-line react-hooks/exhaustive-deps
67
+ }, dependencies);
68
+ return [result, error, state];
69
+ };
70
+ exports.usePromise = usePromise;
71
+ //# sourceMappingURL=usePromise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePromise.js","sourceRoot":"","sources":["../../../src/hooks/usePromise.tsx"],"names":[],"mappings":";AAAA,8DAA8D;;;AAE9D,iCAAsD;AAItD,MAAM,cAAc,GAAG,CAAK,OAA6B,EAAE,EAAE;IAC3D,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;QACjC,OAAO,OAAO,EAAE,CAAA;KACjB;IAED,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA;AAED,IAAK,KAIJ;AAJD,WAAK,KAAK;IACR,4BAAmB,CAAA;IACnB,8BAAqB,CAAA;IACrB,8BAAqB,CAAA;AACvB,CAAC,EAJI,KAAK,KAAL,KAAK,QAIT;AAQD,MAAM,YAAY,GAAiB;IACjC,KAAK,EAAE,SAAS;IAChB,MAAM,EAAE,SAAS;IACjB,KAAK,EAAE,KAAK,CAAC,OAAO;CACrB,CAAA;AAMD,MAAM,OAAO,GAAG,CAAC,MAAoB,EAAE,MAAc,EAAE,EAAE;IACvD,QAAQ,MAAM,CAAC,IAAI,EAAE;QACnB,KAAK,KAAK,CAA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