@ahoo-wang/fetcher-react 2.13.0 → 2.13.2

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/index.es.js CHANGED
@@ -1,249 +1,279 @@
1
- var F = Object.defineProperty;
2
- var f = (t, r) => F(t, "name", { value: r, configurable: !0 });
3
- import { useRef as h, useCallback as o, useEffect as w, useState as S, useMemo as x, useSyncExternalStore as M } from "react";
1
+ var Q = Object.defineProperty;
2
+ var g = (e, r) => Q(e, "name", { value: r, configurable: !0 });
3
+ import { useRef as h, useCallback as n, useEffect as L, useState as w, useMemo as b, useSyncExternalStore as M } from "react";
4
4
  import { nameGenerator as q } from "@ahoo-wang/fetcher-eventbus";
5
5
  import { fetcherRegistrar as O, getFetcher as A } from "@ahoo-wang/fetcher";
6
- function R() {
7
- const t = h(!1), r = o(() => t.current, []);
8
- return w(() => (t.current = !0, () => {
9
- t.current = !1;
6
+ function v() {
7
+ const e = h(!1), r = n(() => e.current, []);
8
+ return L(() => (e.current = !0, () => {
9
+ e.current = !1;
10
10
  }), []), r;
11
11
  }
12
- f(R, "useMounted");
13
- function v(t) {
14
- const r = h(t);
15
- return r.current = t, r;
12
+ g(v, "useMounted");
13
+ function R(e) {
14
+ const r = h(e);
15
+ return r.current = e, r;
16
16
  }
17
- f(v, "useLatest");
18
- var D = /* @__PURE__ */ ((t) => (t.IDLE = "idle", t.LOADING = "loading", t.SUCCESS = "success", t.ERROR = "error", t))(D || {});
19
- function C(t) {
20
- const [r, n] = S(
21
- t?.initialStatus ?? "idle"
17
+ g(R, "useLatest");
18
+ var z = /* @__PURE__ */ ((e) => (e.IDLE = "idle", e.LOADING = "loading", e.SUCCESS = "success", e.ERROR = "error", e))(z || {});
19
+ function C(e) {
20
+ const [r, s] = w(
21
+ e?.initialStatus ?? "idle"
22
22
  /* IDLE */
23
- ), [s, a] = S(void 0), [u, e] = S(void 0), c = R(), d = v(t), l = o(() => {
24
- c() && (n(
23
+ ), [c, i] = w(void 0), [u, t] = w(void 0), o = v(), f = R(e), l = n(() => {
24
+ o() && (s(
25
25
  "loading"
26
26
  /* LOADING */
27
- ), e(void 0));
28
- }, [c]), E = o(
29
- async (i) => {
30
- if (c()) {
31
- a(i), n(
27
+ ), t(void 0));
28
+ }, [o]), y = n(
29
+ async (d) => {
30
+ if (o()) {
31
+ i(d), s(
32
32
  "success"
33
33
  /* SUCCESS */
34
- ), e(void 0);
34
+ ), t(void 0);
35
35
  try {
36
- await d.current?.onSuccess?.(i);
36
+ await f.current?.onSuccess?.(d);
37
37
  } catch (m) {
38
38
  console.warn("PromiseState onSuccess callback error:", m);
39
39
  }
40
40
  }
41
41
  },
42
- [c, d]
43
- ), g = o(
44
- async (i) => {
45
- if (c()) {
46
- e(i), n(
42
+ [o, f]
43
+ ), a = n(
44
+ async (d) => {
45
+ if (o()) {
46
+ t(d), s(
47
47
  "error"
48
48
  /* ERROR */
49
- ), a(void 0);
49
+ ), i(void 0);
50
50
  try {
51
- await d.current?.onError?.(i);
51
+ await f.current?.onError?.(d);
52
52
  } catch (m) {
53
53
  console.warn("PromiseState onError callback error:", m);
54
54
  }
55
55
  }
56
56
  },
57
- [c, d]
58
- ), y = o(() => {
59
- c() && (n(
57
+ [o, f]
58
+ ), E = n(() => {
59
+ o() && (s(
60
60
  "idle"
61
61
  /* IDLE */
62
- ), e(void 0), a(void 0));
63
- }, [c]);
64
- return x(
62
+ ), t(void 0), i(void 0));
63
+ }, [o]);
64
+ return b(
65
65
  () => ({
66
66
  status: r,
67
67
  loading: r === "loading",
68
- result: s,
68
+ result: c,
69
69
  error: u,
70
70
  setLoading: l,
71
- setSuccess: E,
72
- setError: g,
73
- setIdle: y
71
+ setSuccess: y,
72
+ setError: a,
73
+ setIdle: E
74
74
  }),
75
- [r, s, u, l, E, g, y]
75
+ [r, c, u, l, y, a, E]
76
76
  );
77
77
  }
78
- f(C, "usePromiseState");
79
- function Q() {
80
- const t = h(0), r = o(() => ++t.current, []), n = o(() => t.current, []), s = o((e) => e === t.current, []), a = o(() => {
81
- t.current++;
82
- }, []), u = o(() => {
83
- t.current = 0;
78
+ g(C, "usePromiseState");
79
+ function F() {
80
+ const e = h(0), r = n(() => ++e.current, []), s = n(() => e.current, []), c = n((t) => t === e.current, []), i = n(() => {
81
+ e.current++;
82
+ }, []), u = n(() => {
83
+ e.current = 0;
84
84
  }, []);
85
- return x(() => ({
85
+ return b(() => ({
86
86
  generate: r,
87
- current: n,
88
- isLatest: s,
89
- invalidate: a,
87
+ current: s,
88
+ isLatest: c,
89
+ invalidate: i,
90
90
  reset: u
91
- }), [r, n, s, a, u]);
91
+ }), [r, s, c, i, u]);
92
92
  }
93
- f(Q, "useRequestId");
94
- function G(t) {
95
- const { loading: r, result: n, error: s, status: a, setLoading: u, setSuccess: e, setError: c, setIdle: d } = C(t), l = R(), E = Q(), g = t?.propagateError, y = o(
93
+ g(F, "useRequestId");
94
+ function D(e) {
95
+ const { loading: r, result: s, error: c, status: i, setLoading: u, setSuccess: t, setError: o, setIdle: f } = C(e), l = v(), y = F(), a = e?.propagateError, E = n(
96
96
  async (m) => {
97
97
  if (!l())
98
98
  throw new Error("Component is unmounted");
99
- const I = E.generate();
99
+ const p = y.generate();
100
100
  u();
101
101
  try {
102
- const p = await (typeof m == "function" ? m() : m);
103
- return l() && E.isLatest(I) && await e(p), p;
104
- } catch (L) {
105
- if (l() && E.isLatest(I) && await c(L), g)
106
- throw L;
107
- return L;
102
+ const I = await (typeof m == "function" ? m() : m);
103
+ return l() && y.isLatest(p) && await t(I), I;
104
+ } catch (x) {
105
+ if (l() && y.isLatest(p) && await o(x), a)
106
+ throw x;
107
+ return x;
108
108
  }
109
109
  },
110
- [u, e, c, l, E, g]
111
- ), i = o(() => {
112
- l() && d();
113
- }, [d, l]);
114
- return x(
110
+ [u, t, o, l, y, a]
111
+ ), d = n(() => {
112
+ l() && f();
113
+ }, [f, l]);
114
+ return b(
115
115
  () => ({
116
116
  loading: r,
117
- result: n,
118
- error: s,
119
- execute: y,
120
- reset: i,
121
- status: a
117
+ result: s,
118
+ error: c,
119
+ execute: E,
120
+ reset: d,
121
+ status: i
122
+ }),
123
+ [r, s, c, E, d, i]
124
+ );
125
+ }
126
+ g(D, "useExecutePromise");
127
+ function N() {
128
+ const e = h(/* @__PURE__ */ new Map()), r = n((a) => e.current.get(a), []), s = n(
129
+ (a, E) => e.current.set(a, E),
130
+ []
131
+ ), c = n((a) => e.current.has(a), []), i = n((a) => e.current.delete(a), []), u = n(() => e.current.clear(), []), t = n(() => e.current.keys(), []), o = n(() => e.current.values(), []), f = n(() => e.current.entries(), []), l = n(() => e.current[Symbol.iterator](), []), y = n((a) => (E) => {
132
+ E ? e.current.set(a, E) : e.current.delete(a);
133
+ }, []);
134
+ return L(() => () => {
135
+ e.current.clear();
136
+ }, []), b(
137
+ () => ({
138
+ register: y,
139
+ get: r,
140
+ set: s,
141
+ has: c,
142
+ delete: i,
143
+ clear: u,
144
+ keys: t,
145
+ values: o,
146
+ entries: f,
147
+ get size() {
148
+ return e.current.size;
149
+ },
150
+ [Symbol.iterator]: l
122
151
  }),
123
- [r, n, s, y, i, a]
152
+ [y, r, s, c, i, u, t, o, f, l]
124
153
  );
125
154
  }
126
- f(G, "useExecutePromise");
127
- function V(t, r) {
128
- const n = o(
129
- (e) => t.addListener({
155
+ g(N, "useRefs");
156
+ function U(e, r) {
157
+ const s = n(
158
+ (t) => e.addListener({
130
159
  name: q.generate("useKeyStorage"),
131
160
  // Generate unique listener name
132
- handle: e
161
+ handle: t
133
162
  // Callback to trigger React re-render on storage changes
134
163
  }),
135
- [t]
164
+ [e]
136
165
  // Recreate subscription only if keyStorage changes
137
- ), s = o(() => {
138
- const e = t.get();
139
- return e !== null ? e : r ?? null;
140
- }, [t, r]), a = M(n, s, s), u = o(
141
- (e) => t.set(e),
142
- [t]
166
+ ), c = n(() => {
167
+ const t = e.get();
168
+ return t !== null ? t : r ?? null;
169
+ }, [e, r]), i = M(s, c, c), u = n(
170
+ (t) => e.set(t),
171
+ [e]
143
172
  // Recreate setter only if keyStorage changes
144
173
  );
145
- return [a, u];
174
+ return [i, u];
146
175
  }
147
- f(V, "useKeyStorage");
148
- function j(t) {
149
- const { fetcher: r = O.default } = t || {}, n = C(t), [s, a] = S(
176
+ g(U, "useKeyStorage");
177
+ function V(e) {
178
+ const { fetcher: r = O.default } = e || {}, s = C(e), [c, i] = w(
150
179
  void 0
151
- ), u = R(), e = h(), c = Q(), d = v(t), l = A(r), E = o(
152
- async (g) => {
153
- e.current && e.current.abort(), e.current = g.abortController ?? new AbortController(), g.abortController = e.current;
154
- const y = c.generate();
155
- n.setLoading();
180
+ ), u = v(), t = h(void 0), o = F(), f = R(e), l = A(r), y = n(
181
+ async (a) => {
182
+ t.current && t.current.abort(), t.current = a.abortController ?? new AbortController(), a.abortController = t.current;
183
+ const E = o.generate();
184
+ s.setLoading();
156
185
  try {
157
- const i = await l.exchange(
158
- g,
159
- d.current
186
+ const d = await l.exchange(
187
+ a,
188
+ f.current
160
189
  );
161
- u() && c.isLatest(y) && a(i);
162
- const m = await i.extractResult();
163
- u() && c.isLatest(y) && await n.setSuccess(m);
164
- } catch (i) {
165
- if (i instanceof Error && i.name === "AbortError") {
166
- u() && n.setIdle();
190
+ u() && o.isLatest(E) && i(d);
191
+ const m = await d.extractResult();
192
+ u() && o.isLatest(E) && await s.setSuccess(m);
193
+ } catch (d) {
194
+ if (d instanceof Error && d.name === "AbortError") {
195
+ u() && s.setIdle();
167
196
  return;
168
197
  }
169
- u() && c.isLatest(y) && await n.setError(i);
198
+ u() && o.isLatest(E) && await s.setError(d);
170
199
  } finally {
171
- e.current === g.abortController && (e.current = void 0);
200
+ t.current === a.abortController && (t.current = void 0);
172
201
  }
173
202
  },
174
- [l, u, d, n, c]
203
+ [l, u, f, s, o]
175
204
  );
176
- return w(() => () => {
177
- e.current?.abort(), e.current = void 0;
178
- }, []), x(
205
+ return L(() => () => {
206
+ t.current?.abort(), t.current = void 0;
207
+ }, []), b(
179
208
  () => ({
180
- ...n,
181
- exchange: s,
182
- execute: E
209
+ ...s,
210
+ exchange: c,
211
+ execute: y
183
212
  }),
184
- [n, s, E]
213
+ [s, c, y]
185
214
  );
186
215
  }
187
- f(j, "useFetcher");
188
- function b(t) {
189
- const r = v(t), n = G(r.current), s = h(t.initialQuery), a = o(async () => r.current.execute(
190
- s.current,
216
+ g(V, "useFetcher");
217
+ function S(e) {
218
+ const r = R(e), s = D(r.current), c = h(e.initialQuery), i = n(async () => r.current.execute(
219
+ c.current,
191
220
  r.current.attributes
192
- ), [s, r]), { execute: u } = n, e = o(() => u(a), [u, a]), c = o(() => s.current, [s]), d = o(
221
+ ), [c, r]), { execute: u } = s, t = n(() => u(i), [u, i]), o = n(() => c.current, [c]), f = n(
193
222
  (l) => {
194
- s.current = l, r.current.autoExecute && e();
223
+ c.current = l, r.current.autoExecute && t();
195
224
  },
196
- [s, r, e]
225
+ [c, r, t]
197
226
  );
198
- return w(() => {
199
- r.current.autoExecute && e();
200
- }, [r, e]), x(
227
+ return L(() => {
228
+ r.current.autoExecute && t();
229
+ }, [r, t]), b(
201
230
  () => ({
202
- ...n,
203
- execute: e,
204
- getQuery: c,
205
- setQuery: d
231
+ ...s,
232
+ execute: t,
233
+ getQuery: o,
234
+ setQuery: f
206
235
  }),
207
- [n, e, c, d]
236
+ [s, t, o, f]
208
237
  );
209
238
  }
210
- f(b, "useQuery");
211
- function k(t) {
212
- return b(t);
239
+ g(S, "useQuery");
240
+ function j(e) {
241
+ return S(e);
213
242
  }
214
- f(k, "usePagedQuery");
215
- function z(t) {
216
- return b(t);
243
+ g(j, "usePagedQuery");
244
+ function B(e) {
245
+ return S(e);
217
246
  }
218
- f(z, "useSingleQuery");
219
- function B(t) {
220
- return b(t);
247
+ g(B, "useSingleQuery");
248
+ function H(e) {
249
+ return S(e);
221
250
  }
222
- f(B, "useCountQuery");
223
- function H(t) {
224
- return b(t);
251
+ g(H, "useCountQuery");
252
+ function J(e) {
253
+ return S(e);
225
254
  }
226
- f(H, "useListQuery");
227
- function J(t) {
228
- return b(
229
- t
255
+ g(J, "useListQuery");
256
+ function T(e) {
257
+ return S(
258
+ e
230
259
  );
231
260
  }
232
- f(J, "useListStreamQuery");
261
+ g(T, "useListStreamQuery");
233
262
  export {
234
- D as PromiseStatus,
235
- B as useCountQuery,
236
- G as useExecutePromise,
237
- j as useFetcher,
238
- V as useKeyStorage,
239
- v as useLatest,
240
- H as useListQuery,
241
- J as useListStreamQuery,
242
- R as useMounted,
243
- k as usePagedQuery,
263
+ z as PromiseStatus,
264
+ H as useCountQuery,
265
+ D as useExecutePromise,
266
+ V as useFetcher,
267
+ U as useKeyStorage,
268
+ R as useLatest,
269
+ J as useListQuery,
270
+ T as useListStreamQuery,
271
+ v as useMounted,
272
+ j as usePagedQuery,
244
273
  C as usePromiseState,
245
- b as useQuery,
246
- Q as useRequestId,
247
- z as useSingleQuery
274
+ S as useQuery,
275
+ N as useRefs,
276
+ F as useRequestId,
277
+ B as useSingleQuery
248
278
  };
249
279
  //# sourceMappingURL=index.es.js.map