@ahoo-wang/fetcher-react 2.13.11 → 2.15.1

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,279 +1,384 @@
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
- import { nameGenerator as q } from "@ahoo-wang/fetcher-eventbus";
5
- import { fetcherRegistrar as O, getFetcher as A } from "@ahoo-wang/fetcher";
6
- function v() {
7
- const e = h(!1), r = n(() => e.current, []);
8
- return L(() => (e.current = !0, () => {
1
+ var k = Object.defineProperty;
2
+ var b = (e, t) => k(e, "name", { value: t, configurable: !0 });
3
+ import { useRef as y, useCallback as u, useEffect as R, useState as F, useMemo as m, useSyncExternalStore as A } from "react";
4
+ import { nameGenerator as z } from "@ahoo-wang/fetcher-eventbus";
5
+ import { fetcherRegistrar as G, getFetcher as K } from "@ahoo-wang/fetcher";
6
+ function Q() {
7
+ const e = y(!1), t = u(() => e.current, []);
8
+ return R(() => (e.current = !0, () => {
9
9
  e.current = !1;
10
- }), []), r;
10
+ }), []), t;
11
11
  }
12
- g(v, "useMounted");
13
- function R(e) {
14
- const r = h(e);
15
- return r.current = e, r;
12
+ b(Q, "useMounted");
13
+ function S(e) {
14
+ const t = y(e);
15
+ return t.current = e, t;
16
16
  }
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(
17
+ b(S, "useLatest");
18
+ var T = /* @__PURE__ */ ((e) => (e.IDLE = "idle", e.LOADING = "loading", e.SUCCESS = "success", e.ERROR = "error", e))(T || {});
19
+ function M(e) {
20
+ const [t, n] = F(
21
21
  e?.initialStatus ?? "idle"
22
22
  /* IDLE */
23
- ), [c, i] = w(void 0), [u, t] = w(void 0), o = v(), f = R(e), l = n(() => {
24
- o() && (s(
23
+ ), [c, o] = F(void 0), [a, r] = F(void 0), s = Q(), l = S(e), i = u(() => {
24
+ s() && (n(
25
25
  "loading"
26
26
  /* LOADING */
27
- ), t(void 0));
28
- }, [o]), y = n(
29
- async (d) => {
30
- if (o()) {
31
- i(d), s(
27
+ ), r(void 0));
28
+ }, [s]), E = u(
29
+ async (g) => {
30
+ if (s()) {
31
+ o(g), n(
32
32
  "success"
33
33
  /* SUCCESS */
34
- ), t(void 0);
34
+ ), r(void 0);
35
35
  try {
36
- await f.current?.onSuccess?.(d);
37
- } catch (m) {
38
- console.warn("PromiseState onSuccess callback error:", m);
36
+ await l.current?.onSuccess?.(g);
37
+ } catch (h) {
38
+ console.warn("PromiseState onSuccess callback error:", h);
39
39
  }
40
40
  }
41
41
  },
42
- [o, f]
43
- ), a = n(
44
- async (d) => {
45
- if (o()) {
46
- t(d), s(
42
+ [s, l]
43
+ ), d = u(
44
+ async (g) => {
45
+ if (s()) {
46
+ r(g), n(
47
47
  "error"
48
48
  /* ERROR */
49
- ), i(void 0);
49
+ ), o(void 0);
50
50
  try {
51
- await f.current?.onError?.(d);
52
- } catch (m) {
53
- console.warn("PromiseState onError callback error:", m);
51
+ await l.current?.onError?.(g);
52
+ } catch (h) {
53
+ console.warn("PromiseState onError callback error:", h);
54
54
  }
55
55
  }
56
56
  },
57
- [o, f]
58
- ), E = n(() => {
59
- o() && (s(
57
+ [s, l]
58
+ ), f = u(() => {
59
+ s() && (n(
60
60
  "idle"
61
61
  /* IDLE */
62
- ), t(void 0), i(void 0));
63
- }, [o]);
64
- return b(
62
+ ), r(void 0), o(void 0));
63
+ }, [s]);
64
+ return m(
65
65
  () => ({
66
- status: r,
67
- loading: r === "loading",
66
+ status: t,
67
+ loading: t === "loading",
68
68
  result: c,
69
- error: u,
70
- setLoading: l,
71
- setSuccess: y,
72
- setError: a,
73
- setIdle: E
69
+ error: a,
70
+ setLoading: i,
71
+ setSuccess: E,
72
+ setError: d,
73
+ setIdle: f
74
74
  }),
75
- [r, c, u, l, y, a, E]
75
+ [t, c, a, i, E, d, f]
76
76
  );
77
77
  }
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(() => {
78
+ b(M, "usePromiseState");
79
+ function D() {
80
+ const e = y(0), t = u(() => ++e.current, []), n = u(() => e.current, []), c = u((r) => r === e.current, []), o = u(() => {
81
81
  e.current++;
82
- }, []), u = n(() => {
82
+ }, []), a = u(() => {
83
83
  e.current = 0;
84
84
  }, []);
85
- return b(() => ({
86
- generate: r,
87
- current: s,
85
+ return m(() => ({
86
+ generate: t,
87
+ current: n,
88
88
  isLatest: c,
89
- invalidate: i,
90
- reset: u
91
- }), [r, s, c, i, u]);
89
+ invalidate: o,
90
+ reset: a
91
+ }), [t, n, c, o, a]);
92
92
  }
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
- async (m) => {
97
- if (!l())
93
+ b(D, "useRequestId");
94
+ function O(e) {
95
+ const {
96
+ loading: t,
97
+ result: n,
98
+ error: c,
99
+ status: o,
100
+ setLoading: a,
101
+ setSuccess: r,
102
+ setError: s,
103
+ setIdle: l
104
+ } = M(e), i = Q(), E = D(), d = e?.propagateError, f = u(
105
+ async (h) => {
106
+ if (!i())
98
107
  throw new Error("Component is unmounted");
99
- const p = y.generate();
100
- u();
108
+ const L = E.generate();
109
+ a();
101
110
  try {
102
- const I = await (typeof m == "function" ? m() : m);
103
- return l() && y.isLatest(p) && await t(I), I;
111
+ const w = await (typeof h == "function" ? h() : h);
112
+ return i() && E.isLatest(L) && await r(w), w;
104
113
  } catch (x) {
105
- if (l() && y.isLatest(p) && await o(x), a)
114
+ if (i() && E.isLatest(L) && await s(x), d)
106
115
  throw x;
107
116
  return x;
108
117
  }
109
118
  },
110
- [u, t, o, l, y, a]
111
- ), d = n(() => {
112
- l() && f();
113
- }, [f, l]);
114
- return b(
119
+ [a, r, s, i, E, d]
120
+ ), g = u(() => {
121
+ i() && l();
122
+ }, [l, i]);
123
+ return m(
115
124
  () => ({
116
- loading: r,
117
- result: s,
125
+ loading: t,
126
+ result: n,
118
127
  error: c,
119
- execute: E,
120
- reset: d,
121
- status: i
128
+ status: o,
129
+ execute: f,
130
+ reset: g
122
131
  }),
123
- [r, s, c, E, d, i]
132
+ [t, n, c, o, f, g]
124
133
  );
125
134
  }
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),
135
+ b(O, "useExecutePromise");
136
+ function H() {
137
+ const e = y(/* @__PURE__ */ new Map()), t = u((d) => e.current.get(d), []), n = u(
138
+ (d, f) => e.current.set(d, f),
130
139
  []
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);
140
+ ), c = u((d) => e.current.has(d), []), o = u((d) => e.current.delete(d), []), a = u(() => e.current.clear(), []), r = u(() => e.current.keys(), []), s = u(() => e.current.values(), []), l = u(() => e.current.entries(), []), i = u(() => e.current[Symbol.iterator](), []), E = u((d) => (f) => {
141
+ f ? e.current.set(d, f) : e.current.delete(d);
133
142
  }, []);
134
- return L(() => () => {
143
+ return R(() => () => {
135
144
  e.current.clear();
136
- }, []), b(
145
+ }, []), m(
137
146
  () => ({
138
- register: y,
139
- get: r,
140
- set: s,
147
+ register: E,
148
+ get: t,
149
+ set: n,
141
150
  has: c,
142
- delete: i,
143
- clear: u,
144
- keys: t,
145
- values: o,
146
- entries: f,
151
+ delete: o,
152
+ clear: a,
153
+ keys: r,
154
+ values: s,
155
+ entries: l,
147
156
  get size() {
148
157
  return e.current.size;
149
158
  },
150
- [Symbol.iterator]: l
159
+ [Symbol.iterator]: i
151
160
  }),
152
- [y, r, s, c, i, u, t, o, f, l]
161
+ [E, t, n, c, o, a, r, s, l, i]
153
162
  );
154
163
  }
155
- g(N, "useRefs");
156
- function U(e, r) {
157
- const s = n(
158
- (t) => e.addListener({
159
- name: q.generate("useKeyStorage"),
164
+ b(H, "useRefs");
165
+ function P(e, t) {
166
+ if (t.leading === !1 && t.trailing === !1)
167
+ throw new Error(
168
+ "useDebouncedCallback: at least one of leading or trailing must be true"
169
+ );
170
+ const n = y(
171
+ void 0
172
+ ), c = y(!1), o = S(e), a = S(t), r = u(() => n.current !== void 0, []), s = u(() => {
173
+ n.current !== void 0 && (clearTimeout(n.current), n.current = void 0);
174
+ }, []);
175
+ R(() => () => {
176
+ s();
177
+ }, [s]);
178
+ const l = u(
179
+ (...i) => {
180
+ const { leading: E = !1, trailing: d = !0, delay: f } = a.current;
181
+ s(), E && !c.current && (o.current(...i), c.current = !0), d && (n.current = setTimeout(() => {
182
+ c.current || o.current(...i), c.current = !1, n.current = void 0;
183
+ }, f));
184
+ },
185
+ [o, a, s]
186
+ );
187
+ return m(
188
+ () => ({
189
+ run: l,
190
+ cancel: s,
191
+ isPending: r
192
+ }),
193
+ [l, s, r]
194
+ );
195
+ }
196
+ b(P, "useDebouncedCallback");
197
+ function J(e) {
198
+ const { loading: t, result: n, error: c, execute: o, reset: a, status: r } = O(e), { run: s, cancel: l, isPending: i } = P(
199
+ o,
200
+ e.debounce
201
+ );
202
+ return m(
203
+ () => ({
204
+ loading: t,
205
+ result: n,
206
+ error: c,
207
+ status: r,
208
+ run: s,
209
+ cancel: l,
210
+ isPending: i,
211
+ reset: a
212
+ }),
213
+ [t, n, c, r, s, l, i, a]
214
+ );
215
+ }
216
+ b(J, "useDebouncedExecutePromise");
217
+ function W(e, t) {
218
+ const n = u(
219
+ (r) => e.addListener({
220
+ name: z.generate("useKeyStorage"),
160
221
  // Generate unique listener name
161
- handle: t
222
+ handle: r
162
223
  // Callback to trigger React re-render on storage changes
163
224
  }),
164
225
  [e]
165
226
  // Recreate subscription only if keyStorage changes
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),
227
+ ), c = u(() => {
228
+ const r = e.get();
229
+ return r !== null ? r : t ?? null;
230
+ }, [e, t]), o = A(n, c, c), a = u(
231
+ (r) => e.set(r),
171
232
  [e]
172
233
  // Recreate setter only if keyStorage changes
173
234
  );
174
- return [i, u];
235
+ return [o, a];
175
236
  }
176
- g(U, "useKeyStorage");
177
- function V(e) {
178
- const { fetcher: r = O.default } = e || {}, s = C(e), [c, i] = w(
237
+ b(W, "useKeyStorage");
238
+ function N(e) {
239
+ const { fetcher: t = G.default } = e || {}, {
240
+ loading: n,
241
+ result: c,
242
+ error: o,
243
+ status: a,
244
+ setLoading: r,
245
+ setSuccess: s,
246
+ setError: l,
247
+ setIdle: i
248
+ } = M(e), [E, d] = F(
179
249
  void 0
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();
250
+ ), f = Q(), g = y(void 0), h = D(), L = S(e), x = K(t), w = u(
251
+ async (I) => {
252
+ g.current && g.current.abort(), g.current = I.abortController ?? new AbortController(), I.abortController = g.current;
253
+ const p = h.generate();
254
+ r();
185
255
  try {
186
- const d = await l.exchange(
187
- a,
188
- f.current
256
+ const v = await x.exchange(
257
+ I,
258
+ L.current
189
259
  );
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();
260
+ f() && h.isLatest(p) && d(v);
261
+ const q = await v.extractResult();
262
+ f() && h.isLatest(p) && await s(q);
263
+ } catch (v) {
264
+ if (v instanceof Error && v.name === "AbortError") {
265
+ f() && i();
196
266
  return;
197
267
  }
198
- u() && o.isLatest(E) && await s.setError(d);
268
+ f() && h.isLatest(p) && await l(v);
199
269
  } finally {
200
- t.current === a.abortController && (t.current = void 0);
270
+ g.current === I.abortController && (g.current = void 0);
201
271
  }
202
272
  },
203
- [l, u, f, s, o]
273
+ [
274
+ x,
275
+ f,
276
+ L,
277
+ r,
278
+ s,
279
+ i,
280
+ l,
281
+ h
282
+ ]
204
283
  );
205
- return L(() => () => {
206
- t.current?.abort(), t.current = void 0;
207
- }, []), b(
284
+ return R(() => () => {
285
+ g.current?.abort(), g.current = void 0;
286
+ }, []), m(
208
287
  () => ({
209
- ...s,
210
- exchange: c,
211
- execute: y
288
+ loading: n,
289
+ result: c,
290
+ error: o,
291
+ status: a,
292
+ exchange: E,
293
+ execute: w
212
294
  }),
213
- [s, c, y]
295
+ [n, c, o, a, E, w]
214
296
  );
215
297
  }
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(
298
+ b(N, "useFetcher");
299
+ function X(e) {
300
+ const { loading: t, result: n, error: c, status: o, exchange: a, execute: r } = N(e), { run: s, cancel: l, isPending: i } = P(
301
+ r,
302
+ e.debounce
303
+ );
304
+ return m(
305
+ () => ({
306
+ loading: t,
307
+ result: n,
308
+ error: c,
309
+ status: o,
310
+ exchange: a,
311
+ run: s,
312
+ cancel: l,
313
+ isPending: i
314
+ }),
315
+ [t, n, c, o, a, s, l, i]
316
+ );
317
+ }
318
+ b(X, "useDebouncedFetcher");
319
+ function C(e) {
320
+ const t = S(e), n = O(t.current), c = y(e.initialQuery), o = u(async () => t.current.execute(
219
321
  c.current,
220
- r.current.attributes
221
- ), [c, r]), { execute: u } = s, t = n(() => u(i), [u, i]), o = n(() => c.current, [c]), f = n(
222
- (l) => {
223
- c.current = l, r.current.autoExecute && t();
322
+ t.current.attributes
323
+ ), [c, t]), { execute: a } = n, r = u(() => a(o), [a, o]), s = u(() => c.current, [c]), l = u(
324
+ (i) => {
325
+ c.current = i, t.current.autoExecute && r();
224
326
  },
225
- [c, r, t]
327
+ [c, t, r]
226
328
  );
227
- return L(() => {
228
- r.current.autoExecute && t();
229
- }, [r, t]), b(
329
+ return R(() => {
330
+ t.current.autoExecute && r();
331
+ }, [t, r]), m(
230
332
  () => ({
231
- ...s,
232
- execute: t,
233
- getQuery: o,
234
- setQuery: f
333
+ ...n,
334
+ execute: r,
335
+ getQuery: s,
336
+ setQuery: l
235
337
  }),
236
- [s, t, o, f]
338
+ [n, r, s, l]
237
339
  );
238
340
  }
239
- g(S, "useQuery");
240
- function j(e) {
241
- return S(e);
341
+ b(C, "useQuery");
342
+ function Y(e) {
343
+ return C(e);
242
344
  }
243
- g(j, "usePagedQuery");
244
- function B(e) {
245
- return S(e);
345
+ b(Y, "usePagedQuery");
346
+ function Z(e) {
347
+ return C(e);
246
348
  }
247
- g(B, "useSingleQuery");
248
- function H(e) {
249
- return S(e);
349
+ b(Z, "useSingleQuery");
350
+ function _(e) {
351
+ return C(e);
250
352
  }
251
- g(H, "useCountQuery");
252
- function J(e) {
253
- return S(e);
353
+ b(_, "useCountQuery");
354
+ function $(e) {
355
+ return C(e);
254
356
  }
255
- g(J, "useListQuery");
256
- function T(e) {
257
- return S(
357
+ b($, "useListQuery");
358
+ function ee(e) {
359
+ return C(
258
360
  e
259
361
  );
260
362
  }
261
- g(T, "useListStreamQuery");
363
+ b(ee, "useListStreamQuery");
262
364
  export {
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,
273
- C as usePromiseState,
274
- S as useQuery,
275
- N as useRefs,
276
- F as useRequestId,
277
- B as useSingleQuery
365
+ T as PromiseStatus,
366
+ _ as useCountQuery,
367
+ P as useDebouncedCallback,
368
+ J as useDebouncedExecutePromise,
369
+ X as useDebouncedFetcher,
370
+ O as useExecutePromise,
371
+ N as useFetcher,
372
+ W as useKeyStorage,
373
+ S as useLatest,
374
+ $ as useListQuery,
375
+ ee as useListStreamQuery,
376
+ Q as useMounted,
377
+ Y as usePagedQuery,
378
+ M as usePromiseState,
379
+ C as useQuery,
380
+ H as useRefs,
381
+ D as useRequestId,
382
+ Z as useSingleQuery
278
383
  };
279
384
  //# sourceMappingURL=index.es.js.map