@ahoo-wang/fetcher-react 3.1.3 → 3.1.6

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.
@@ -6,4 +6,5 @@ export * from './useMounted';
6
6
  export * from './useRefs';
7
7
  export * from './useDebouncedCallback';
8
8
  export * from './useDebouncedExecutePromise';
9
+ export * from './useForceUpdate';
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * A React hook that returns a function to force a component to re-render.
3
+ * This is useful when you need to trigger a re-render imperatively, such as
4
+ * when dealing with external state changes or imperative updates.
5
+ *
6
+ * @returns A function that when called, forces the component to re-render
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { useForceUpdate } from '@ahoo-wang/fetcher-react';
11
+ *
12
+ * const MyComponent = () => {
13
+ * const forceUpdate = useForceUpdate();
14
+ *
15
+ * const handleExternalChange = () => {
16
+ * // Some external state change that doesn't trigger React re-render
17
+ * externalLibrary.updateSomething();
18
+ * forceUpdate(); // Force re-render to reflect changes
19
+ * };
20
+ *
21
+ * return (
22
+ * <div>
23
+ * <p>Component state: {someValue}</p>
24
+ * <button onClick={handleExternalChange}>Update External State</button>
25
+ * </div>
26
+ * );
27
+ * };
28
+ * ```
29
+ */
30
+ export declare function useForceUpdate(): () => void;
31
+ //# sourceMappingURL=useForceUpdate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useForceUpdate.d.ts","sourceRoot":"","sources":["../../src/core/useForceUpdate.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,cAAc,IAAI,MAAM,IAAI,CAG3C"}
package/dist/index.es.js CHANGED
@@ -1,15 +1,15 @@
1
1
  var k = Object.defineProperty;
2
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";
3
+ import { useRef as m, useCallback as u, useEffect as C, useState as Q, useMemo as h, useReducer as A, useSyncExternalStore as U } 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 M() {
6
+ function p() {
7
7
  const e = m(!1), t = u(() => e.current, []);
8
8
  return C(() => (e.current = !0, () => {
9
9
  e.current = !1;
10
10
  }), []), t;
11
11
  }
12
- y(M, "useMounted");
12
+ y(p, "useMounted");
13
13
  function S(e) {
14
14
  const t = m(e);
15
15
  return t.current = e, t;
@@ -20,14 +20,14 @@ function D(e) {
20
20
  const [t, r] = Q(
21
21
  e?.initialStatus ?? "idle"
22
22
  /* IDLE */
23
- ), [c, o] = Q(void 0), [i, n] = Q(void 0), s = M(), a = S(e), l = u(() => {
24
- s() && (r(
23
+ ), [s, o] = Q(void 0), [i, n] = Q(void 0), c = p(), a = S(e), l = u(() => {
24
+ c() && (r(
25
25
  "loading"
26
26
  /* LOADING */
27
27
  ), n(void 0));
28
- }, [s]), f = u(
28
+ }, [c]), f = u(
29
29
  async (b) => {
30
- if (s()) {
30
+ if (c()) {
31
31
  o(b), r(
32
32
  "success"
33
33
  /* SUCCESS */
@@ -39,10 +39,10 @@ function D(e) {
39
39
  }
40
40
  }
41
41
  },
42
- [s, a]
42
+ [c, a]
43
43
  ), d = u(
44
44
  async (b) => {
45
- if (s()) {
45
+ if (c()) {
46
46
  n(b), r(
47
47
  "error"
48
48
  /* ERROR */
@@ -54,30 +54,30 @@ function D(e) {
54
54
  }
55
55
  }
56
56
  },
57
- [s, a]
57
+ [c, a]
58
58
  ), g = u(() => {
59
- s() && (r(
59
+ c() && (r(
60
60
  "idle"
61
61
  /* IDLE */
62
62
  ), n(void 0), o(void 0));
63
- }, [s]);
63
+ }, [c]);
64
64
  return h(
65
65
  () => ({
66
66
  status: t,
67
67
  loading: t === "loading",
68
- result: c,
68
+ result: s,
69
69
  error: i,
70
70
  setLoading: l,
71
71
  setSuccess: f,
72
72
  setError: d,
73
73
  setIdle: g
74
74
  }),
75
- [t, c, i, l, f, d, g]
75
+ [t, s, i, l, f, d, g]
76
76
  );
77
77
  }
78
78
  y(D, "usePromiseState");
79
79
  function P() {
80
- const e = m(0), t = u(() => ++e.current, []), r = u(() => e.current, []), c = u((n) => n === e.current, []), o = u(() => {
80
+ const e = m(0), t = u(() => ++e.current, []), r = u(() => e.current, []), s = u((n) => n === e.current, []), o = u(() => {
81
81
  e.current++;
82
82
  }, []), i = u(() => {
83
83
  e.current = 0;
@@ -85,23 +85,23 @@ function P() {
85
85
  return h(() => ({
86
86
  generate: t,
87
87
  current: r,
88
- isLatest: c,
88
+ isLatest: s,
89
89
  invalidate: o,
90
90
  reset: i
91
- }), [t, r, c, o, i]);
91
+ }), [t, r, s, o, i]);
92
92
  }
93
93
  y(P, "useRequestId");
94
94
  function O(e) {
95
95
  const {
96
96
  loading: t,
97
97
  result: r,
98
- error: c,
98
+ error: s,
99
99
  status: o,
100
100
  setLoading: i,
101
101
  setSuccess: n,
102
- setError: s,
102
+ setError: c,
103
103
  setIdle: a
104
- } = D(e), l = M(), f = P(), d = e?.propagateError, g = u(
104
+ } = D(e), l = p(), f = P(), d = e?.propagateError, g = u(
105
105
  async (E) => {
106
106
  if (!l())
107
107
  throw new Error("Component is unmounted");
@@ -111,12 +111,12 @@ function O(e) {
111
111
  const R = await (typeof E == "function" ? E() : E);
112
112
  return l() && f.isLatest(w) && await n(R), R;
113
113
  } catch (x) {
114
- if (l() && f.isLatest(w) && await s(x), d)
114
+ if (l() && f.isLatest(w) && await c(x), d)
115
115
  throw x;
116
116
  return x;
117
117
  }
118
118
  },
119
- [i, n, s, l, f, d]
119
+ [i, n, c, l, f, d]
120
120
  ), b = u(() => {
121
121
  l() && a();
122
122
  }, [a, l]);
@@ -124,20 +124,20 @@ function O(e) {
124
124
  () => ({
125
125
  loading: t,
126
126
  result: r,
127
- error: c,
127
+ error: s,
128
128
  status: o,
129
129
  execute: g,
130
130
  reset: b
131
131
  }),
132
- [t, r, c, o, g, b]
132
+ [t, r, s, o, g, b]
133
133
  );
134
134
  }
135
135
  y(O, "useExecutePromise");
136
- function H() {
136
+ function J() {
137
137
  const e = m(/* @__PURE__ */ new Map()), t = u((d) => e.current.get(d), []), r = u(
138
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), []), 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) => {
140
+ ), s = u((d) => e.current.has(d), []), o = u((d) => e.current.delete(d), []), i = u(() => e.current.clear(), []), n = u(() => e.current.keys(), []), c = u(() => e.current.values(), []), a = u(() => e.current.entries(), []), l = u(() => e.current[Symbol.iterator](), []), f = u((d) => (g) => {
141
141
  g ? e.current.set(d, g) : e.current.delete(d);
142
142
  }, []);
143
143
  return C(() => () => {
@@ -147,55 +147,55 @@ function H() {
147
147
  register: f,
148
148
  get: t,
149
149
  set: r,
150
- has: c,
150
+ has: s,
151
151
  delete: o,
152
152
  clear: i,
153
153
  keys: n,
154
- values: s,
154
+ values: c,
155
155
  entries: a,
156
156
  get size() {
157
157
  return e.current.size;
158
158
  },
159
159
  [Symbol.iterator]: l
160
160
  }),
161
- [f, t, r, c, o, i, n, s, a, l]
161
+ [f, t, r, s, o, i, n, c, a, l]
162
162
  );
163
163
  }
164
- y(H, "useRefs");
165
- function p(e, t) {
164
+ y(J, "useRefs");
165
+ function M(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
170
  const r = m(
171
171
  void 0
172
- ), c = m(!1), o = S(e), i = S(t), n = u(() => r.current !== void 0, []), s = u(() => {
172
+ ), s = m(!1), o = S(e), i = S(t), n = u(() => r.current !== void 0, []), c = u(() => {
173
173
  r.current !== void 0 && (clearTimeout(r.current), r.current = void 0);
174
174
  }, []);
175
175
  C(() => () => {
176
- s();
177
- }, [s]);
176
+ c();
177
+ }, [c]);
178
178
  const a = u(
179
179
  (...l) => {
180
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;
181
+ c(), f && !s.current && (o.current(...l), s.current = !0), d && (r.current = setTimeout(() => {
182
+ s.current || o.current(...l), s.current = !1, r.current = void 0;
183
183
  }, g));
184
184
  },
185
- [o, i, s]
185
+ [o, i, c]
186
186
  );
187
187
  return h(
188
188
  () => ({
189
189
  run: a,
190
- cancel: s,
190
+ cancel: c,
191
191
  isPending: n
192
192
  }),
193
- [a, s, n]
193
+ [a, c, n]
194
194
  );
195
195
  }
196
- y(p, "useDebouncedCallback");
197
- function J(e) {
198
- const { loading: t, result: r, error: c, execute: o, reset: i, status: n } = O(e), { run: s, cancel: a, isPending: l } = p(
196
+ y(M, "useDebouncedCallback");
197
+ function W(e) {
198
+ const { loading: t, result: r, error: s, execute: o, reset: i, status: n } = O(e), { run: c, cancel: a, isPending: l } = M(
199
199
  o,
200
200
  e.debounce
201
201
  );
@@ -203,18 +203,23 @@ function J(e) {
203
203
  () => ({
204
204
  loading: t,
205
205
  result: r,
206
- error: c,
206
+ error: s,
207
207
  status: n,
208
- run: s,
208
+ run: c,
209
209
  cancel: a,
210
210
  isPending: l,
211
211
  reset: i
212
212
  }),
213
- [t, r, c, n, s, a, l, i]
213
+ [t, r, s, n, c, a, l, i]
214
214
  );
215
215
  }
216
- y(J, "useDebouncedExecutePromise");
217
- function W(e, t) {
216
+ y(W, "useDebouncedExecutePromise");
217
+ function X() {
218
+ const [, e] = A((t) => t + 1, 0);
219
+ return e;
220
+ }
221
+ y(X, "useForceUpdate");
222
+ function Y(e, t) {
218
223
  const r = u(
219
224
  (n) => e.addListener({
220
225
  name: z.generate("useKeyStorage"),
@@ -224,30 +229,30 @@ function W(e, t) {
224
229
  }),
225
230
  [e]
226
231
  // Recreate subscription only if keyStorage changes
227
- ), c = u(() => {
232
+ ), s = u(() => {
228
233
  const n = e.get();
229
234
  return n !== null ? n : t ?? null;
230
- }, [e, t]), o = A(r, c, c), i = u(
235
+ }, [e, t]), o = U(r, s, s), i = u(
231
236
  (n) => e.set(n),
232
237
  [e]
233
238
  // Recreate setter only if keyStorage changes
234
239
  );
235
240
  return [o, i];
236
241
  }
237
- y(W, "useKeyStorage");
242
+ y(Y, "useKeyStorage");
238
243
  function N(e) {
239
244
  const { fetcher: t = G.default } = e || {}, {
240
245
  loading: r,
241
- result: c,
246
+ result: s,
242
247
  error: o,
243
248
  status: i,
244
249
  setLoading: n,
245
- setSuccess: s,
250
+ setSuccess: c,
246
251
  setError: a,
247
252
  setIdle: l
248
253
  } = D(e), [f, d] = Q(
249
254
  void 0
250
- ), g = M(), b = m(void 0), E = P(), w = S(e), x = K(t), R = u(
255
+ ), g = p(), b = m(void 0), E = P(), w = S(e), x = K(t), R = u(
251
256
  async (I) => {
252
257
  b.current && b.current.abort(), b.current = I.abortController ?? new AbortController(), I.abortController = b.current;
253
258
  const F = E.generate();
@@ -259,7 +264,7 @@ function N(e) {
259
264
  );
260
265
  g() && E.isLatest(F) && d(v);
261
266
  const q = await v.extractResult();
262
- g() && E.isLatest(F) && await s(q);
267
+ g() && E.isLatest(F) && await c(q);
263
268
  } catch (v) {
264
269
  if (v instanceof Error && v.name === "AbortError") {
265
270
  g() && l();
@@ -275,7 +280,7 @@ function N(e) {
275
280
  g,
276
281
  w,
277
282
  n,
278
- s,
283
+ c,
279
284
  l,
280
285
  a,
281
286
  E
@@ -286,18 +291,18 @@ function N(e) {
286
291
  }, []), h(
287
292
  () => ({
288
293
  loading: r,
289
- result: c,
294
+ result: s,
290
295
  error: o,
291
296
  status: i,
292
297
  exchange: f,
293
298
  execute: R
294
299
  }),
295
- [r, c, o, i, f, R]
300
+ [r, s, o, i, f, R]
296
301
  );
297
302
  }
298
303
  y(N, "useFetcher");
299
- function X(e) {
300
- const { loading: t, result: r, error: c, status: o, exchange: i, execute: n } = N(e), { run: s, cancel: a, isPending: l } = p(
304
+ function Z(e) {
305
+ const { loading: t, result: r, error: s, status: o, exchange: i, execute: n } = N(e), { run: c, cancel: a, isPending: l } = M(
301
306
  n,
302
307
  e.debounce
303
308
  );
@@ -305,25 +310,25 @@ function X(e) {
305
310
  () => ({
306
311
  loading: t,
307
312
  result: r,
308
- error: c,
313
+ error: s,
309
314
  status: o,
310
315
  exchange: i,
311
- run: s,
316
+ run: c,
312
317
  cancel: a,
313
318
  isPending: l
314
319
  }),
315
- [t, r, c, o, i, s, a, l]
320
+ [t, r, s, o, i, c, a, l]
316
321
  );
317
322
  }
318
- y(X, "useDebouncedFetcher");
323
+ y(Z, "useDebouncedFetcher");
319
324
  function L(e) {
320
325
  const t = S(e), {
321
326
  loading: r,
322
- result: c,
327
+ result: s,
323
328
  error: o,
324
329
  status: i,
325
330
  execute: n,
326
- reset: s
331
+ reset: c
327
332
  } = O(t.current), a = m(e.initialQuery), l = u(async () => t.current.execute(
328
333
  a.current,
329
334
  t.current.attributes
@@ -338,51 +343,51 @@ function L(e) {
338
343
  }, [t, f]), h(
339
344
  () => ({
340
345
  loading: r,
341
- result: c,
346
+ result: s,
342
347
  error: o,
343
348
  status: i,
344
349
  execute: f,
345
- reset: s,
350
+ reset: c,
346
351
  getQuery: d,
347
352
  setQuery: g
348
353
  }),
349
354
  [
350
355
  r,
351
- c,
356
+ s,
352
357
  o,
353
358
  i,
354
359
  f,
355
- s,
360
+ c,
356
361
  d,
357
362
  g
358
363
  ]
359
364
  );
360
365
  }
361
366
  y(L, "useQuery");
362
- function Y(e) {
367
+ function _(e) {
363
368
  return L(e);
364
369
  }
365
- y(Y, "usePagedQuery");
366
- function Z(e) {
370
+ y(_, "usePagedQuery");
371
+ function $(e) {
367
372
  return L(e);
368
373
  }
369
- y(Z, "useSingleQuery");
370
- function _(e) {
374
+ y($, "useSingleQuery");
375
+ function ee(e) {
371
376
  return L(e);
372
377
  }
373
- y(_, "useCountQuery");
374
- function $(e) {
378
+ y(ee, "useCountQuery");
379
+ function te(e) {
375
380
  return L(e);
376
381
  }
377
- y($, "useListQuery");
378
- function ee(e) {
382
+ y(te, "useListQuery");
383
+ function re(e) {
379
384
  return L(
380
385
  e
381
386
  );
382
387
  }
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(
388
+ y(re, "useListStreamQuery");
389
+ function ne(e) {
390
+ const { loading: t, result: r, error: s, status: o, execute: i, reset: n, getQuery: c, setQuery: a } = L(e), { run: l, cancel: f, isPending: d } = M(
386
391
  i,
387
392
  e.debounce
388
393
  );
@@ -390,10 +395,10 @@ function te(e) {
390
395
  () => ({
391
396
  loading: t,
392
397
  result: r,
393
- error: c,
398
+ error: s,
394
399
  status: o,
395
400
  reset: n,
396
- getQuery: s,
401
+ getQuery: c,
397
402
  setQuery: a,
398
403
  run: l,
399
404
  cancel: f,
@@ -402,10 +407,10 @@ function te(e) {
402
407
  [
403
408
  t,
404
409
  r,
405
- c,
410
+ s,
406
411
  o,
407
412
  n,
408
- s,
413
+ c,
409
414
  a,
410
415
  l,
411
416
  f,
@@ -413,26 +418,27 @@ function te(e) {
413
418
  ]
414
419
  );
415
420
  }
416
- y(te, "useDebouncedQuery");
421
+ y(ne, "useDebouncedQuery");
417
422
  export {
418
423
  T as PromiseStatus,
419
- _ as useCountQuery,
420
- p as useDebouncedCallback,
421
- J as useDebouncedExecutePromise,
422
- X as useDebouncedFetcher,
423
- te as useDebouncedQuery,
424
+ ee as useCountQuery,
425
+ M as useDebouncedCallback,
426
+ W as useDebouncedExecutePromise,
427
+ Z as useDebouncedFetcher,
428
+ ne as useDebouncedQuery,
424
429
  O as useExecutePromise,
425
430
  N as useFetcher,
426
- W as useKeyStorage,
431
+ X as useForceUpdate,
432
+ Y as useKeyStorage,
427
433
  S as useLatest,
428
- $ as useListQuery,
429
- ee as useListStreamQuery,
430
- M as useMounted,
431
- Y as usePagedQuery,
434
+ te as useListQuery,
435
+ re as useListStreamQuery,
436
+ p as useMounted,
437
+ _ as usePagedQuery,
432
438
  D as usePromiseState,
433
439
  L as useQuery,
434
- H as useRefs,
440
+ J as useRefs,
435
441
  P as useRequestId,
436
- Z as useSingleQuery
442
+ $ as useSingleQuery
437
443
  };
438
444
  //# sourceMappingURL=index.es.js.map