@hyper-fetch/react 7.5.2 → 8.0.0
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/.eslintrc.cjs +12 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +115 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +69 -0
- package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +213 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +550 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +566 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +260 -0
- package/__tests__/features/provider/provider.hydration.spec.tsx +165 -0
- package/__tests__/features/provider/use-config-provider.base.spec.tsx +69 -0
- package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +85 -0
- package/__tests__/features/use-cache/use-cache.base.spec.ts +136 -0
- package/__tests__/features/use-cache/use-cache.helpers.ts +40 -0
- package/__tests__/features/use-cache/use-cache.typescript.spec.ts +111 -0
- package/__tests__/features/use-emitter/use-emitter.base.spec.ts +124 -0
- package/__tests__/features/use-fetch/use-fetch.base.spec.ts +372 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +83 -0
- package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +82 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +91 -0
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +153 -0
- package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +80 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.ts +39 -0
- package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +172 -0
- package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +233 -0
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +103 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +36 -0
- package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +73 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +187 -0
- package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +151 -0
- package/__tests__/features/use-fetch/use-fetch.retry.ts +38 -0
- package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +237 -0
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +505 -0
- package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +182 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +26 -0
- package/__tests__/features/use-fetch/use-fetch.window.spec.ts +131 -0
- package/__tests__/features/use-listener/use-listener.base.spec.ts +57 -0
- package/__tests__/features/use-queue/use-queue.actions.spec.ts +238 -0
- package/__tests__/features/use-queue/use-queue.base.spec.ts +367 -0
- package/__tests__/features/use-queue/use-queue.resolve.spec.ts +173 -0
- package/__tests__/features/use-stream/use-stream.base.spec.ts +481 -0
- package/__tests__/features/use-submit/use-submit.base.spec.ts +76 -0
- package/__tests__/features/use-submit/use-submit.cancel.spec.ts +46 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +62 -0
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +320 -0
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +276 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +548 -0
- package/__tests__/features/use-submit/use-submit.queue.spec.ts +84 -0
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +92 -0
- package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +99 -0
- package/__tests__/features/use-submit/use-submit.submit.spec.ts +234 -0
- package/__tests__/features/use-submit/use-submit.typescript.spec.ts +325 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/isolation.tests.ts +12 -0
- package/__tests__/shared/response.tests.ts +128 -0
- package/__tests__/tsconfig.json +27 -0
- package/__tests__/utils/cache.utils.ts +55 -0
- package/__tests__/utils/client.utils.ts +3 -0
- package/__tests__/utils/emitter.utils.ts +8 -0
- package/__tests__/utils/helpers.utils.ts +9 -0
- package/__tests__/utils/index.ts +14 -0
- package/__tests__/utils/listener.utils.ts +11 -0
- package/__tests__/utils/request.utils.ts +12 -0
- package/__tests__/utils/state.utils.ts +7 -0
- package/__tests__/utils/use-app-manager.utils.ts +10 -0
- package/__tests__/utils/use-cache.utils.ts +12 -0
- package/__tests__/utils/use-emitter.utils.ts +12 -0
- package/__tests__/utils/use-fetch.utils.ts +12 -0
- package/__tests__/utils/use-listener.utils.ts +12 -0
- package/__tests__/utils/use-queue.utils.ts +70 -0
- package/__tests__/utils/use-request-events.utils.ts +43 -0
- package/__tests__/utils/use-socket-state.utils.ts +12 -0
- package/__tests__/utils/use-submit.utils.ts +12 -0
- package/__tests__/utils/use-tracked-state.utils.ts +27 -0
- package/__tests__/vitest.setup.ts +8 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/index.d.ts +3 -0
- package/dist/helpers/use-request-events/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +10 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +78 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/index.d.ts +4 -0
- package/dist/helpers/use-socket-state/index.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts +4 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts +24 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts +12 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/index.d.ts +5 -0
- package/dist/helpers/use-tracked-state/index.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +3 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +12 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +113 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +29 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/index.d.ts +3 -0
- package/dist/hooks/use-app-manager/index.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +4 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +19 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts.map +1 -0
- package/dist/hooks/use-cache/index.d.ts +4 -0
- package/dist/hooks/use-cache/index.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts +6 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +4 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts +24 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -0
- package/dist/hooks/use-emitter/index.d.ts +3 -0
- package/dist/hooks/use-emitter/index.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +26 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts +4 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/index.d.ts +5 -0
- package/dist/hooks/use-fetch/index.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts +8 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +10 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts +118 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts +2 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts.map +1 -0
- package/dist/hooks/use-listener/index.d.ts +3 -0
- package/dist/hooks/use-listener/index.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts +31 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts +4 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -0
- package/dist/hooks/use-queue/index.d.ts +4 -0
- package/dist/hooks/use-queue/index.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts +4 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +10 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts +58 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -0
- package/dist/hooks/use-stream/index.d.ts +3 -0
- package/dist/hooks/use-stream/index.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts +11 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts +52 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts.map +1 -0
- package/dist/hooks/use-submit/index.d.ts +4 -0
- package/dist/hooks/use-submit/index.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts +8 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +10 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts +115 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -0
- package/dist/index.d.ts +12 -733
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1935 -0
- package/dist/index.mjs.map +1 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.d.ts.map +1 -0
- package/dist/provider/provider.d.ts +13 -0
- package/dist/provider/provider.d.ts.map +1 -0
- package/dist/provider/provider.types.d.ts +31 -0
- package/dist/provider/provider.types.d.ts.map +1 -0
- package/dist/utils/bounce.utils.d.ts +8 -0
- package/dist/utils/bounce.utils.d.ts.map +1 -0
- package/dist/utils/deep-equal.utils.d.ts +14 -0
- package/dist/utils/deep-equal.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/tracked-proxy.utils.d.ts +7 -0
- package/dist/utils/tracked-proxy.utils.d.ts.map +1 -0
- package/package.json +21 -13
- package/project.json +4 -0
- package/src/helpers/use-request-events/use-request-events.hooks.ts +60 -14
- package/src/helpers/use-request-events/use-request-events.types.ts +19 -12
- package/src/helpers/use-socket-state/use-socket-state.constants.ts +2 -2
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +47 -13
- package/src/helpers/use-socket-state/use-socket-state.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +74 -22
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +7 -2
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +39 -4
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
- package/src/hooks/use-cache/use-cache.hooks.ts +33 -41
- package/src/hooks/use-cache/use-cache.types.ts +3 -3
- package/src/hooks/use-emitter/use-emitter.hooks.ts +17 -16
- package/src/hooks/use-fetch/use-fetch.constants.ts +5 -2
- package/src/hooks/use-fetch/use-fetch.hooks.ts +109 -51
- package/src/hooks/use-fetch/use-fetch.types.ts +18 -3
- package/src/hooks/use-listener/use-listener.hooks.ts +19 -27
- package/src/hooks/use-queue/use-queue.constants.ts +2 -2
- package/src/hooks/use-queue/use-queue.hooks.ts +25 -6
- package/src/hooks/use-queue/use-queue.types.ts +8 -2
- package/src/hooks/use-stream/index.ts +2 -0
- package/src/hooks/use-stream/use-stream.hooks.ts +164 -0
- package/src/hooks/use-stream/use-stream.types.ts +53 -0
- package/src/hooks/use-submit/use-submit.constants.ts +2 -2
- package/src/hooks/use-submit/use-submit.hooks.ts +43 -48
- package/src/hooks/use-submit/use-submit.types.ts +9 -9
- package/src/index.ts +1 -0
- package/src/provider/provider.tsx +1 -1
- package/src/provider/provider.types.ts +8 -8
- package/src/utils/index.ts +1 -0
- package/src/utils/tracked-proxy.utils.ts +19 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/dist/index.js +0 -1541
- package/dist/index.js.map +0 -7
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1935 @@
|
|
|
1
|
+
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { Request, Time, getRequestDispatcher, scopeKey, sendRequest } from "@hyper-fetch/core";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region ../../node_modules/@better-hooks/lifecycle/dist/index.esm.js
|
|
5
|
+
var o = function(t) {
|
|
6
|
+
var u = useRef(!1);
|
|
7
|
+
useEffect((function() {
|
|
8
|
+
if (!u.current) return u.current = !0, t();
|
|
9
|
+
}), []);
|
|
10
|
+
}, i = function(t, u, c) {
|
|
11
|
+
void 0 === c && (c = !1);
|
|
12
|
+
var e = useRef(c);
|
|
13
|
+
useEffect((function() {
|
|
14
|
+
if (e.current) return t();
|
|
15
|
+
e.current = !0;
|
|
16
|
+
}), u);
|
|
17
|
+
}, f$1 = function() {
|
|
18
|
+
var r = function(r, n) {
|
|
19
|
+
var t = "function" == typeof Symbol && r[Symbol.iterator];
|
|
20
|
+
if (!t) return r;
|
|
21
|
+
var u, c, e = t.call(r), o = [];
|
|
22
|
+
try {
|
|
23
|
+
for (; (void 0 === n || n-- > 0) && !(u = e.next()).done;) o.push(u.value);
|
|
24
|
+
} catch (r) {
|
|
25
|
+
c = { error: r };
|
|
26
|
+
} finally {
|
|
27
|
+
try {
|
|
28
|
+
u && !u.done && (t = e.return) && t.call(e);
|
|
29
|
+
} finally {
|
|
30
|
+
if (c) throw c.error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return o;
|
|
34
|
+
}(useState(0), 2)[1];
|
|
35
|
+
return useCallback((function() {
|
|
36
|
+
r((function(r) {
|
|
37
|
+
return r + 1;
|
|
38
|
+
}));
|
|
39
|
+
}), []);
|
|
40
|
+
}, d$1 = function(r) {
|
|
41
|
+
useEffect((function() {
|
|
42
|
+
return r;
|
|
43
|
+
}), []);
|
|
44
|
+
}, f = function(e) {
|
|
45
|
+
var u = (e || {}).delay, c = void 0 === u ? 400 : u, i = useRef(!1), l = useRef(null), f = f$1(), a = useCallback((function() {
|
|
46
|
+
i.current && f();
|
|
47
|
+
}), [f]), v = function() {
|
|
48
|
+
null !== l.current && clearTimeout(l.current), l.current = null;
|
|
49
|
+
}, d = useCallback((function(n, t) {
|
|
50
|
+
var r = null != t ? t : c;
|
|
51
|
+
v(), l.current = setTimeout((function() {
|
|
52
|
+
l.current = null, n(), a();
|
|
53
|
+
}), r), a();
|
|
54
|
+
}), [c, a]);
|
|
55
|
+
return useEffect((function() {
|
|
56
|
+
return v;
|
|
57
|
+
}), []), {
|
|
58
|
+
get active() {
|
|
59
|
+
return i.current = !0, !!l.current;
|
|
60
|
+
},
|
|
61
|
+
debounce: d,
|
|
62
|
+
reset: v
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
function v(n, t) {
|
|
66
|
+
var r = "function" == typeof Symbol && n[Symbol.iterator];
|
|
67
|
+
if (!r) return n;
|
|
68
|
+
var e, u, c = r.call(n), o = [];
|
|
69
|
+
try {
|
|
70
|
+
for (; (void 0 === t || t-- > 0) && !(e = c.next()).done;) o.push(e.value);
|
|
71
|
+
} catch (n) {
|
|
72
|
+
u = { error: n };
|
|
73
|
+
} finally {
|
|
74
|
+
try {
|
|
75
|
+
e && !e.done && (r = c.return) && r.call(c);
|
|
76
|
+
} finally {
|
|
77
|
+
if (u) throw u.error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return o;
|
|
81
|
+
}
|
|
82
|
+
var y = function(n) {
|
|
83
|
+
var t = Object.prototype.toString.call(n);
|
|
84
|
+
return Array.isArray(n) ? !n.length : "object" == typeof n && null !== n && "[object Object]" === t && !Object.keys(n).length;
|
|
85
|
+
}, m = function(n, t) {
|
|
86
|
+
var r, e = Object.prototype.toString.call(n), u = Object.prototype.toString.call(t), c = typeof n, o = typeof t, i = function(n) {
|
|
87
|
+
return c === n && o === n;
|
|
88
|
+
};
|
|
89
|
+
return e === u && (null === n && null === t || !!(i("number") && Number.isNaN(n) && Number.isNaN(t)) || !(!y(n) || !y(t)) || (Array.isArray(n) && Array.isArray(t) ? n.length === t.length && !n.some((function(n, r) {
|
|
90
|
+
return !m(n, t[r]);
|
|
91
|
+
})) : i("object") && e === (r = "[object Object]") && u === r ? Object.keys(n).length === Object.keys(t).length && !Object.entries(n).some((function(n) {
|
|
92
|
+
var r = v(n, 2), e = r[0], u = r[1];
|
|
93
|
+
return !m(u, t[e]);
|
|
94
|
+
})) : n instanceof Date && t instanceof Date ? +n == +t : n === t));
|
|
95
|
+
}, p = function(t) {
|
|
96
|
+
var e = t || {}, u = e.interval, c = void 0 === u ? 200 : u, i = e.timeout, l = void 0 === i ? 200 : i, f = useRef(0), a = useRef(!0), v = useRef(!1), d = useRef(null), y = f$1(), m = function() {
|
|
97
|
+
a.current && (a.current = !1), v.current && y();
|
|
98
|
+
}, s = function() {
|
|
99
|
+
null !== d.current && clearTimeout(d.current), d.current = null;
|
|
100
|
+
};
|
|
101
|
+
return useEffect((function() {
|
|
102
|
+
return s;
|
|
103
|
+
}), []), {
|
|
104
|
+
get active() {
|
|
105
|
+
return v.current = !0, !!d.current;
|
|
106
|
+
},
|
|
107
|
+
throttle: function(n, t) {
|
|
108
|
+
var r, e, u = function() {
|
|
109
|
+
f.current = Date.now(), n(), m();
|
|
110
|
+
}, o = null !== (r = null == t ? void 0 : t.interval) && void 0 !== r ? r : c, i = null !== (e = null == t ? void 0 : t.timeout) && void 0 !== e ? e : l, v = Date.now() >= f.current + o;
|
|
111
|
+
a.current && m(), d.current && s(), v ? u() : i && (d.current = setTimeout((function() {
|
|
112
|
+
d.current = null, a.current = !0, u();
|
|
113
|
+
}), i));
|
|
114
|
+
},
|
|
115
|
+
reset: s
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
"undefined" == typeof window || void 0 === window.document || window.document.createElement;
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/helpers/use-request-events/use-request-events.hooks.ts
|
|
121
|
+
/**
|
|
122
|
+
* This is helper hook that handles main Hyper-Fetch event/data flow
|
|
123
|
+
* @internal
|
|
124
|
+
* @param options
|
|
125
|
+
* @returns
|
|
126
|
+
*/
|
|
127
|
+
var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, getIsDataProcessing }) => {
|
|
128
|
+
const { unstable_responseMapper } = request;
|
|
129
|
+
const { cache, requestManager } = request.client;
|
|
130
|
+
const onSuccessCallback = useRef(null);
|
|
131
|
+
const onErrorCallback = useRef(null);
|
|
132
|
+
const onAbortCallback = useRef(null);
|
|
133
|
+
const onOfflineErrorCallback = useRef(null);
|
|
134
|
+
const onFinishedCallback = useRef(null);
|
|
135
|
+
const onRequestStartCallback = useRef(null);
|
|
136
|
+
const onResponseStartCallback = useRef(null);
|
|
137
|
+
const onDownloadProgressCallback = useRef(null);
|
|
138
|
+
const onUploadProgressCallback = useRef(null);
|
|
139
|
+
const lifecycleEvents = useRef(/* @__PURE__ */ new Map());
|
|
140
|
+
const dataEvents = useRef(null);
|
|
141
|
+
const removeLifecycleListener = (requestId) => {
|
|
142
|
+
lifecycleEvents.current.get(requestId)?.unmount();
|
|
143
|
+
lifecycleEvents.current.delete(requestId);
|
|
144
|
+
};
|
|
145
|
+
const clearLifecycleListeners = () => {
|
|
146
|
+
const events = lifecycleEvents.current;
|
|
147
|
+
Array.from(events.values()).forEach((value) => {
|
|
148
|
+
value.unmount();
|
|
149
|
+
});
|
|
150
|
+
events.clear();
|
|
151
|
+
};
|
|
152
|
+
const optimisticResultsRef = useRef(/* @__PURE__ */ new Map());
|
|
153
|
+
const handleResponseCallbacks = (values) => {
|
|
154
|
+
const { success } = values.response;
|
|
155
|
+
const { isOffline, isCanceled, willRetry } = values.details;
|
|
156
|
+
const opt = optimisticResultsRef.current.get(values.requestId);
|
|
157
|
+
const paramsWithContext = {
|
|
158
|
+
...values,
|
|
159
|
+
mutationContext: opt?.context
|
|
160
|
+
};
|
|
161
|
+
if (request.offline && isOffline && !success) {
|
|
162
|
+
logger.debug({
|
|
163
|
+
title: "Performing offline error callback",
|
|
164
|
+
type: "system",
|
|
165
|
+
extra: values
|
|
166
|
+
});
|
|
167
|
+
onOfflineErrorCallback.current?.(paramsWithContext);
|
|
168
|
+
} else if (isCanceled) {
|
|
169
|
+
logger.debug({
|
|
170
|
+
title: "Performing abort callback",
|
|
171
|
+
type: "system",
|
|
172
|
+
extra: values
|
|
173
|
+
});
|
|
174
|
+
try {
|
|
175
|
+
opt?.rollback?.();
|
|
176
|
+
} catch {}
|
|
177
|
+
onAbortCallback.current?.(paramsWithContext);
|
|
178
|
+
} else if (success) {
|
|
179
|
+
logger.debug({
|
|
180
|
+
title: "Performing success callback",
|
|
181
|
+
type: "system",
|
|
182
|
+
extra: values
|
|
183
|
+
});
|
|
184
|
+
if (opt?.invalidate) opt.invalidate.forEach((req) => cache.invalidate(req));
|
|
185
|
+
onSuccessCallback.current?.(paramsWithContext);
|
|
186
|
+
} else {
|
|
187
|
+
logger.debug({
|
|
188
|
+
title: "Performing error callback",
|
|
189
|
+
type: "system",
|
|
190
|
+
extra: values
|
|
191
|
+
});
|
|
192
|
+
if (!willRetry) try {
|
|
193
|
+
opt?.rollback?.();
|
|
194
|
+
} catch {}
|
|
195
|
+
onErrorCallback.current?.(paramsWithContext);
|
|
196
|
+
}
|
|
197
|
+
onFinishedCallback.current?.(paramsWithContext);
|
|
198
|
+
if (!willRetry) optimisticResultsRef.current.delete(values.requestId);
|
|
199
|
+
};
|
|
200
|
+
const handleGetLoadingEvent = (req) => {
|
|
201
|
+
return ({ loading }) => {
|
|
202
|
+
if (getIsDataProcessing(req.cacheKey)) return;
|
|
203
|
+
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(scopeKey(req.queryKey, req.scope));
|
|
204
|
+
if (loading || canDisableLoading) actions.setLoading(loading);
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
const handleDownloadProgress = (data) => {
|
|
208
|
+
onDownloadProgressCallback.current?.(data);
|
|
209
|
+
};
|
|
210
|
+
const handleUploadProgress = (data) => {
|
|
211
|
+
onUploadProgressCallback.current?.(data);
|
|
212
|
+
};
|
|
213
|
+
const handleRequestStart = () => {
|
|
214
|
+
return (details) => {
|
|
215
|
+
onRequestStartCallback.current?.(details);
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
const handleResponseStart = () => {
|
|
219
|
+
return (details) => {
|
|
220
|
+
onResponseStartCallback.current?.(details);
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
const handleResponse = () => {
|
|
224
|
+
return (values) => {
|
|
225
|
+
const data = unstable_responseMapper ? unstable_responseMapper(values.response) : values.response;
|
|
226
|
+
if (data instanceof Promise) return (async () => {
|
|
227
|
+
handleResponseCallbacks({
|
|
228
|
+
...values,
|
|
229
|
+
response: await data
|
|
230
|
+
});
|
|
231
|
+
})();
|
|
232
|
+
return handleResponseCallbacks(values);
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
const handleRemove = ({ requestId }) => {
|
|
236
|
+
const opt = optimisticResultsRef.current.get(requestId);
|
|
237
|
+
if (opt) {
|
|
238
|
+
try {
|
|
239
|
+
opt.rollback?.();
|
|
240
|
+
} catch {}
|
|
241
|
+
optimisticResultsRef.current.delete(requestId);
|
|
242
|
+
}
|
|
243
|
+
removeLifecycleListener(requestId);
|
|
244
|
+
};
|
|
245
|
+
const clearCacheDataListener = () => {
|
|
246
|
+
dataEvents.current?.unmount();
|
|
247
|
+
dataEvents.current = null;
|
|
248
|
+
};
|
|
249
|
+
const addCacheDataListener = (req) => {
|
|
250
|
+
const loadingUnmount = requestManager.events.onLoadingByQueue(scopeKey(req.queryKey, req.scope), handleGetLoadingEvent(req));
|
|
251
|
+
const getResponseUnmount = cache.events.onDataByKey(req.cacheKey, setCacheData);
|
|
252
|
+
const unmount = () => {
|
|
253
|
+
loadingUnmount();
|
|
254
|
+
getResponseUnmount();
|
|
255
|
+
};
|
|
256
|
+
clearCacheDataListener();
|
|
257
|
+
dataEvents.current = { unmount };
|
|
258
|
+
return unmount;
|
|
259
|
+
};
|
|
260
|
+
const addLifecycleListeners = (req, requestId, optimisticResult) => {
|
|
261
|
+
/**
|
|
262
|
+
* useFetch handles requesting by general keys
|
|
263
|
+
* This makes it possible to deduplicate requests from different places and share data
|
|
264
|
+
*/
|
|
265
|
+
if (!requestId) {
|
|
266
|
+
clearLifecycleListeners();
|
|
267
|
+
const { queryKey, cacheKey } = req;
|
|
268
|
+
const requestStartUnmount = requestManager.events.onRequestStartByQueue(queryKey, handleRequestStart());
|
|
269
|
+
const responseStartUnmount = requestManager.events.onResponseStartByQueue(queryKey, handleResponseStart());
|
|
270
|
+
const uploadUnmount = requestManager.events.onUploadProgressByQueue(queryKey, handleUploadProgress);
|
|
271
|
+
const downloadUnmount = requestManager.events.onDownloadProgressByQueue(queryKey, handleDownloadProgress);
|
|
272
|
+
const responseUnmount = requestManager.events.onResponseByCache(cacheKey, handleResponse());
|
|
273
|
+
const unmount = () => {
|
|
274
|
+
downloadUnmount();
|
|
275
|
+
uploadUnmount();
|
|
276
|
+
requestStartUnmount();
|
|
277
|
+
responseStartUnmount();
|
|
278
|
+
responseUnmount();
|
|
279
|
+
};
|
|
280
|
+
lifecycleEvents.current.set(queryKey, { unmount });
|
|
281
|
+
return unmount;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* useSubmit handles requesting by requestIds, this makes it possible to track single requests
|
|
285
|
+
*/
|
|
286
|
+
if (optimisticResult) optimisticResultsRef.current.set(requestId, optimisticResult);
|
|
287
|
+
const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
|
|
288
|
+
const requestStartUnmount = requestManager.events.onRequestStartById(requestId, handleRequestStart());
|
|
289
|
+
const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart());
|
|
290
|
+
const responseUnmount = requestManager.events.onResponseById(requestId, handleResponse());
|
|
291
|
+
const uploadUnmount = requestManager.events.onUploadProgressById(requestId, handleUploadProgress);
|
|
292
|
+
const downloadUnmount = requestManager.events.onDownloadProgressById(requestId, handleDownloadProgress);
|
|
293
|
+
const unmount = () => {
|
|
294
|
+
requestRemove();
|
|
295
|
+
downloadUnmount();
|
|
296
|
+
uploadUnmount();
|
|
297
|
+
requestStartUnmount();
|
|
298
|
+
responseStartUnmount();
|
|
299
|
+
responseUnmount();
|
|
300
|
+
};
|
|
301
|
+
lifecycleEvents.current.set(requestId, { unmount });
|
|
302
|
+
return unmount;
|
|
303
|
+
};
|
|
304
|
+
const abort = () => {
|
|
305
|
+
const ak = scopeKey(request.abortKey, request.scope);
|
|
306
|
+
dispatcher.getAllRunningRequests().forEach((requestData) => {
|
|
307
|
+
const reqAk = scopeKey(requestData.request.abortKey, requestData.request.scope);
|
|
308
|
+
if (reqAk === ak) {
|
|
309
|
+
const qk = scopeKey(requestData.request.queryKey, requestData.request.scope);
|
|
310
|
+
dispatcher.delete(qk, requestData.requestId, reqAk);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* On unmount we want to clear all the listeners to prevent memory leaks
|
|
316
|
+
*/
|
|
317
|
+
d$1(() => {
|
|
318
|
+
clearLifecycleListeners();
|
|
319
|
+
clearCacheDataListener();
|
|
320
|
+
});
|
|
321
|
+
return [{
|
|
322
|
+
abort,
|
|
323
|
+
onSuccess: (callback) => {
|
|
324
|
+
onSuccessCallback.current = callback;
|
|
325
|
+
},
|
|
326
|
+
onError: (callback) => {
|
|
327
|
+
onErrorCallback.current = callback;
|
|
328
|
+
},
|
|
329
|
+
onAbort: (callback) => {
|
|
330
|
+
onAbortCallback.current = callback;
|
|
331
|
+
},
|
|
332
|
+
onOfflineError: (callback) => {
|
|
333
|
+
onOfflineErrorCallback.current = callback;
|
|
334
|
+
},
|
|
335
|
+
onFinished: (callback) => {
|
|
336
|
+
onFinishedCallback.current = callback;
|
|
337
|
+
},
|
|
338
|
+
onRequestStart: (callback) => {
|
|
339
|
+
onRequestStartCallback.current = callback;
|
|
340
|
+
},
|
|
341
|
+
onResponseStart: (callback) => {
|
|
342
|
+
onResponseStartCallback.current = callback;
|
|
343
|
+
},
|
|
344
|
+
onDownloadProgress: (callback) => {
|
|
345
|
+
onDownloadProgressCallback.current = callback;
|
|
346
|
+
},
|
|
347
|
+
onUploadProgress: (callback) => {
|
|
348
|
+
onUploadProgressCallback.current = callback;
|
|
349
|
+
}
|
|
350
|
+
}, {
|
|
351
|
+
addCacheDataListener,
|
|
352
|
+
clearCacheDataListener,
|
|
353
|
+
addLifecycleListeners,
|
|
354
|
+
removeLifecycleListener,
|
|
355
|
+
clearLifecycleListeners
|
|
356
|
+
}];
|
|
357
|
+
};
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/helpers/use-tracked-state/use-tracked-state.constants.ts
|
|
360
|
+
var initialState = {
|
|
361
|
+
data: null,
|
|
362
|
+
error: null,
|
|
363
|
+
status: null,
|
|
364
|
+
extra: {},
|
|
365
|
+
success: false,
|
|
366
|
+
loading: false,
|
|
367
|
+
retries: 0,
|
|
368
|
+
responseTimestamp: null,
|
|
369
|
+
requestTimestamp: null
|
|
370
|
+
};
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/utils/deep-equal.utils.ts
|
|
373
|
+
/**
|
|
374
|
+
* Check if value is empty
|
|
375
|
+
* @param value any object or primitive
|
|
376
|
+
* @returns true when value is empty
|
|
377
|
+
*/
|
|
378
|
+
var isEmpty = (value) => {
|
|
379
|
+
const valueType = Object.prototype.toString.call(value);
|
|
380
|
+
if (Array.isArray(value)) return !value.length;
|
|
381
|
+
if (typeof value === "object" && value !== null && valueType === "[object Object]") return !Object.keys(value).length;
|
|
382
|
+
return false;
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* Allow to deep compare any passed values
|
|
386
|
+
* @param firstValue unknown
|
|
387
|
+
* @param secondValue unknown
|
|
388
|
+
* @returns true when elements are equal
|
|
389
|
+
*/
|
|
390
|
+
var isEqual = (firstValue, secondValue) => {
|
|
391
|
+
if (firstValue === secondValue) return true;
|
|
392
|
+
try {
|
|
393
|
+
const firstValueType = Object.prototype.toString.call(firstValue);
|
|
394
|
+
const secondValueType = Object.prototype.toString.call(secondValue);
|
|
395
|
+
const firstType = typeof firstValue;
|
|
396
|
+
const secondType = typeof secondValue;
|
|
397
|
+
const isType = (type) => firstType === type && secondType === type;
|
|
398
|
+
const isTypeValue = (type) => firstValueType === type && secondValueType === type;
|
|
399
|
+
if (firstValueType !== secondValueType) return false;
|
|
400
|
+
if (isType("number") && Number.isNaN(firstValue) && Number.isNaN(secondValue)) return true;
|
|
401
|
+
if (isEmpty(firstValue) && isEmpty(secondValue)) return true;
|
|
402
|
+
if (Array.isArray(firstValue) && Array.isArray(secondValue)) {
|
|
403
|
+
if (firstValue.length !== secondValue.length) return false;
|
|
404
|
+
return !firstValue.some((element, i) => !isEqual(element, secondValue[i]));
|
|
405
|
+
}
|
|
406
|
+
if (isType("object") && isTypeValue("[object Object]")) {
|
|
407
|
+
if (Object.keys(firstValue).length !== Object.keys(secondValue).length) return false;
|
|
408
|
+
return !Object.entries(firstValue).some(([key, value]) => !isEqual(value, secondValue[key]));
|
|
409
|
+
}
|
|
410
|
+
if (firstValue instanceof Date && secondValue instanceof Date) return +firstValue === +secondValue;
|
|
411
|
+
return firstValue === secondValue;
|
|
412
|
+
} catch (err) {
|
|
413
|
+
console.error(err);
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/utils/bounce.utils.ts
|
|
419
|
+
var getBounceData = (bounceData) => {
|
|
420
|
+
return {
|
|
421
|
+
...bounceData,
|
|
422
|
+
throttle: void 0,
|
|
423
|
+
debounce: void 0
|
|
424
|
+
};
|
|
425
|
+
};
|
|
426
|
+
//#endregion
|
|
427
|
+
//#region src/utils/tracked-proxy.utils.ts
|
|
428
|
+
/**
|
|
429
|
+
* Wraps a plain object in a Proxy that calls `setRenderKey` when tracked properties are accessed.
|
|
430
|
+
* Unlike getter-based tracking, Proxy-wrapped objects display their actual values in console.log,
|
|
431
|
+
* making debugging significantly easier while preserving field-level dependency tracking.
|
|
432
|
+
*/
|
|
433
|
+
var createTrackedProxy = (target, trackedKeys, setRenderKey) => {
|
|
434
|
+
return new Proxy(target, { get(obj, prop, receiver) {
|
|
435
|
+
if (typeof prop === "string" && trackedKeys.includes(prop)) setRenderKey(prop);
|
|
436
|
+
return Reflect.get(obj, prop, receiver);
|
|
437
|
+
} });
|
|
438
|
+
};
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/helpers/use-tracked-state/use-tracked-state.utils.ts
|
|
441
|
+
/**
|
|
442
|
+
* Extracts the "identity" portion of a cache key (method + endpoint with resolved params),
|
|
443
|
+
* stripping the query params part. Cache keys follow the format: `method_endpoint_queryParams`.
|
|
444
|
+
*/
|
|
445
|
+
var getCacheKeyIdentity = (cacheKey) => {
|
|
446
|
+
const lastUnderscoreIndex = cacheKey.lastIndexOf("_");
|
|
447
|
+
if (lastUnderscoreIndex === -1) return cacheKey;
|
|
448
|
+
return cacheKey.substring(0, lastUnderscoreIndex);
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* Determines whether state should be cleared when the cache key changes.
|
|
452
|
+
*
|
|
453
|
+
* - `"clean"` — always clear
|
|
454
|
+
* - `"preserve"` — never clear
|
|
455
|
+
* - `"auto"` — clear when the resource identity changed (URL params), preserve when only query params changed
|
|
456
|
+
*/
|
|
457
|
+
var getShouldClearState = (mode, oldCacheKey, newCacheKey) => {
|
|
458
|
+
if (oldCacheKey === newCacheKey) return false;
|
|
459
|
+
if (mode === "clean") return true;
|
|
460
|
+
if (mode === "preserve") return false;
|
|
461
|
+
return getCacheKeyIdentity(oldCacheKey) !== getCacheKeyIdentity(newCacheKey);
|
|
462
|
+
};
|
|
463
|
+
var getDetailsState = (state, details) => {
|
|
464
|
+
return {
|
|
465
|
+
retries: state?.retries || 0,
|
|
466
|
+
isCanceled: false,
|
|
467
|
+
isOffline: false,
|
|
468
|
+
willRetry: false,
|
|
469
|
+
addedTimestamp: +/* @__PURE__ */ new Date(),
|
|
470
|
+
triggerTimestamp: +/* @__PURE__ */ new Date(),
|
|
471
|
+
requestTimestamp: +/* @__PURE__ */ new Date(),
|
|
472
|
+
responseTimestamp: +/* @__PURE__ */ new Date(),
|
|
473
|
+
...details
|
|
474
|
+
};
|
|
475
|
+
};
|
|
476
|
+
var isStaleCacheData = (staleTime, staleTimestamp) => {
|
|
477
|
+
if (!staleTimestamp) return true;
|
|
478
|
+
return +/* @__PURE__ */ new Date() > +staleTimestamp + staleTime;
|
|
479
|
+
};
|
|
480
|
+
var getValidCacheData = (request, initialResponse, cacheData) => {
|
|
481
|
+
if (!isStaleCacheData(request.staleTime, cacheData?.responseTimestamp) && cacheData) return cacheData;
|
|
482
|
+
if (initialResponse) return {
|
|
483
|
+
data: null,
|
|
484
|
+
error: null,
|
|
485
|
+
status: null,
|
|
486
|
+
success: true,
|
|
487
|
+
extra: null,
|
|
488
|
+
cached: !!request.cache,
|
|
489
|
+
...initialResponse,
|
|
490
|
+
...getDetailsState(),
|
|
491
|
+
staleTime: 1e3,
|
|
492
|
+
version: request.client.cache.version,
|
|
493
|
+
cacheKey: request.cacheKey,
|
|
494
|
+
scope: request.scope,
|
|
495
|
+
cacheTime: request.cacheTime,
|
|
496
|
+
requestTimestamp: initialResponse?.requestTimestamp ?? +/* @__PURE__ */ new Date(),
|
|
497
|
+
responseTimestamp: initialResponse?.responseTimestamp ?? +/* @__PURE__ */ new Date()
|
|
498
|
+
};
|
|
499
|
+
return null;
|
|
500
|
+
};
|
|
501
|
+
var getTimestamp = (timestamp) => {
|
|
502
|
+
return timestamp ? new Date(timestamp) : null;
|
|
503
|
+
};
|
|
504
|
+
var getIsInitiallyLoading = ({ queryKey, dispatcher, hasState, revalidate, disabled }) => {
|
|
505
|
+
if (!revalidate && hasState) return false;
|
|
506
|
+
const queue = dispatcher.getQueue(queryKey);
|
|
507
|
+
return dispatcher.hasRunningRequests(queryKey) || !queue.stopped && disabled === false;
|
|
508
|
+
};
|
|
509
|
+
var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalidate }) => {
|
|
510
|
+
const { client, cacheKey, unstable_responseMapper } = request;
|
|
511
|
+
const { cache } = client;
|
|
512
|
+
const cacheState = getValidCacheData(request, initialResponse, cache.get(scopeKey(cacheKey, request.scope)));
|
|
513
|
+
const initialLoading = getIsInitiallyLoading({
|
|
514
|
+
queryKey: scopeKey(request.queryKey, request.scope),
|
|
515
|
+
dispatcher,
|
|
516
|
+
disabled,
|
|
517
|
+
revalidate,
|
|
518
|
+
hasState: !!cacheState
|
|
519
|
+
});
|
|
520
|
+
if (cacheState) {
|
|
521
|
+
const mappedData = unstable_responseMapper ? unstable_responseMapper(cacheState) : cacheState;
|
|
522
|
+
if (mappedData instanceof Promise) return initialState;
|
|
523
|
+
return {
|
|
524
|
+
data: mappedData.data,
|
|
525
|
+
error: mappedData.error,
|
|
526
|
+
status: mappedData.status,
|
|
527
|
+
success: mappedData.success,
|
|
528
|
+
extra: mappedData.extra || client.adapter.defaultExtra,
|
|
529
|
+
retries: cacheState.retries,
|
|
530
|
+
requestTimestamp: getTimestamp(cacheState.requestTimestamp),
|
|
531
|
+
responseTimestamp: getTimestamp(cacheState.responseTimestamp),
|
|
532
|
+
loading: initialLoading
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return {
|
|
536
|
+
data: initialState.data,
|
|
537
|
+
error: initialState.error,
|
|
538
|
+
status: initialState.status,
|
|
539
|
+
success: initialState.success,
|
|
540
|
+
extra: request.client.adapter.defaultExtra,
|
|
541
|
+
retries: initialState.retries,
|
|
542
|
+
requestTimestamp: getTimestamp(initialState.requestTimestamp),
|
|
543
|
+
responseTimestamp: getTimestamp(initialState.responseTimestamp),
|
|
544
|
+
loading: initialLoading
|
|
545
|
+
};
|
|
546
|
+
};
|
|
547
|
+
//#endregion
|
|
548
|
+
//#region src/helpers/use-tracked-state/use-tracked-state.hooks.ts
|
|
549
|
+
/**
|
|
550
|
+
*
|
|
551
|
+
* @param request
|
|
552
|
+
* @param initialResponse
|
|
553
|
+
* @param dispatcher
|
|
554
|
+
* @param dependencies
|
|
555
|
+
* @internal
|
|
556
|
+
*/
|
|
557
|
+
var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, dependencyTracking, keepPreviousData = "auto", disabled, revalidate }) => {
|
|
558
|
+
const { client, cacheKey, queryKey, staleTime, unstable_responseMapper } = request;
|
|
559
|
+
const { cache } = client;
|
|
560
|
+
const state = useRef(getInitialState({
|
|
561
|
+
initialResponse,
|
|
562
|
+
dispatcher,
|
|
563
|
+
request,
|
|
564
|
+
disabled
|
|
565
|
+
}));
|
|
566
|
+
const renderKeys = useRef([]);
|
|
567
|
+
const isProcessingData = useRef("");
|
|
568
|
+
const previousCacheKey = useRef(cacheKey);
|
|
569
|
+
const versionRef = useRef(0);
|
|
570
|
+
const listenerRef = useRef(null);
|
|
571
|
+
const subscribe = useCallback((listener) => {
|
|
572
|
+
listenerRef.current = listener;
|
|
573
|
+
return () => {
|
|
574
|
+
listenerRef.current = null;
|
|
575
|
+
};
|
|
576
|
+
}, []);
|
|
577
|
+
const getSnapshot = useCallback(() => versionRef.current, []);
|
|
578
|
+
useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
579
|
+
const emitChange = () => {
|
|
580
|
+
versionRef.current += 1;
|
|
581
|
+
listenerRef.current?.();
|
|
582
|
+
};
|
|
583
|
+
const getStaleStatus = () => {
|
|
584
|
+
const cacheData = cache.get(scopeKey(cacheKey, request.scope));
|
|
585
|
+
return !cacheData || isStaleCacheData(staleTime, cacheData?.responseTimestamp);
|
|
586
|
+
};
|
|
587
|
+
const renderKeyTrigger = (keys) => {
|
|
588
|
+
if (renderKeys.current.some((renderKey) => keys.includes(renderKey))) emitChange();
|
|
589
|
+
};
|
|
590
|
+
const setRenderKey = (renderKey) => {
|
|
591
|
+
if (!renderKeys.current.includes(renderKey)) renderKeys.current.push(renderKey);
|
|
592
|
+
};
|
|
593
|
+
i(() => {
|
|
594
|
+
const oldKey = previousCacheKey.current;
|
|
595
|
+
previousCacheKey.current = cacheKey;
|
|
596
|
+
const cacheState = getValidCacheData(request, initialResponse, cache.get(cacheKey));
|
|
597
|
+
if (getShouldClearState(keepPreviousData, oldKey, cacheKey) && !cacheState) {
|
|
598
|
+
state.current = getInitialState({
|
|
599
|
+
initialResponse,
|
|
600
|
+
dispatcher,
|
|
601
|
+
request,
|
|
602
|
+
disabled,
|
|
603
|
+
revalidate
|
|
604
|
+
});
|
|
605
|
+
renderKeyTrigger(Object.keys(state.current));
|
|
606
|
+
} else if (cacheState) {
|
|
607
|
+
state.current.loading = getIsInitiallyLoading({
|
|
608
|
+
queryKey: scopeKey(request.queryKey, request.scope),
|
|
609
|
+
dispatcher,
|
|
610
|
+
disabled,
|
|
611
|
+
revalidate,
|
|
612
|
+
hasState: true
|
|
613
|
+
});
|
|
614
|
+
setCacheData(cacheState);
|
|
615
|
+
}
|
|
616
|
+
}, [cacheKey, queryKey], true);
|
|
617
|
+
i(() => {
|
|
618
|
+
const handleDependencyTracking = () => {
|
|
619
|
+
if (!dependencyTracking) Object.keys(state.current).forEach((key) => setRenderKey(key));
|
|
620
|
+
};
|
|
621
|
+
handleDependencyTracking();
|
|
622
|
+
}, [dependencyTracking], true);
|
|
623
|
+
const handleCompare = (firstValue, secondValue) => {
|
|
624
|
+
if (typeof deepCompare === "function") return deepCompare(firstValue, secondValue);
|
|
625
|
+
if (deepCompare) return isEqual(firstValue, secondValue);
|
|
626
|
+
return false;
|
|
627
|
+
};
|
|
628
|
+
const handleCacheData = (cacheData) => {
|
|
629
|
+
const newStateValues = {
|
|
630
|
+
data: cacheData.data,
|
|
631
|
+
error: cacheData.error,
|
|
632
|
+
status: cacheData.status,
|
|
633
|
+
success: cacheData.success,
|
|
634
|
+
extra: cacheData.extra,
|
|
635
|
+
retries: cacheData.retries,
|
|
636
|
+
responseTimestamp: new Date(cacheData.responseTimestamp),
|
|
637
|
+
requestTimestamp: new Date(cacheData.requestTimestamp),
|
|
638
|
+
loading: dispatcher.hasRunningRequests(queryKey)
|
|
639
|
+
};
|
|
640
|
+
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
641
|
+
const keyValue = key;
|
|
642
|
+
const firstValue = state.current[keyValue];
|
|
643
|
+
const secondValue = newStateValues[keyValue];
|
|
644
|
+
return !handleCompare(firstValue, secondValue);
|
|
645
|
+
});
|
|
646
|
+
state.current = {
|
|
647
|
+
...state.current,
|
|
648
|
+
...newStateValues
|
|
649
|
+
};
|
|
650
|
+
renderKeyTrigger(changedKeys);
|
|
651
|
+
};
|
|
652
|
+
const setIsDataProcessing = ({ processingCacheKey, isProcessing }) => {
|
|
653
|
+
if (isProcessing) isProcessingData.current = processingCacheKey;
|
|
654
|
+
else if (isProcessingData.current === cacheKey) isProcessingData.current = "";
|
|
655
|
+
};
|
|
656
|
+
const getIsDataProcessing = (processingCacheKey) => {
|
|
657
|
+
return isProcessingData.current === processingCacheKey;
|
|
658
|
+
};
|
|
659
|
+
const setCacheData = (cacheData) => {
|
|
660
|
+
setIsDataProcessing({
|
|
661
|
+
processingCacheKey: cacheKey,
|
|
662
|
+
isProcessing: true
|
|
663
|
+
});
|
|
664
|
+
const data = unstable_responseMapper ? unstable_responseMapper(cacheData) : cacheData;
|
|
665
|
+
if (data instanceof Promise) return (async () => {
|
|
666
|
+
const promiseData = await data;
|
|
667
|
+
handleCacheData({
|
|
668
|
+
...cacheData,
|
|
669
|
+
...promiseData
|
|
670
|
+
});
|
|
671
|
+
setIsDataProcessing({
|
|
672
|
+
processingCacheKey: cacheKey,
|
|
673
|
+
isProcessing: false
|
|
674
|
+
});
|
|
675
|
+
})();
|
|
676
|
+
setIsDataProcessing({
|
|
677
|
+
processingCacheKey: cacheKey,
|
|
678
|
+
isProcessing: false
|
|
679
|
+
});
|
|
680
|
+
return handleCacheData({
|
|
681
|
+
...cacheData,
|
|
682
|
+
...data
|
|
683
|
+
});
|
|
684
|
+
};
|
|
685
|
+
return [
|
|
686
|
+
state.current,
|
|
687
|
+
{
|
|
688
|
+
setData: (data) => {
|
|
689
|
+
state.current.data = data instanceof Function ? data(state.current.data || null) : data;
|
|
690
|
+
renderKeyTrigger(["data"]);
|
|
691
|
+
},
|
|
692
|
+
setError: (error) => {
|
|
693
|
+
state.current.error = error instanceof Function ? error(state.current.error || null) : error;
|
|
694
|
+
renderKeyTrigger(["error"]);
|
|
695
|
+
},
|
|
696
|
+
setLoading: (loading) => {
|
|
697
|
+
const value = loading instanceof Function ? loading(state.current.loading) : loading;
|
|
698
|
+
if (value === state.current.loading) return;
|
|
699
|
+
state.current.loading = value;
|
|
700
|
+
renderKeyTrigger(["loading"]);
|
|
701
|
+
},
|
|
702
|
+
setStatus: (status) => {
|
|
703
|
+
if ((status instanceof Function ? status(state.current.status) : status) === state.current.status) return;
|
|
704
|
+
state.current.status = status instanceof Function ? status(state.current.status || null) : status;
|
|
705
|
+
renderKeyTrigger(["status"]);
|
|
706
|
+
},
|
|
707
|
+
setSuccess: (success) => {
|
|
708
|
+
if ((success instanceof Function ? success(state.current.success || false) : success) === state.current.success) return;
|
|
709
|
+
state.current.success = success instanceof Function ? success(state.current.success || false) : success;
|
|
710
|
+
renderKeyTrigger(["success"]);
|
|
711
|
+
},
|
|
712
|
+
setExtra: (extra) => {
|
|
713
|
+
if ((extra instanceof Function ? extra(state.current.extra) : extra) === state.current.extra) return;
|
|
714
|
+
state.current.extra = extra instanceof Function ? extra(state.current.extra) : extra;
|
|
715
|
+
renderKeyTrigger(["extra"]);
|
|
716
|
+
},
|
|
717
|
+
setRetries: (retries) => {
|
|
718
|
+
if ((retries instanceof Function ? retries(state.current.retries || 0) : retries) === state.current.retries) return;
|
|
719
|
+
state.current.retries = retries instanceof Function ? retries(state.current.retries || 0) : retries;
|
|
720
|
+
renderKeyTrigger(["retries"]);
|
|
721
|
+
},
|
|
722
|
+
setResponseTimestamp: (timestamp) => {
|
|
723
|
+
if ((timestamp instanceof Function ? timestamp(state.current.responseTimestamp) : timestamp) === state.current.responseTimestamp) return;
|
|
724
|
+
const getTimestamp = (prev) => {
|
|
725
|
+
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
726
|
+
};
|
|
727
|
+
state.current.responseTimestamp = getTimestamp(state.current.responseTimestamp);
|
|
728
|
+
renderKeyTrigger(["responseTimestamp"]);
|
|
729
|
+
},
|
|
730
|
+
setRequestTimestamp: (timestamp) => {
|
|
731
|
+
if ((timestamp instanceof Function ? timestamp(state.current.requestTimestamp) : timestamp) === state.current.requestTimestamp) return;
|
|
732
|
+
const getTimestamp = (prev) => {
|
|
733
|
+
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
734
|
+
};
|
|
735
|
+
state.current.requestTimestamp = getTimestamp(state.current.requestTimestamp);
|
|
736
|
+
renderKeyTrigger(["requestTimestamp"]);
|
|
737
|
+
},
|
|
738
|
+
clearState: () => {
|
|
739
|
+
state.current = {
|
|
740
|
+
data: null,
|
|
741
|
+
error: null,
|
|
742
|
+
loading: false,
|
|
743
|
+
status: null,
|
|
744
|
+
success: false,
|
|
745
|
+
extra: null,
|
|
746
|
+
retries: 0,
|
|
747
|
+
responseTimestamp: null,
|
|
748
|
+
requestTimestamp: null
|
|
749
|
+
};
|
|
750
|
+
renderKeyTrigger(Object.keys(state.current));
|
|
751
|
+
}
|
|
752
|
+
},
|
|
753
|
+
{
|
|
754
|
+
setRenderKey,
|
|
755
|
+
setCacheData,
|
|
756
|
+
getStaleStatus,
|
|
757
|
+
getIsDataProcessing
|
|
758
|
+
}
|
|
759
|
+
];
|
|
760
|
+
};
|
|
761
|
+
//#endregion
|
|
762
|
+
//#region src/helpers/use-socket-state/use-socket-state.hooks.ts
|
|
763
|
+
var useSocketState = (socket, { dependencyTracking }) => {
|
|
764
|
+
const onDisconnectCallback = useRef(null);
|
|
765
|
+
const onErrorCallback = useRef(null);
|
|
766
|
+
const onConnectedCallback = useRef(null);
|
|
767
|
+
const onConnectingCallback = useRef(null);
|
|
768
|
+
const onReconnectingCallback = useRef(null);
|
|
769
|
+
const onReconnectingFailedCallback = useRef(null);
|
|
770
|
+
const state = useRef({
|
|
771
|
+
data: null,
|
|
772
|
+
extra: null,
|
|
773
|
+
connected: socket.adapter.connected,
|
|
774
|
+
connecting: socket.adapter.connecting,
|
|
775
|
+
timestamp: null
|
|
776
|
+
});
|
|
777
|
+
const renderKeys = useRef([]);
|
|
778
|
+
const versionRef = useRef(0);
|
|
779
|
+
const listenerRef = useRef(null);
|
|
780
|
+
const subscribe = useCallback((listener) => {
|
|
781
|
+
listenerRef.current = listener;
|
|
782
|
+
return () => {
|
|
783
|
+
listenerRef.current = null;
|
|
784
|
+
};
|
|
785
|
+
}, []);
|
|
786
|
+
const getSnapshot = useCallback(() => versionRef.current, []);
|
|
787
|
+
useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
788
|
+
const emitChange = () => {
|
|
789
|
+
versionRef.current += 1;
|
|
790
|
+
listenerRef.current?.();
|
|
791
|
+
};
|
|
792
|
+
const renderKeyTrigger = (keys) => {
|
|
793
|
+
if (renderKeys.current.some((renderKey) => keys.includes(renderKey))) emitChange();
|
|
794
|
+
};
|
|
795
|
+
const setRenderKey = (renderKey) => {
|
|
796
|
+
if (!renderKeys.current.includes(renderKey)) renderKeys.current.push(renderKey);
|
|
797
|
+
};
|
|
798
|
+
i(() => {
|
|
799
|
+
const handleDependencyTracking = () => {
|
|
800
|
+
if (!dependencyTracking) Object.keys(state.current).forEach((key) => setRenderKey(key));
|
|
801
|
+
};
|
|
802
|
+
handleDependencyTracking();
|
|
803
|
+
}, [dependencyTracking], true);
|
|
804
|
+
const actions = {
|
|
805
|
+
setData: (data) => {
|
|
806
|
+
state.current.data = data;
|
|
807
|
+
renderKeyTrigger(["data"]);
|
|
808
|
+
},
|
|
809
|
+
setExtra: (extra) => {
|
|
810
|
+
state.current.extra = extra;
|
|
811
|
+
renderKeyTrigger(["extra"]);
|
|
812
|
+
},
|
|
813
|
+
setConnected: (connected) => {
|
|
814
|
+
state.current.connected = connected;
|
|
815
|
+
renderKeyTrigger(["connected"]);
|
|
816
|
+
},
|
|
817
|
+
setConnecting: (connecting) => {
|
|
818
|
+
state.current.connecting = connecting;
|
|
819
|
+
renderKeyTrigger(["connecting"]);
|
|
820
|
+
},
|
|
821
|
+
setTimestamp: (timestamp) => {
|
|
822
|
+
state.current.timestamp = timestamp;
|
|
823
|
+
renderKeyTrigger(["timestamp"]);
|
|
824
|
+
},
|
|
825
|
+
clearState: () => {
|
|
826
|
+
state.current = {
|
|
827
|
+
data: null,
|
|
828
|
+
extra: null,
|
|
829
|
+
connected: false,
|
|
830
|
+
connecting: false,
|
|
831
|
+
timestamp: null
|
|
832
|
+
};
|
|
833
|
+
renderKeyTrigger(Object.keys(state.current));
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
const callbacks = {
|
|
837
|
+
onConnected: (callback) => {
|
|
838
|
+
onConnectedCallback.current = callback;
|
|
839
|
+
},
|
|
840
|
+
onDisconnected: (callback) => {
|
|
841
|
+
onDisconnectCallback.current = callback;
|
|
842
|
+
},
|
|
843
|
+
onError: (callback) => {
|
|
844
|
+
onErrorCallback.current = callback;
|
|
845
|
+
},
|
|
846
|
+
onConnecting: (callback) => {
|
|
847
|
+
onConnectingCallback.current = callback;
|
|
848
|
+
},
|
|
849
|
+
onReconnecting: (callback) => {
|
|
850
|
+
onReconnectingCallback.current = callback;
|
|
851
|
+
},
|
|
852
|
+
onReconnectingFailed: (callback) => {
|
|
853
|
+
onReconnectingFailedCallback.current = callback;
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
o(() => {
|
|
857
|
+
const umountOnError = socket.events.onError((event) => {
|
|
858
|
+
onErrorCallback.current?.(event);
|
|
859
|
+
});
|
|
860
|
+
const umountOnConnecting = socket.events.onConnecting(({ connecting }) => {
|
|
861
|
+
actions.setConnecting(connecting);
|
|
862
|
+
onConnectingCallback.current?.();
|
|
863
|
+
});
|
|
864
|
+
const umountOnOpen = socket.events.onConnected(() => {
|
|
865
|
+
actions.setConnected(true);
|
|
866
|
+
onConnectedCallback.current?.();
|
|
867
|
+
});
|
|
868
|
+
const umountOnClose = socket.events.onDisconnected(() => {
|
|
869
|
+
actions.setConnected(false);
|
|
870
|
+
onDisconnectCallback.current?.();
|
|
871
|
+
});
|
|
872
|
+
const umountOnReconnecting = socket.events.onReconnecting(({ attempts }) => {
|
|
873
|
+
onReconnectingCallback.current?.({ attempts });
|
|
874
|
+
});
|
|
875
|
+
const umountOnReconnectingFailed = socket.events.onReconnectingFailed(({ attempts }) => {
|
|
876
|
+
onReconnectingFailedCallback.current?.({ attempts });
|
|
877
|
+
});
|
|
878
|
+
return () => {
|
|
879
|
+
umountOnError();
|
|
880
|
+
umountOnConnecting();
|
|
881
|
+
umountOnOpen();
|
|
882
|
+
umountOnClose();
|
|
883
|
+
umountOnReconnecting();
|
|
884
|
+
umountOnReconnectingFailed();
|
|
885
|
+
};
|
|
886
|
+
});
|
|
887
|
+
return [
|
|
888
|
+
state.current,
|
|
889
|
+
actions,
|
|
890
|
+
callbacks,
|
|
891
|
+
{ setRenderKey }
|
|
892
|
+
];
|
|
893
|
+
};
|
|
894
|
+
//#endregion
|
|
895
|
+
//#region src/helpers/use-socket-state/use-socket-state.constants.ts
|
|
896
|
+
var initialSocketState = {
|
|
897
|
+
data: null,
|
|
898
|
+
extra: null,
|
|
899
|
+
connected: false,
|
|
900
|
+
connecting: false,
|
|
901
|
+
timestamp: null
|
|
902
|
+
};
|
|
903
|
+
//#endregion
|
|
904
|
+
//#region src/provider/provider.tsx
|
|
905
|
+
var ConfigContext = React.createContext({
|
|
906
|
+
config: {},
|
|
907
|
+
setConfig: () => null
|
|
908
|
+
});
|
|
909
|
+
/**
|
|
910
|
+
* Provider with configuration for hooks
|
|
911
|
+
* @param options
|
|
912
|
+
* @returns
|
|
913
|
+
*/
|
|
914
|
+
var Provider = ({ children, config }) => {
|
|
915
|
+
const [currentConfig, setConfig] = useState(config || {});
|
|
916
|
+
const value = useMemo(() => {
|
|
917
|
+
return {
|
|
918
|
+
config: currentConfig,
|
|
919
|
+
setConfig
|
|
920
|
+
};
|
|
921
|
+
}, [currentConfig]);
|
|
922
|
+
return /* @__PURE__ */ jsx(ConfigContext.Provider, {
|
|
923
|
+
value,
|
|
924
|
+
children
|
|
925
|
+
});
|
|
926
|
+
};
|
|
927
|
+
/**
|
|
928
|
+
* Hook to allow reading current context config
|
|
929
|
+
* @returns
|
|
930
|
+
*/
|
|
931
|
+
var useProvider = () => {
|
|
932
|
+
return useContext(ConfigContext);
|
|
933
|
+
};
|
|
934
|
+
//#endregion
|
|
935
|
+
//#region src/hooks/use-fetch/use-fetch.hooks.ts
|
|
936
|
+
var suspensePromiseMap = /* @__PURE__ */ new Map();
|
|
937
|
+
var suspenseResultMap = /* @__PURE__ */ new Map();
|
|
938
|
+
/**
|
|
939
|
+
* This hooks aims to retrieve data from server.
|
|
940
|
+
* @param request Request instance
|
|
941
|
+
* @param options Hook options
|
|
942
|
+
* @returns
|
|
943
|
+
*/
|
|
944
|
+
var useFetch = (request, options) => {
|
|
945
|
+
const { config: globalConfig } = useProvider();
|
|
946
|
+
const { suspense, dependencies, disabled, dependencyTracking, revalidate, initialResponse, keepPreviousData, refresh, refreshTime, refetchBlurred, refetchOnBlur, refetchOnFocus, refetchOnReconnect, bounce, bounceType, bounceTime, bounceTimeout, deepCompare } = {
|
|
947
|
+
...useFetchDefaultOptions,
|
|
948
|
+
...globalConfig.useFetchConfig,
|
|
949
|
+
...options
|
|
950
|
+
};
|
|
951
|
+
const updateKey = JSON.stringify(request.toJSON());
|
|
952
|
+
const requestDebounce = f({ delay: bounceTime });
|
|
953
|
+
const requestThrottle = p({
|
|
954
|
+
interval: bounceTime,
|
|
955
|
+
timeout: bounceTimeout
|
|
956
|
+
});
|
|
957
|
+
const refreshDebounce = f({ delay: refreshTime });
|
|
958
|
+
const { cacheKey, queryKey, client } = request;
|
|
959
|
+
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
960
|
+
const ignoreReact18DoubleRender = useRef(true);
|
|
961
|
+
const logger = useRef(loggerManager.initialize(client, "useFetch")).current;
|
|
962
|
+
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
963
|
+
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
964
|
+
/**
|
|
965
|
+
* State handler with optimization for re-rendering, that hooks into the cache state and dispatchers queues
|
|
966
|
+
*/
|
|
967
|
+
const [state, actions, { setRenderKey, setCacheData, getStaleStatus, getIsDataProcessing }] = useTrackedState({
|
|
968
|
+
logger,
|
|
969
|
+
request,
|
|
970
|
+
dispatcher,
|
|
971
|
+
initialResponse,
|
|
972
|
+
deepCompare,
|
|
973
|
+
dependencyTracking,
|
|
974
|
+
keepPreviousData,
|
|
975
|
+
disabled,
|
|
976
|
+
revalidate
|
|
977
|
+
});
|
|
978
|
+
/**
|
|
979
|
+
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
980
|
+
*/
|
|
981
|
+
const [callbacks, listeners] = useRequestEvents({
|
|
982
|
+
logger,
|
|
983
|
+
actions,
|
|
984
|
+
request,
|
|
985
|
+
dispatcher,
|
|
986
|
+
setCacheData,
|
|
987
|
+
getIsDataProcessing
|
|
988
|
+
});
|
|
989
|
+
const { addCacheDataListener, addLifecycleListeners, clearCacheDataListener } = listeners;
|
|
990
|
+
const handleFetch = () => {
|
|
991
|
+
if (!disabled) {
|
|
992
|
+
logger.debug({
|
|
993
|
+
title: `Fetching data`,
|
|
994
|
+
type: "system",
|
|
995
|
+
extra: { request }
|
|
996
|
+
});
|
|
997
|
+
dispatcher.add(request);
|
|
998
|
+
} else logger.debug({
|
|
999
|
+
title: `Cannot add to fetch queue`,
|
|
1000
|
+
type: "system",
|
|
1001
|
+
extra: { disabled }
|
|
1002
|
+
});
|
|
1003
|
+
};
|
|
1004
|
+
function handleRefresh() {
|
|
1005
|
+
if (!refresh || disabled) {
|
|
1006
|
+
refreshDebounce.reset();
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
refreshDebounce.debounce(() => {
|
|
1010
|
+
const isBlurred = !appManager.isFocused;
|
|
1011
|
+
const scopedQueryKey = scopeKey(request.queryKey, request.scope);
|
|
1012
|
+
const isFetching = dispatcher.hasRunningRequests(scopedQueryKey);
|
|
1013
|
+
const isQueued = dispatcher.getIsActiveQueue(scopedQueryKey);
|
|
1014
|
+
const isActive = isFetching || isQueued;
|
|
1015
|
+
if (isBlurred && refetchBlurred && !isActive || !isBlurred && !isActive) {
|
|
1016
|
+
handleFetch();
|
|
1017
|
+
logger.debug({
|
|
1018
|
+
title: `Performing refresh request`,
|
|
1019
|
+
type: "system",
|
|
1020
|
+
extra: { request }
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
handleRefresh();
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
const refetch = () => {
|
|
1027
|
+
handleFetch();
|
|
1028
|
+
handleRefresh();
|
|
1029
|
+
};
|
|
1030
|
+
const getIsFetchingIdentity = () => {
|
|
1031
|
+
return dispatcher.getRunningRequests(queryKey).some((running) => running.request.cacheKey === cacheKey);
|
|
1032
|
+
};
|
|
1033
|
+
const initialFetchData = () => {
|
|
1034
|
+
const hasStaleData = getStaleStatus();
|
|
1035
|
+
const isFetching = getIsFetchingIdentity();
|
|
1036
|
+
if ((revalidate || hasStaleData) && !isFetching) handleFetch();
|
|
1037
|
+
};
|
|
1038
|
+
const updateFetchData = () => {
|
|
1039
|
+
const hasStaleData = getStaleStatus();
|
|
1040
|
+
const shouldUpdate = !revalidate ? hasStaleData : true;
|
|
1041
|
+
/**
|
|
1042
|
+
* This is a hack to avoid double rendering in React 18
|
|
1043
|
+
* It renders initial mount event and allow us to consume only hook updates
|
|
1044
|
+
*/
|
|
1045
|
+
if (!ignoreReact18DoubleRender.current && shouldUpdate)
|
|
1046
|
+
/**
|
|
1047
|
+
* While debouncing we need to make sure that first request is not debounced when the cache is not available
|
|
1048
|
+
* This way it will not wait for debouncing but fetch data right away
|
|
1049
|
+
*/
|
|
1050
|
+
if (bounce) {
|
|
1051
|
+
logger.debug({
|
|
1052
|
+
title: `Bounce request with ${bounceType}`,
|
|
1053
|
+
type: "system",
|
|
1054
|
+
extra: {
|
|
1055
|
+
queryKey,
|
|
1056
|
+
request
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
bounceFunction(() => handleFetch());
|
|
1060
|
+
} else handleFetch();
|
|
1061
|
+
else ignoreReact18DoubleRender.current = false;
|
|
1062
|
+
};
|
|
1063
|
+
const handleMountEvents = () => {
|
|
1064
|
+
addCacheDataListener(request);
|
|
1065
|
+
addLifecycleListeners(request);
|
|
1066
|
+
const focusUnmount = appManager.events.onFocus(() => {
|
|
1067
|
+
if (refetchOnFocus && !disabled) {
|
|
1068
|
+
handleFetch();
|
|
1069
|
+
handleRefresh();
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
const blurUnmount = appManager.events.onBlur(() => {
|
|
1073
|
+
if (refetchOnBlur && !disabled) {
|
|
1074
|
+
handleFetch();
|
|
1075
|
+
handleRefresh();
|
|
1076
|
+
}
|
|
1077
|
+
});
|
|
1078
|
+
const onlineUnmount = appManager.events.onOnline(() => {
|
|
1079
|
+
if (refetchOnReconnect && !disabled) {
|
|
1080
|
+
handleFetch();
|
|
1081
|
+
handleRefresh();
|
|
1082
|
+
}
|
|
1083
|
+
});
|
|
1084
|
+
const invalidateUnmount = cache.events.onInvalidateByKey(cacheKey, handleFetch);
|
|
1085
|
+
const deletionUnmount = cache.events.onDeleteByKey(cacheKey, handleFetch);
|
|
1086
|
+
const unmount = () => {
|
|
1087
|
+
clearCacheDataListener();
|
|
1088
|
+
focusUnmount();
|
|
1089
|
+
blurUnmount();
|
|
1090
|
+
onlineUnmount();
|
|
1091
|
+
invalidateUnmount();
|
|
1092
|
+
deletionUnmount();
|
|
1093
|
+
};
|
|
1094
|
+
return unmount;
|
|
1095
|
+
};
|
|
1096
|
+
/**
|
|
1097
|
+
* Initialization of the events related to data exchange with cache and queue
|
|
1098
|
+
* This allows to share the state with other hooks and keep it related
|
|
1099
|
+
*/
|
|
1100
|
+
i(handleMountEvents, [updateKey, disabled], true);
|
|
1101
|
+
/**
|
|
1102
|
+
* Initial fetch triggered once data is stale or we use the refetch strategy
|
|
1103
|
+
*/
|
|
1104
|
+
o(initialFetchData);
|
|
1105
|
+
/**
|
|
1106
|
+
* Fetching logic for updates handling
|
|
1107
|
+
*/
|
|
1108
|
+
i(updateFetchData, [
|
|
1109
|
+
updateKey,
|
|
1110
|
+
disabled,
|
|
1111
|
+
...dependencies
|
|
1112
|
+
], true);
|
|
1113
|
+
/**
|
|
1114
|
+
* Refresh lifecycle handler
|
|
1115
|
+
*/
|
|
1116
|
+
i(handleRefresh, [
|
|
1117
|
+
updateKey,
|
|
1118
|
+
...dependencies,
|
|
1119
|
+
disabled,
|
|
1120
|
+
refresh,
|
|
1121
|
+
refreshTime
|
|
1122
|
+
], true);
|
|
1123
|
+
/**
|
|
1124
|
+
* Reset the ignore flag for React 18 strict mode
|
|
1125
|
+
*/
|
|
1126
|
+
d$1(() => {
|
|
1127
|
+
ignoreReact18DoubleRender.current = true;
|
|
1128
|
+
});
|
|
1129
|
+
if (suspense && !disabled) {
|
|
1130
|
+
const suspenseResult = suspenseResultMap.get(cacheKey);
|
|
1131
|
+
if (suspenseResult) {
|
|
1132
|
+
suspenseResultMap.delete(cacheKey);
|
|
1133
|
+
if (suspenseResult.data !== null) state.data = suspenseResult.data;
|
|
1134
|
+
if (suspenseResult.error !== null) state.error = suspenseResult.error;
|
|
1135
|
+
if (suspenseResult.status !== null) state.status = suspenseResult.status;
|
|
1136
|
+
if (suspenseResult.extra !== null) state.extra = suspenseResult.extra;
|
|
1137
|
+
state.success = suspenseResult.success;
|
|
1138
|
+
}
|
|
1139
|
+
if (!(state.data !== null || state.error !== null)) {
|
|
1140
|
+
let entry = suspensePromiseMap.get(cacheKey);
|
|
1141
|
+
if (!entry) {
|
|
1142
|
+
let resolvePromise;
|
|
1143
|
+
const promise = new Promise((r) => {
|
|
1144
|
+
resolvePromise = r;
|
|
1145
|
+
});
|
|
1146
|
+
const unsubscribe = cache.events.onDataByKey(cacheKey, (cacheData) => {
|
|
1147
|
+
suspenseResultMap.set(cacheKey, {
|
|
1148
|
+
data: cacheData.data ?? null,
|
|
1149
|
+
error: cacheData.error ?? null,
|
|
1150
|
+
status: cacheData.status ?? null,
|
|
1151
|
+
extra: cacheData.extra ?? null,
|
|
1152
|
+
success: cacheData.success ?? false
|
|
1153
|
+
});
|
|
1154
|
+
resolvePromise();
|
|
1155
|
+
unsubscribe();
|
|
1156
|
+
suspensePromiseMap.delete(cacheKey);
|
|
1157
|
+
});
|
|
1158
|
+
entry = {
|
|
1159
|
+
promise,
|
|
1160
|
+
resolve: resolvePromise,
|
|
1161
|
+
cleanup: unsubscribe
|
|
1162
|
+
};
|
|
1163
|
+
suspensePromiseMap.set(cacheKey, entry);
|
|
1164
|
+
}
|
|
1165
|
+
if (!dispatcher.hasRunningRequests(queryKey)) dispatcher.add(request);
|
|
1166
|
+
throw entry.promise;
|
|
1167
|
+
}
|
|
1168
|
+
const entry = suspensePromiseMap.get(cacheKey);
|
|
1169
|
+
if (entry) {
|
|
1170
|
+
entry.cleanup();
|
|
1171
|
+
suspensePromiseMap.delete(cacheKey);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
const trackedKeys = [
|
|
1175
|
+
"data",
|
|
1176
|
+
"error",
|
|
1177
|
+
"loading",
|
|
1178
|
+
"status",
|
|
1179
|
+
"success",
|
|
1180
|
+
"extra",
|
|
1181
|
+
"retries",
|
|
1182
|
+
"responseTimestamp",
|
|
1183
|
+
"requestTimestamp"
|
|
1184
|
+
];
|
|
1185
|
+
return createTrackedProxy({
|
|
1186
|
+
data: state.data,
|
|
1187
|
+
error: state.error,
|
|
1188
|
+
loading: state.loading,
|
|
1189
|
+
status: state.status,
|
|
1190
|
+
success: state.success,
|
|
1191
|
+
extra: state.extra,
|
|
1192
|
+
retries: state.retries,
|
|
1193
|
+
responseTimestamp: state.responseTimestamp,
|
|
1194
|
+
requestTimestamp: state.requestTimestamp,
|
|
1195
|
+
bounce: getBounceData(bounceData),
|
|
1196
|
+
...actions,
|
|
1197
|
+
...callbacks,
|
|
1198
|
+
refetch
|
|
1199
|
+
}, trackedKeys, setRenderKey);
|
|
1200
|
+
};
|
|
1201
|
+
//#endregion
|
|
1202
|
+
//#region src/hooks/use-fetch/use-fetch.utils.ts
|
|
1203
|
+
var getRefreshTime = (refreshTime, dataTimestamp) => {
|
|
1204
|
+
if (dataTimestamp) {
|
|
1205
|
+
const timeDiff = Date.now() - +dataTimestamp;
|
|
1206
|
+
return timeDiff < refreshTime ? refreshTime - timeDiff : refreshTime;
|
|
1207
|
+
}
|
|
1208
|
+
return refreshTime;
|
|
1209
|
+
};
|
|
1210
|
+
//#endregion
|
|
1211
|
+
//#region src/hooks/use-fetch/use-fetch.constants.ts
|
|
1212
|
+
var useFetchDefaultOptions = {
|
|
1213
|
+
suspense: false,
|
|
1214
|
+
dependencies: [],
|
|
1215
|
+
disabled: false,
|
|
1216
|
+
dependencyTracking: true,
|
|
1217
|
+
revalidate: true,
|
|
1218
|
+
initialResponse: null,
|
|
1219
|
+
keepPreviousData: "auto",
|
|
1220
|
+
refresh: false,
|
|
1221
|
+
refreshTime: Time.HOUR,
|
|
1222
|
+
refetchBlurred: true,
|
|
1223
|
+
refetchOnBlur: false,
|
|
1224
|
+
refetchOnFocus: false,
|
|
1225
|
+
refetchOnReconnect: false,
|
|
1226
|
+
bounce: false,
|
|
1227
|
+
bounceType: "debounce",
|
|
1228
|
+
bounceTime: 400,
|
|
1229
|
+
bounceTimeout: 400,
|
|
1230
|
+
deepCompare: true
|
|
1231
|
+
};
|
|
1232
|
+
//#endregion
|
|
1233
|
+
//#region src/hooks/use-submit/use-submit.hooks.ts
|
|
1234
|
+
/**
|
|
1235
|
+
* This hooks aims to mutate data on the server.
|
|
1236
|
+
* @param request
|
|
1237
|
+
* @param options
|
|
1238
|
+
* @returns
|
|
1239
|
+
*/
|
|
1240
|
+
var useSubmit = (request, options) => {
|
|
1241
|
+
const { config: globalConfig } = useProvider();
|
|
1242
|
+
const mergedOptions = useMemo(() => ({
|
|
1243
|
+
...useSubmitDefaultOptions,
|
|
1244
|
+
...globalConfig.useSubmitConfig,
|
|
1245
|
+
...options
|
|
1246
|
+
}), [
|
|
1247
|
+
globalConfig.useSubmitConfig,
|
|
1248
|
+
JSON.stringify(options),
|
|
1249
|
+
options?.deepCompare
|
|
1250
|
+
]);
|
|
1251
|
+
const { disabled, dependencyTracking, initialResponse, bounce, bounceType, bounceTime, deepCompare } = mergedOptions;
|
|
1252
|
+
/**
|
|
1253
|
+
* Because of the dynamic cacheKey / queryKey signing within the request we need to store it's latest instance
|
|
1254
|
+
* so the events got triggered properly and show the latest result without mixing it up
|
|
1255
|
+
*/
|
|
1256
|
+
const { client } = request;
|
|
1257
|
+
const { cache, submitDispatcher: dispatcher, loggerManager } = client;
|
|
1258
|
+
const logger = useRef(loggerManager.initialize(client, "useSubmit")).current;
|
|
1259
|
+
const requestDebounce = f({ delay: bounceTime });
|
|
1260
|
+
const requestThrottle = p({
|
|
1261
|
+
interval: bounceTime,
|
|
1262
|
+
timeout: "bounceTimeout" in mergedOptions ? mergedOptions.bounceTimeout : bounceTime
|
|
1263
|
+
});
|
|
1264
|
+
const bounceResolver = useRef(() => null);
|
|
1265
|
+
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
1266
|
+
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
1267
|
+
/**
|
|
1268
|
+
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
1269
|
+
*/
|
|
1270
|
+
const [state, actions, { setRenderKey, setCacheData, getIsDataProcessing }] = useTrackedState({
|
|
1271
|
+
logger,
|
|
1272
|
+
request,
|
|
1273
|
+
dispatcher,
|
|
1274
|
+
initialResponse,
|
|
1275
|
+
deepCompare,
|
|
1276
|
+
dependencyTracking
|
|
1277
|
+
});
|
|
1278
|
+
/**
|
|
1279
|
+
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
1280
|
+
*/
|
|
1281
|
+
const [callbacks, listeners] = useRequestEvents({
|
|
1282
|
+
logger,
|
|
1283
|
+
actions,
|
|
1284
|
+
request,
|
|
1285
|
+
dispatcher,
|
|
1286
|
+
setCacheData,
|
|
1287
|
+
getIsDataProcessing
|
|
1288
|
+
});
|
|
1289
|
+
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
1290
|
+
const handleSubmit = (submitOptions) => {
|
|
1291
|
+
const requestClone = request.clone(submitOptions);
|
|
1292
|
+
if (disabled) {
|
|
1293
|
+
logger.warning({
|
|
1294
|
+
title: `Cannot submit request`,
|
|
1295
|
+
type: "system",
|
|
1296
|
+
extra: {
|
|
1297
|
+
disabled,
|
|
1298
|
+
submitOptions
|
|
1299
|
+
}
|
|
1300
|
+
});
|
|
1301
|
+
return Promise.resolve({
|
|
1302
|
+
data: null,
|
|
1303
|
+
error: /* @__PURE__ */ new Error("Cannot submit request. Option 'disabled' is enabled"),
|
|
1304
|
+
status: null,
|
|
1305
|
+
extra: request.client.adapter.defaultExtra
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
const triggerRequest = () => {
|
|
1309
|
+
addCacheDataListener(requestClone);
|
|
1310
|
+
return sendRequest(requestClone, {
|
|
1311
|
+
dispatcherType: "submit",
|
|
1312
|
+
...submitOptions,
|
|
1313
|
+
onBeforeSent: (data) => {
|
|
1314
|
+
addLifecycleListeners(requestClone, data.requestId, data.mutationContext);
|
|
1315
|
+
submitOptions?.onBeforeSent?.(data);
|
|
1316
|
+
}
|
|
1317
|
+
});
|
|
1318
|
+
};
|
|
1319
|
+
return new Promise((resolve) => {
|
|
1320
|
+
const performSubmit = async () => {
|
|
1321
|
+
logger.debug({
|
|
1322
|
+
title: `Submitting request`,
|
|
1323
|
+
type: "system",
|
|
1324
|
+
extra: {
|
|
1325
|
+
disabled,
|
|
1326
|
+
submitOptions
|
|
1327
|
+
}
|
|
1328
|
+
});
|
|
1329
|
+
if (bounce) {
|
|
1330
|
+
const bouncedResolve = bounceResolver.current;
|
|
1331
|
+
bounceResolver.current = (value) => {
|
|
1332
|
+
bouncedResolve(value);
|
|
1333
|
+
resolve(value);
|
|
1334
|
+
};
|
|
1335
|
+
bounceFunction(async () => {
|
|
1336
|
+
const callback = bounceResolver.current;
|
|
1337
|
+
bounceResolver.current = () => null;
|
|
1338
|
+
callback(await triggerRequest());
|
|
1339
|
+
});
|
|
1340
|
+
} else resolve(await triggerRequest());
|
|
1341
|
+
};
|
|
1342
|
+
performSubmit();
|
|
1343
|
+
});
|
|
1344
|
+
};
|
|
1345
|
+
const refetch = () => {
|
|
1346
|
+
cache.invalidate(request);
|
|
1347
|
+
};
|
|
1348
|
+
const handlers = {
|
|
1349
|
+
onSubmitSuccess: callbacks.onSuccess,
|
|
1350
|
+
onSubmitError: callbacks.onError,
|
|
1351
|
+
onSubmitFinished: callbacks.onFinished,
|
|
1352
|
+
onSubmitRequestStart: callbacks.onRequestStart,
|
|
1353
|
+
onSubmitResponseStart: callbacks.onResponseStart,
|
|
1354
|
+
onSubmitDownloadProgress: callbacks.onDownloadProgress,
|
|
1355
|
+
onSubmitUploadProgress: callbacks.onUploadProgress,
|
|
1356
|
+
onSubmitOfflineError: callbacks.onOfflineError,
|
|
1357
|
+
onSubmitAbort: callbacks.onAbort
|
|
1358
|
+
};
|
|
1359
|
+
o(() => {
|
|
1360
|
+
addCacheDataListener(request);
|
|
1361
|
+
});
|
|
1362
|
+
const setSubmitRenderKey = (key) => {
|
|
1363
|
+
setRenderKey(key === "submitting" ? "loading" : key);
|
|
1364
|
+
};
|
|
1365
|
+
const trackedKeys = [
|
|
1366
|
+
"data",
|
|
1367
|
+
"error",
|
|
1368
|
+
"submitting",
|
|
1369
|
+
"status",
|
|
1370
|
+
"success",
|
|
1371
|
+
"extra",
|
|
1372
|
+
"retries",
|
|
1373
|
+
"responseTimestamp",
|
|
1374
|
+
"requestTimestamp"
|
|
1375
|
+
];
|
|
1376
|
+
return createTrackedProxy({
|
|
1377
|
+
submit: handleSubmit,
|
|
1378
|
+
data: state.data,
|
|
1379
|
+
error: state.error,
|
|
1380
|
+
submitting: state.loading,
|
|
1381
|
+
status: state.status,
|
|
1382
|
+
success: state.success,
|
|
1383
|
+
extra: state.extra,
|
|
1384
|
+
retries: state.retries,
|
|
1385
|
+
responseTimestamp: state.responseTimestamp,
|
|
1386
|
+
requestTimestamp: state.requestTimestamp,
|
|
1387
|
+
abort: callbacks.abort,
|
|
1388
|
+
...actions,
|
|
1389
|
+
...handlers,
|
|
1390
|
+
bounce: getBounceData(bounceData),
|
|
1391
|
+
refetch
|
|
1392
|
+
}, trackedKeys, setSubmitRenderKey);
|
|
1393
|
+
};
|
|
1394
|
+
//#endregion
|
|
1395
|
+
//#region src/hooks/use-submit/use-submit.constants.ts
|
|
1396
|
+
var useSubmitDefaultOptions = {
|
|
1397
|
+
disabled: false,
|
|
1398
|
+
dependencyTracking: true,
|
|
1399
|
+
initialResponse: null,
|
|
1400
|
+
bounce: false,
|
|
1401
|
+
bounceType: "debounce",
|
|
1402
|
+
bounceTime: 400,
|
|
1403
|
+
deepCompare: true
|
|
1404
|
+
};
|
|
1405
|
+
//#endregion
|
|
1406
|
+
//#region src/hooks/use-queue/use-queue.hooks.ts
|
|
1407
|
+
var canUpdate = (item) => {
|
|
1408
|
+
if (item.success || item.failed) return false;
|
|
1409
|
+
return true;
|
|
1410
|
+
};
|
|
1411
|
+
/**
|
|
1412
|
+
* This hook allows to control dispatchers request queues
|
|
1413
|
+
* @param request
|
|
1414
|
+
* @param options
|
|
1415
|
+
* @returns
|
|
1416
|
+
*/
|
|
1417
|
+
var useQueue = (request, options) => {
|
|
1418
|
+
const { config: globalConfig } = useProvider();
|
|
1419
|
+
const { dispatcherType, keepFinishedRequests } = {
|
|
1420
|
+
...useQueueDefaultOptions,
|
|
1421
|
+
...globalConfig.useQueueConfig,
|
|
1422
|
+
...options
|
|
1423
|
+
};
|
|
1424
|
+
const { abortKey: rawAbortKey, queryKey: rawQueryKey, scope, client } = request;
|
|
1425
|
+
const abortKey = scopeKey(rawAbortKey, scope);
|
|
1426
|
+
const queryKey = scopeKey(rawQueryKey, scope);
|
|
1427
|
+
const { requestManager } = client;
|
|
1428
|
+
const [dispatcher] = getRequestDispatcher(request, dispatcherType);
|
|
1429
|
+
const [stopped, setStopped] = useState(false);
|
|
1430
|
+
const [requests, setRequests] = useState([]);
|
|
1431
|
+
const createRequestsArray = useCallback((queueElements, prevRequests) => {
|
|
1432
|
+
const newRequests = queueElements.filter((el) => !prevRequests?.some((prevEl) => prevEl.requestId === el.requestId)).map((req) => ({
|
|
1433
|
+
failed: false,
|
|
1434
|
+
canceled: false,
|
|
1435
|
+
removed: false,
|
|
1436
|
+
success: false,
|
|
1437
|
+
...req,
|
|
1438
|
+
downloading: {
|
|
1439
|
+
progress: 0,
|
|
1440
|
+
timeLeft: 0,
|
|
1441
|
+
sizeLeft: 0,
|
|
1442
|
+
total: 0,
|
|
1443
|
+
loaded: 0,
|
|
1444
|
+
startTimestamp: 0
|
|
1445
|
+
},
|
|
1446
|
+
uploading: {
|
|
1447
|
+
progress: 0,
|
|
1448
|
+
timeLeft: 0,
|
|
1449
|
+
sizeLeft: 0,
|
|
1450
|
+
total: 0,
|
|
1451
|
+
loaded: 0,
|
|
1452
|
+
startTimestamp: 0
|
|
1453
|
+
},
|
|
1454
|
+
stopRequest: () => dispatcher.stopRequest(queryKey, req.requestId),
|
|
1455
|
+
startRequest: () => dispatcher.startRequest(queryKey, req.requestId),
|
|
1456
|
+
deleteRequest: () => dispatcher.delete(queryKey, req.requestId, abortKey)
|
|
1457
|
+
}));
|
|
1458
|
+
if (keepFinishedRequests && prevRequests) return [...prevRequests, ...newRequests];
|
|
1459
|
+
return newRequests;
|
|
1460
|
+
}, [
|
|
1461
|
+
abortKey,
|
|
1462
|
+
dispatcher,
|
|
1463
|
+
queryKey,
|
|
1464
|
+
keepFinishedRequests
|
|
1465
|
+
]);
|
|
1466
|
+
const mergePayloadType = useCallback((requestId, data) => {
|
|
1467
|
+
setRequests((prev) => prev.map((el) => {
|
|
1468
|
+
if (el.requestId === requestId && canUpdate(el)) return {
|
|
1469
|
+
...el,
|
|
1470
|
+
...data
|
|
1471
|
+
};
|
|
1472
|
+
return el;
|
|
1473
|
+
}));
|
|
1474
|
+
}, []);
|
|
1475
|
+
const getInitialState = () => {
|
|
1476
|
+
const requestQueue = dispatcher.getQueue(queryKey);
|
|
1477
|
+
setStopped(requestQueue.stopped);
|
|
1478
|
+
setRequests(createRequestsArray(requestQueue.requests));
|
|
1479
|
+
};
|
|
1480
|
+
const resolveQueueItems = useCallback((items) => {
|
|
1481
|
+
return items.map((item) => {
|
|
1482
|
+
if (item.request instanceof Request) return item;
|
|
1483
|
+
return {
|
|
1484
|
+
...item,
|
|
1485
|
+
request: client.fromJSON(item.request)
|
|
1486
|
+
};
|
|
1487
|
+
});
|
|
1488
|
+
}, [client]);
|
|
1489
|
+
const updateQueueState = useCallback((values) => {
|
|
1490
|
+
setStopped(values.stopped);
|
|
1491
|
+
setRequests((prev) => createRequestsArray(resolveQueueItems(values.requests), prev));
|
|
1492
|
+
}, [createRequestsArray, resolveQueueItems]);
|
|
1493
|
+
const mountEvents = () => {
|
|
1494
|
+
const unmountChange = dispatcher.events.onQueueChangeByKey(queryKey, updateQueueState);
|
|
1495
|
+
const unmountStatus = dispatcher.events.onQueueStatusChangeByKey(queryKey, updateQueueState);
|
|
1496
|
+
const unmountFailed = requestManager.events.onResponse(({ requestId, response }) => {
|
|
1497
|
+
if (!response.success) setRequests((prev) => prev.map((el) => el.requestId === requestId ? {
|
|
1498
|
+
...el,
|
|
1499
|
+
failed: true
|
|
1500
|
+
} : el));
|
|
1501
|
+
else setRequests((prev) => prev.map((el) => el.requestId === requestId ? {
|
|
1502
|
+
...el,
|
|
1503
|
+
success: true
|
|
1504
|
+
} : el));
|
|
1505
|
+
});
|
|
1506
|
+
const unmountCanceled = requestManager.events.onAbort(({ requestId }) => {
|
|
1507
|
+
setRequests((prev) => prev.map((el) => el.requestId === requestId ? {
|
|
1508
|
+
...el,
|
|
1509
|
+
canceled: true
|
|
1510
|
+
} : el));
|
|
1511
|
+
});
|
|
1512
|
+
const unmountRemoved = requestManager.events.onRemove(({ requestId }) => {
|
|
1513
|
+
setRequests((prev) => prev.map((el) => el.requestId === requestId && canUpdate(el) ? {
|
|
1514
|
+
...el,
|
|
1515
|
+
removed: true
|
|
1516
|
+
} : el));
|
|
1517
|
+
});
|
|
1518
|
+
const unmountDownload = requestManager.events.onDownloadProgress(({ progress, timeLeft, sizeLeft, total, loaded, startTimestamp, requestId }) => {
|
|
1519
|
+
mergePayloadType(requestId, { downloading: {
|
|
1520
|
+
progress,
|
|
1521
|
+
timeLeft,
|
|
1522
|
+
sizeLeft,
|
|
1523
|
+
total,
|
|
1524
|
+
loaded,
|
|
1525
|
+
startTimestamp
|
|
1526
|
+
} });
|
|
1527
|
+
});
|
|
1528
|
+
const unmountUpload = requestManager.events.onUploadProgress(({ progress, timeLeft, sizeLeft, total, loaded, startTimestamp, requestId }) => {
|
|
1529
|
+
mergePayloadType(requestId, { uploading: {
|
|
1530
|
+
progress,
|
|
1531
|
+
timeLeft,
|
|
1532
|
+
sizeLeft,
|
|
1533
|
+
total,
|
|
1534
|
+
loaded,
|
|
1535
|
+
startTimestamp
|
|
1536
|
+
} });
|
|
1537
|
+
});
|
|
1538
|
+
const unmount = () => {
|
|
1539
|
+
unmountStatus();
|
|
1540
|
+
unmountChange();
|
|
1541
|
+
unmountDownload();
|
|
1542
|
+
unmountUpload();
|
|
1543
|
+
unmountFailed();
|
|
1544
|
+
unmountCanceled();
|
|
1545
|
+
unmountRemoved();
|
|
1546
|
+
};
|
|
1547
|
+
return unmount;
|
|
1548
|
+
};
|
|
1549
|
+
useEffect(getInitialState, [
|
|
1550
|
+
createRequestsArray,
|
|
1551
|
+
dispatcher,
|
|
1552
|
+
queryKey
|
|
1553
|
+
]);
|
|
1554
|
+
useEffect(mountEvents, [
|
|
1555
|
+
stopped,
|
|
1556
|
+
requests,
|
|
1557
|
+
setRequests,
|
|
1558
|
+
setStopped,
|
|
1559
|
+
queryKey,
|
|
1560
|
+
dispatcher.events,
|
|
1561
|
+
requestManager.events,
|
|
1562
|
+
updateQueueState,
|
|
1563
|
+
mergePayloadType
|
|
1564
|
+
]);
|
|
1565
|
+
return {
|
|
1566
|
+
stopped,
|
|
1567
|
+
requests,
|
|
1568
|
+
dispatcher,
|
|
1569
|
+
stop: () => dispatcher.stop(queryKey),
|
|
1570
|
+
pause: () => dispatcher.pause(queryKey),
|
|
1571
|
+
start: () => dispatcher.start(queryKey)
|
|
1572
|
+
};
|
|
1573
|
+
};
|
|
1574
|
+
//#endregion
|
|
1575
|
+
//#region src/hooks/use-queue/use-queue.constants.ts
|
|
1576
|
+
var useQueueDefaultOptions = {
|
|
1577
|
+
dispatcherType: "auto",
|
|
1578
|
+
keepFinishedRequests: false
|
|
1579
|
+
};
|
|
1580
|
+
//#endregion
|
|
1581
|
+
//#region src/hooks/use-cache/use-cache.hooks.ts
|
|
1582
|
+
var useCache = (request, options) => {
|
|
1583
|
+
const { cacheKey, client } = request;
|
|
1584
|
+
const { cache, loggerManager } = client;
|
|
1585
|
+
const logger = useRef(loggerManager.initialize(client, "useCache")).current;
|
|
1586
|
+
const [dispatcher] = getRequestDispatcher(request);
|
|
1587
|
+
const updateKey = JSON.stringify(request.toJSON());
|
|
1588
|
+
const { config: globalConfig } = useProvider();
|
|
1589
|
+
const { dependencyTracking, initialResponse, deepCompare } = {
|
|
1590
|
+
...useCacheDefaultOptions,
|
|
1591
|
+
...globalConfig.useCacheConfig,
|
|
1592
|
+
...options
|
|
1593
|
+
};
|
|
1594
|
+
/**
|
|
1595
|
+
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
1596
|
+
*/
|
|
1597
|
+
const [state, actions, { setRenderKey, setCacheData, getIsDataProcessing }] = useTrackedState({
|
|
1598
|
+
logger,
|
|
1599
|
+
request,
|
|
1600
|
+
dispatcher,
|
|
1601
|
+
initialResponse,
|
|
1602
|
+
deepCompare,
|
|
1603
|
+
dependencyTracking
|
|
1604
|
+
});
|
|
1605
|
+
/**
|
|
1606
|
+
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
1607
|
+
*/
|
|
1608
|
+
const [, listeners] = useRequestEvents({
|
|
1609
|
+
logger,
|
|
1610
|
+
actions,
|
|
1611
|
+
request,
|
|
1612
|
+
dispatcher,
|
|
1613
|
+
setCacheData,
|
|
1614
|
+
getIsDataProcessing
|
|
1615
|
+
});
|
|
1616
|
+
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
1617
|
+
const handleMountEvents = () => {
|
|
1618
|
+
const unmountDataListener = addCacheDataListener(request);
|
|
1619
|
+
const unmountLifecycleListener = addLifecycleListeners(request);
|
|
1620
|
+
return () => {
|
|
1621
|
+
unmountDataListener();
|
|
1622
|
+
unmountLifecycleListener();
|
|
1623
|
+
};
|
|
1624
|
+
};
|
|
1625
|
+
i(() => {
|
|
1626
|
+
return handleMountEvents();
|
|
1627
|
+
}, [updateKey], true);
|
|
1628
|
+
const invalidate = (cacheKeys) => {
|
|
1629
|
+
cache.invalidate(cacheKeys ?? cacheKey);
|
|
1630
|
+
};
|
|
1631
|
+
const trackedKeys = [
|
|
1632
|
+
"data",
|
|
1633
|
+
"error",
|
|
1634
|
+
"loading",
|
|
1635
|
+
"status",
|
|
1636
|
+
"success",
|
|
1637
|
+
"extra",
|
|
1638
|
+
"retries",
|
|
1639
|
+
"responseTimestamp",
|
|
1640
|
+
"requestTimestamp"
|
|
1641
|
+
];
|
|
1642
|
+
return createTrackedProxy({
|
|
1643
|
+
data: state.data,
|
|
1644
|
+
error: state.error,
|
|
1645
|
+
loading: state.loading,
|
|
1646
|
+
status: state.status,
|
|
1647
|
+
success: state.success,
|
|
1648
|
+
extra: state.extra,
|
|
1649
|
+
retries: state.retries,
|
|
1650
|
+
responseTimestamp: state.responseTimestamp,
|
|
1651
|
+
requestTimestamp: state.requestTimestamp,
|
|
1652
|
+
...actions,
|
|
1653
|
+
invalidate
|
|
1654
|
+
}, trackedKeys, setRenderKey);
|
|
1655
|
+
};
|
|
1656
|
+
//#endregion
|
|
1657
|
+
//#region src/hooks/use-cache/use-cache.constants.ts
|
|
1658
|
+
var useCacheDefaultOptions = {
|
|
1659
|
+
dependencyTracking: true,
|
|
1660
|
+
initialResponse: null,
|
|
1661
|
+
deepCompare: true
|
|
1662
|
+
};
|
|
1663
|
+
//#endregion
|
|
1664
|
+
//#region src/hooks/use-stream/use-stream.hooks.ts
|
|
1665
|
+
var getInitialState$1 = () => ({
|
|
1666
|
+
text: "",
|
|
1667
|
+
chunks: [],
|
|
1668
|
+
streaming: false,
|
|
1669
|
+
done: false,
|
|
1670
|
+
error: null,
|
|
1671
|
+
extra: null,
|
|
1672
|
+
status: null
|
|
1673
|
+
});
|
|
1674
|
+
/**
|
|
1675
|
+
* Hook for consuming streaming responses chunk-by-chunk.
|
|
1676
|
+
* Works with any request that uses the fetch adapter's `streaming: true` option.
|
|
1677
|
+
*
|
|
1678
|
+
* Provides both accumulated `text` (for LLM/SSE use cases) and raw `chunks`
|
|
1679
|
+
* (for binary streaming / file downloads).
|
|
1680
|
+
*/
|
|
1681
|
+
var useStream = (request, options) => {
|
|
1682
|
+
const { autoStart = false } = options || {};
|
|
1683
|
+
const state = useRef(getInitialState$1());
|
|
1684
|
+
const abortRef = useRef(null);
|
|
1685
|
+
const startedRef = useRef(false);
|
|
1686
|
+
const versionRef = useRef(0);
|
|
1687
|
+
const listenerRef = useRef(null);
|
|
1688
|
+
const subscribe = useCallback((listener) => {
|
|
1689
|
+
listenerRef.current = listener;
|
|
1690
|
+
return () => {
|
|
1691
|
+
listenerRef.current = null;
|
|
1692
|
+
};
|
|
1693
|
+
}, []);
|
|
1694
|
+
const getSnapshot = useCallback(() => versionRef.current, []);
|
|
1695
|
+
useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
1696
|
+
const emitChange = () => {
|
|
1697
|
+
versionRef.current += 1;
|
|
1698
|
+
listenerRef.current?.();
|
|
1699
|
+
};
|
|
1700
|
+
const consumeStream = async (stream) => {
|
|
1701
|
+
const reader = stream.getReader();
|
|
1702
|
+
const decoder = new TextDecoder();
|
|
1703
|
+
try {
|
|
1704
|
+
while (true) {
|
|
1705
|
+
const { done, value } = await reader.read();
|
|
1706
|
+
if (done) break;
|
|
1707
|
+
state.current.chunks = [...state.current.chunks, value];
|
|
1708
|
+
state.current.text += decoder.decode(value, { stream: true });
|
|
1709
|
+
emitChange();
|
|
1710
|
+
}
|
|
1711
|
+
state.current.streaming = false;
|
|
1712
|
+
state.current.done = true;
|
|
1713
|
+
emitChange();
|
|
1714
|
+
} catch (err) {
|
|
1715
|
+
state.current.streaming = false;
|
|
1716
|
+
state.current.error = err;
|
|
1717
|
+
emitChange();
|
|
1718
|
+
}
|
|
1719
|
+
};
|
|
1720
|
+
const start = useCallback(() => {
|
|
1721
|
+
if (state.current.streaming) return;
|
|
1722
|
+
state.current = {
|
|
1723
|
+
...getInitialState$1(),
|
|
1724
|
+
streaming: true
|
|
1725
|
+
};
|
|
1726
|
+
emitChange();
|
|
1727
|
+
request.setOptions({ streaming: true }).send().then((response) => {
|
|
1728
|
+
const { data, error, status, extra } = response;
|
|
1729
|
+
state.current.status = status;
|
|
1730
|
+
state.current.extra = extra;
|
|
1731
|
+
if (error) {
|
|
1732
|
+
state.current.streaming = false;
|
|
1733
|
+
state.current.error = error;
|
|
1734
|
+
emitChange();
|
|
1735
|
+
return;
|
|
1736
|
+
}
|
|
1737
|
+
if (data && typeof data.getReader === "function") consumeStream(data);
|
|
1738
|
+
else {
|
|
1739
|
+
state.current.streaming = false;
|
|
1740
|
+
state.current.text = typeof data === "string" ? data : JSON.stringify(data);
|
|
1741
|
+
state.current.done = true;
|
|
1742
|
+
emitChange();
|
|
1743
|
+
}
|
|
1744
|
+
});
|
|
1745
|
+
abortRef.current = () => {
|
|
1746
|
+
request.client.requestManager.abortByKey(scopeKey(request.abortKey, request.scope));
|
|
1747
|
+
};
|
|
1748
|
+
}, [request]);
|
|
1749
|
+
const abort = useCallback(() => {
|
|
1750
|
+
abortRef.current?.();
|
|
1751
|
+
state.current.streaming = false;
|
|
1752
|
+
emitChange();
|
|
1753
|
+
}, []);
|
|
1754
|
+
const reset = useCallback(() => {
|
|
1755
|
+
abortRef.current?.();
|
|
1756
|
+
state.current = getInitialState$1();
|
|
1757
|
+
startedRef.current = false;
|
|
1758
|
+
emitChange();
|
|
1759
|
+
}, []);
|
|
1760
|
+
useEffect(() => {
|
|
1761
|
+
if (autoStart && !startedRef.current) {
|
|
1762
|
+
startedRef.current = true;
|
|
1763
|
+
start();
|
|
1764
|
+
}
|
|
1765
|
+
}, [autoStart, start]);
|
|
1766
|
+
return {
|
|
1767
|
+
text: state.current.text,
|
|
1768
|
+
chunks: state.current.chunks,
|
|
1769
|
+
streaming: state.current.streaming,
|
|
1770
|
+
done: state.current.done,
|
|
1771
|
+
error: state.current.error,
|
|
1772
|
+
extra: state.current.extra,
|
|
1773
|
+
status: state.current.status,
|
|
1774
|
+
start,
|
|
1775
|
+
abort,
|
|
1776
|
+
reset
|
|
1777
|
+
};
|
|
1778
|
+
};
|
|
1779
|
+
//#endregion
|
|
1780
|
+
//#region src/hooks/use-listener/use-listener.hooks.ts
|
|
1781
|
+
var useListener = (listener, options) => {
|
|
1782
|
+
const { config: globalConfig } = useProvider();
|
|
1783
|
+
const { dependencyTracking } = {
|
|
1784
|
+
...globalConfig.useListener,
|
|
1785
|
+
...options
|
|
1786
|
+
};
|
|
1787
|
+
const [state, actions, callbacks, { setRenderKey }] = useSocketState(listener.socket, { dependencyTracking });
|
|
1788
|
+
const removeListenerRef = useRef(null);
|
|
1789
|
+
/**
|
|
1790
|
+
* Callbacks
|
|
1791
|
+
*/
|
|
1792
|
+
const onEventCallback = useRef(null);
|
|
1793
|
+
/**
|
|
1794
|
+
* Actions
|
|
1795
|
+
*/
|
|
1796
|
+
const stopListener = () => {
|
|
1797
|
+
removeListenerRef.current?.();
|
|
1798
|
+
};
|
|
1799
|
+
const listen = () => {
|
|
1800
|
+
stopListener();
|
|
1801
|
+
removeListenerRef.current = listener.listen(({ data, extra }) => {
|
|
1802
|
+
onEventCallback.current?.({
|
|
1803
|
+
data,
|
|
1804
|
+
extra
|
|
1805
|
+
});
|
|
1806
|
+
actions.setData(data);
|
|
1807
|
+
actions.setExtra(extra);
|
|
1808
|
+
actions.setTimestamp(+/* @__PURE__ */ new Date());
|
|
1809
|
+
});
|
|
1810
|
+
};
|
|
1811
|
+
const additionalCallbacks = { onEvent: (callback) => {
|
|
1812
|
+
onEventCallback.current = callback;
|
|
1813
|
+
} };
|
|
1814
|
+
/**
|
|
1815
|
+
* Lifecycle
|
|
1816
|
+
*/
|
|
1817
|
+
i(() => {
|
|
1818
|
+
listen();
|
|
1819
|
+
}, [listener.params, JSON.stringify(listener.options)], true);
|
|
1820
|
+
d$1(() => {
|
|
1821
|
+
stopListener();
|
|
1822
|
+
});
|
|
1823
|
+
const trackedKeys = [
|
|
1824
|
+
"data",
|
|
1825
|
+
"extra",
|
|
1826
|
+
"connected",
|
|
1827
|
+
"connecting",
|
|
1828
|
+
"timestamp"
|
|
1829
|
+
];
|
|
1830
|
+
return createTrackedProxy({
|
|
1831
|
+
data: state.data,
|
|
1832
|
+
extra: state.extra,
|
|
1833
|
+
connected: state.connected,
|
|
1834
|
+
connecting: state.connecting,
|
|
1835
|
+
timestamp: state.timestamp,
|
|
1836
|
+
...actions,
|
|
1837
|
+
...callbacks,
|
|
1838
|
+
...additionalCallbacks,
|
|
1839
|
+
listen
|
|
1840
|
+
}, trackedKeys, setRenderKey);
|
|
1841
|
+
};
|
|
1842
|
+
//#endregion
|
|
1843
|
+
//#region src/hooks/use-emitter/use-emitter.hooks.ts
|
|
1844
|
+
var useEmitter = (emitter, options) => {
|
|
1845
|
+
const { config: globalConfig } = useProvider();
|
|
1846
|
+
const { dependencyTracking } = {
|
|
1847
|
+
...globalConfig.useEmitter,
|
|
1848
|
+
...options
|
|
1849
|
+
};
|
|
1850
|
+
const [state, actions, callbacks, { setRenderKey }] = useSocketState(emitter.socket, { dependencyTracking });
|
|
1851
|
+
/**
|
|
1852
|
+
* Callbacks
|
|
1853
|
+
*/
|
|
1854
|
+
const onEventStartCallback = useRef(null);
|
|
1855
|
+
const onEventErrorCallback = useRef(null);
|
|
1856
|
+
i(() => {
|
|
1857
|
+
const onEventStart = () => {
|
|
1858
|
+
return emitter.socket.events.onEmitterStartEventByTopic(emitter, () => {
|
|
1859
|
+
onEventStartCallback.current?.(emitter);
|
|
1860
|
+
});
|
|
1861
|
+
};
|
|
1862
|
+
const onEventError = () => {
|
|
1863
|
+
return emitter.socket.events.onEmitterErrorByTopic(emitter, ({ error }) => {
|
|
1864
|
+
onEventErrorCallback.current?.({ error });
|
|
1865
|
+
});
|
|
1866
|
+
};
|
|
1867
|
+
const unmountEventStart = onEventStart();
|
|
1868
|
+
const unmountEventError = onEventError();
|
|
1869
|
+
return () => {
|
|
1870
|
+
unmountEventStart();
|
|
1871
|
+
unmountEventError();
|
|
1872
|
+
};
|
|
1873
|
+
}, [
|
|
1874
|
+
emitter.topic,
|
|
1875
|
+
onEventStartCallback.current,
|
|
1876
|
+
onEventErrorCallback.current
|
|
1877
|
+
], true);
|
|
1878
|
+
/**
|
|
1879
|
+
* Emitter
|
|
1880
|
+
*/
|
|
1881
|
+
const emit = (emitOptions) => {
|
|
1882
|
+
return emitter.emit(emitOptions);
|
|
1883
|
+
};
|
|
1884
|
+
const onEmit = (callback) => {
|
|
1885
|
+
onEventStartCallback.current = callback;
|
|
1886
|
+
};
|
|
1887
|
+
const onEmitError = (callback) => {
|
|
1888
|
+
onEventErrorCallback.current = callback;
|
|
1889
|
+
};
|
|
1890
|
+
const trackedKeys = ["connected", "connecting"];
|
|
1891
|
+
return createTrackedProxy({
|
|
1892
|
+
connected: state.connected,
|
|
1893
|
+
connecting: state.connecting,
|
|
1894
|
+
...actions,
|
|
1895
|
+
...callbacks,
|
|
1896
|
+
onEmit,
|
|
1897
|
+
onEmitError,
|
|
1898
|
+
emit
|
|
1899
|
+
}, trackedKeys, setRenderKey);
|
|
1900
|
+
};
|
|
1901
|
+
//#endregion
|
|
1902
|
+
//#region src/hooks/use-app-manager/use-app-manager.hooks.ts
|
|
1903
|
+
var useAppManager = (client) => {
|
|
1904
|
+
const [online, setIsOnline] = useState(client.appManager.isOnline);
|
|
1905
|
+
const [focused, setIsFocused] = useState(client.appManager.isFocused);
|
|
1906
|
+
const mountEvents = () => {
|
|
1907
|
+
const unmountIsOnline = client.appManager.events.onOnline(() => setIsOnline(true));
|
|
1908
|
+
const unmountIsOffline = client.appManager.events.onOffline(() => setIsOnline(false));
|
|
1909
|
+
const unmountIsFocus = client.appManager.events.onFocus(() => setIsFocused(true));
|
|
1910
|
+
const unmountIsBlur = client.appManager.events.onBlur(() => setIsFocused(false));
|
|
1911
|
+
return () => {
|
|
1912
|
+
unmountIsOnline();
|
|
1913
|
+
unmountIsOffline();
|
|
1914
|
+
unmountIsFocus();
|
|
1915
|
+
unmountIsBlur();
|
|
1916
|
+
};
|
|
1917
|
+
};
|
|
1918
|
+
const setOnline = (isOnline) => {
|
|
1919
|
+
client.appManager.setOnline(isOnline);
|
|
1920
|
+
};
|
|
1921
|
+
const setFocused = (isFocused) => {
|
|
1922
|
+
client.appManager.setFocused(isFocused);
|
|
1923
|
+
};
|
|
1924
|
+
o(mountEvents);
|
|
1925
|
+
return {
|
|
1926
|
+
isOnline: online,
|
|
1927
|
+
isFocused: focused,
|
|
1928
|
+
setOnline,
|
|
1929
|
+
setFocused
|
|
1930
|
+
};
|
|
1931
|
+
};
|
|
1932
|
+
//#endregion
|
|
1933
|
+
export { Provider, createTrackedProxy, getBounceData, getDetailsState, getInitialState, getIsInitiallyLoading, getRefreshTime, getShouldClearState, getTimestamp, getValidCacheData, initialSocketState, initialState, isEmpty, isEqual, isStaleCacheData, useAppManager, useCache, useCacheDefaultOptions, useEmitter, useFetch, useFetchDefaultOptions, useListener, useProvider, useQueue, useQueueDefaultOptions, useRequestEvents, useSocketState, useStream, useSubmit, useSubmitDefaultOptions, useTrackedState };
|
|
1934
|
+
|
|
1935
|
+
//# sourceMappingURL=index.mjs.map
|