@ahoo-wang/fetcher-react 2.15.2 → 2.15.5

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