@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"}
@@ -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.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","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.js","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"}
@@ -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,67 @@
1
+ // Inspired from https://github.com/bsonntag/react-use-promise
2
+ import { useEffect, useMemo, useReducer } from 'react';
3
+ const resolvePromise = (promise) => {
4
+ if (typeof promise === 'function') {
5
+ return promise();
6
+ }
7
+ return promise;
8
+ };
9
+ var State;
10
+ (function (State) {
11
+ State["pending"] = "pending";
12
+ State["rejected"] = "rejected";
13
+ State["resolved"] = "resolved";
14
+ })(State || (State = {}));
15
+ const defaultState = {
16
+ error: undefined,
17
+ result: undefined,
18
+ state: State.pending,
19
+ };
20
+ const reducer = (_state, action) => {
21
+ switch (action.type) {
22
+ case State.pending:
23
+ return defaultState;
24
+ case State.resolved:
25
+ return {
26
+ error: undefined,
27
+ result: action.payload,
28
+ state: State.resolved,
29
+ };
30
+ case State.rejected:
31
+ return {
32
+ error: action.payload,
33
+ result: undefined,
34
+ state: State.rejected,
35
+ };
36
+ default:
37
+ throw Error(`Error parsing action ${JSON.stringify(action, null, 2)}`);
38
+ }
39
+ };
40
+ export const usePromise = (promiseArg, inputs = []) => {
41
+ const [{ error, result, state }, dispatch] = useReducer(reducer, defaultState);
42
+ const dependencies = useMemo(() => [promiseArg, ...inputs], [inputs, promiseArg]);
43
+ useEffect(() => {
44
+ const promise = resolvePromise(promiseArg);
45
+ if (!promise) {
46
+ return;
47
+ }
48
+ let canceled = false;
49
+ dispatch({ type: State.pending });
50
+ promise.then((result) => !canceled &&
51
+ dispatch({
52
+ payload: result,
53
+ type: State.resolved,
54
+ }), (error) => !canceled &&
55
+ dispatch({
56
+ payload: error,
57
+ type: State.rejected,
58
+ }));
59
+ return () => {
60
+ canceled = true;
61
+ };
62
+ // eslint can't inspect the array to verify dependencies are missing
63
+ // eslint-disable-next-line react-hooks/exhaustive-deps
64
+ }, dependencies);
65
+ return [result, error, state];
66
+ };
67
+ //# sourceMappingURL=usePromise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePromise.js","sourceRoot":"","sources":["../../../src/hooks/usePromise.tsx"],"names":[],"mappings":"AAAA,8DAA8D;AAE9D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;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,CAAC,OAAO;YAChB,OAAO,YAAY,CAAA;Q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ACR,CAAC,QAAQ;YACT,QAAQ,CAAC;gBACP,OAAO,EAAE,KAAK;gBACd,IAAI,EAAE,KAAK,CAAC,QAAQ;aACrB,CAAC,CACL,CAAA;QAED,OAAO,GAAG,EAAE;YACV,QAAQ,GAAG,IAAI,CAAA;QACjB,CAAC,CAAA;QACD,oEAAoE;QACpE,uDAAuD;IACzD,CAAC,EAAE,YAAY,CAAC,CAAA;IAEhB,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;AAC/B,CAAC,CAAA"}
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "@xylabs/react-flexbox": "^2.15.1",
17
17
  "@xylabs/react-link": "^2.15.1",
18
18
  "@xylabs/react-shared": "^2.15.1",
19
- "@xyo-network/react-event": "^2.40.1",
19
+ "@xyo-network/react-event": "^2.40.3",
20
20
  "tslib": "^2.4.1"
21
21
  },
22
22
  "peerDependencies": {
@@ -70,5 +70,5 @@
70
70
  },
71
71
  "sideEffects": false,
72
72
  "types": "dist/esm/index.d.ts",
73
- "version": "2.40.1"
73
+ "version": "2.40.3"
74
74
  }
@@ -2,4 +2,5 @@ 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'
@@ -0,0 +1,100 @@
1
+ // Inspired from https://github.com/bsonntag/react-use-promise
2
+
3
+ import { useEffect, useMemo, useReducer } from 'react'
4
+
5
+ type PromiseOrFunction<T> = Promise<T> | (() => Promise<T>)
6
+
7
+ const resolvePromise = <T,>(promise: PromiseOrFunction<T>) => {
8
+ if (typeof promise === 'function') {
9
+ return promise()
10
+ }
11
+
12
+ return promise
13
+ }
14
+
15
+ enum State {
16
+ pending = 'pending',
17
+ rejected = 'rejected',
18
+ resolved = 'resolved',
19
+ }
20
+
21
+ interface PromiseState<T = void> {
22
+ error?: Error
23
+ result?: T
24
+ state?: State
25
+ }
26
+
27
+ const defaultState: PromiseState = {
28
+ error: undefined,
29
+ result: undefined,
30
+ state: State.pending,
31
+ }
32
+
33
+ // Since the resolved promise value can be anything, any seems appropriate
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ type Action = { type: State; payload?: any }
36
+
37
+ const reducer = (_state: PromiseState, action: Action) => {
38
+ switch (action.type) {
39
+ case State.pending:
40
+ return defaultState
41
+
42
+ case State.resolved:
43
+ return {
44
+ error: undefined,
45
+ result: action.payload,
46
+ state: State.resolved,
47
+ }
48
+
49
+ case State.rejected:
50
+ return {
51
+ error: action.payload,
52
+ result: undefined,
53
+ state: State.rejected,
54
+ }
55
+
56
+ default:
57
+ throw Error(`Error parsing action ${JSON.stringify(action, null, 2)}`)
58
+ }
59
+ }
60
+
61
+ export const usePromise = <T,>(promiseArg: PromiseOrFunction<T>, inputs: unknown[] = []) => {
62
+ const [{ error, result, state }, dispatch] = useReducer(reducer, defaultState)
63
+
64
+ const dependencies = useMemo(() => [promiseArg, ...inputs], [inputs, promiseArg])
65
+
66
+ useEffect(() => {
67
+ const promise = resolvePromise<T>(promiseArg)
68
+
69
+ if (!promise) {
70
+ return
71
+ }
72
+
73
+ let canceled = false
74
+
75
+ dispatch({ type: State.pending })
76
+
77
+ promise.then(
78
+ (result) =>
79
+ !canceled &&
80
+ dispatch({
81
+ payload: result,
82
+ type: State.resolved,
83
+ }),
84
+ (error) =>
85
+ !canceled &&
86
+ dispatch({
87
+ payload: error,
88
+ type: State.rejected,
89
+ }),
90
+ )
91
+
92
+ return () => {
93
+ canceled = true
94
+ }
95
+ // eslint can't inspect the array to verify dependencies are missing
96
+ // eslint-disable-next-line react-hooks/exhaustive-deps
97
+ }, dependencies)
98
+
99
+ return [result, error, state]
100
+ }