@xyo-network/react-shared 2.40.12 → 2.40.14

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.
@@ -1,4 +1,5 @@
1
- declare type PromiseOrFunction<T> = Promise<T> | (() => Promise<T>);
2
- export declare const usePromise: <T>(promiseArg: PromiseOrFunction<T>, inputs?: unknown[]) => any[];
3
- export {};
1
+ /**
2
+ * usePromise -
3
+ */
4
+ export declare const usePromise: <D, T extends D | Promise<D>>(promise?: T | undefined, dependencies?: unknown[]) => any[];
4
5
  //# sourceMappingURL=usePromise.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"usePromise.d.ts","sourceRoot":"","sources":["../../../src/hooks/usePromise.tsx"],"names":[],"mappings":"AAmDA;;GAEG;AACH,eAAO,MAAM,UAAU,wEAA4D,OAAO,EAAE,UAuC3F,CAAA"}
@@ -1,11 +1,6 @@
1
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
- };
2
+ import { useAsyncEffect } from '@xylabs/react-shared';
3
+ import { useReducer } from 'react';
9
4
  var State;
10
5
  (function (State) {
11
6
  State["pending"] = "pending";
@@ -37,31 +32,39 @@ const reducer = (_state, action) => {
37
32
  throw Error(`Error parsing action ${JSON.stringify(action, null, 2)}`);
38
33
  }
39
34
  };
40
- export const usePromise = (promiseArg, inputs = []) => {
35
+ /**
36
+ * usePromise -
37
+ */
38
+ export const usePromise = (promise, dependencies = []) => {
41
39
  const [{ error, result, state }, dispatch] = useReducer(reducer, defaultState);
42
- const dependencies = useMemo(() => [promiseArg, ...inputs], [inputs, promiseArg]);
43
- useEffect(() => {
44
- const promise = resolvePromise(promiseArg);
40
+ useAsyncEffect(
41
+ // eslint-disable-next-line react-hooks/exhaustive-deps
42
+ async () => {
45
43
  if (!promise) {
46
44
  return;
47
45
  }
48
46
  let canceled = false;
49
47
  dispatch({ type: State.pending });
50
- promise.then((result) => !canceled &&
51
- dispatch({
48
+ const result = await promise;
49
+ !canceled
50
+ ? dispatch({
52
51
  payload: result,
53
52
  type: State.resolved,
54
- }), (error) => !canceled &&
55
- dispatch({
53
+ })
54
+ : undefined;
55
+ !canceled
56
+ ? dispatch({
56
57
  payload: error,
57
58
  type: State.rejected,
58
- }));
59
+ })
60
+ : undefined;
59
61
  return () => {
60
62
  canceled = true;
61
63
  };
62
- // eslint can't inspect the array to verify dependencies are missing
63
- // eslint-disable-next-line react-hooks/exhaustive-deps
64
- }, dependencies);
64
+ },
65
+ // eslint can't inspect the array to verify dependencies are missing
66
+ // eslint-disable-next-line react-hooks/exhaustive-deps
67
+ dependencies);
65
68
  return [result, error, state];
66
69
  };
67
70
  //# sourceMappingURL=usePromise.js.map
@@ -1 +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;AACv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CAAC,SAAS,CAAA;QAEb,OAAO,GAAG,EAAE;YACV,QAAQ,GAAG,IAAI,CAAA;QACjB,CAAC,CAAA;IACH,CAAC;IACD,oEAAoE;IACpE,uDAAuD;IACvD,YAAY,CACb,CAAA;IAED,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.12",
19
+ "@xyo-network/react-event": "^2.40.14",
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.12"
73
+ "version": "2.40.14"
74
74
  }
@@ -1,16 +1,7 @@
1
1
  // Inspired from https://github.com/bsonntag/react-use-promise
2
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
- }
3
+ import { useAsyncEffect } from '@xylabs/react-shared'
4
+ import { useReducer } from 'react'
14
5
 
15
6
  enum State {
16
7
  pending = 'pending',
@@ -58,43 +49,46 @@ const reducer = (_state: PromiseState, action: Action) => {
58
49
  }
59
50
  }
60
51
 
61
- export const usePromise = <T,>(promiseArg: PromiseOrFunction<T>, inputs: unknown[] = []) => {
52
+ /**
53
+ * usePromise -
54
+ */
55
+ export const usePromise = <D, T extends Promise<D> | D>(promise?: T, dependencies: unknown[] = []) => {
62
56
  const [{ error, result, state }, dispatch] = useReducer(reducer, defaultState)
63
57
 
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
- }
58
+ useAsyncEffect(
59
+ // eslint-disable-next-line react-hooks/exhaustive-deps
60
+ async () => {
61
+ if (!promise) {
62
+ return
63
+ }
64
+
65
+ let canceled = false
66
+
67
+ dispatch({ type: State.pending })
68
+
69
+ const result = await promise
70
+ !canceled
71
+ ? dispatch({
72
+ payload: result,
73
+ type: State.resolved,
74
+ })
75
+ : undefined
76
+
77
+ !canceled
78
+ ? dispatch({
79
+ payload: error,
80
+ type: State.rejected,
81
+ })
82
+ : undefined
83
+
84
+ return () => {
85
+ canceled = true
86
+ }
87
+ },
95
88
  // eslint can't inspect the array to verify dependencies are missing
96
89
  // eslint-disable-next-line react-hooks/exhaustive-deps
97
- }, dependencies)
90
+ dependencies,
91
+ )
98
92
 
99
93
  return [result, error, state]
100
94
  }