@kakadu/components 6.12.0 → 6.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/hooks/use-fetch.js +1 -1
- package/build/hooks/use-fetch.mjs +49 -51
- package/package.json +1 -1
package/build/hooks/use-fetch.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("react"),
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("react"),_=require("./use-is-document-focused.js"),h=new Map,M=new Map,D=new Map,F=new Map,E=new Map;function J(e){return D.get(e)??0}function z(e,s){let t=F.get(e);return t||(t=new Set,F.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&F.delete(e)}}function B(e,s){M.set(e,{timestamp:Date.now(),response:s});const t=F.get(e);if(t)for(const r of t)r(s)}function H(e,s){let t=E.get(e);return t||(t=new Set,E.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&E.delete(e)}}function G(e){D.set(e,J(e)+1),M.delete(e),h.delete(e),queueMicrotask(()=>{const s=E.get(e);if(s)for(const t of s)t()})}function N(...e){for(const s of new Set([...M.keys(),...E.keys(),...h.keys()])){const{url:t}=JSON.parse(s);e.some(r=>typeof r=="function"?r(t):r===t)&&G(s)}}async function O(e,s){if(e.status===204)return null;const t=await e.clone().json();return s?s(t):t}function T(e){const[s,t]=o.useState(!1),[r,l]=o.useState(),[f,p]=o.useState(),w=o.useCallback(async(b,y)=>{try{p(void 0),t(!0);const d=await fetch(b,y),a=e?await e(d):d;return l(a),a}catch(d){const a=d instanceof Error?d:new Error("Unknown error");throw p(a),l(void 0),a}finally{t(!1)}},[e]);return o.useMemo(()=>({isLoading:s,fetch:w,response:r,error:f}),[s,w,r,f])}function P(e){const s=o.useCallback(async t=>O(t,e),[e]);return T(s)}function U(e,s,t,r){const{cacheLifetime:l=1e3*30,shouldFetch:f=!0}=r??{},[p,w]=o.useState(0),[b,y]=o.useState(()=>!!e),[d,a]=o.useState(()=>!!e&&f),n=o.useMemo(()=>e?JSON.stringify({url:e,options:s}):"",[e,s]),m=o.useCallback(()=>{if(!n)return;const c=M.get(n);if(c){if(Date.now()-c.timestamp>l){M.delete(n);return}return c.response}},[n,l]),[L,g]=o.useState(()=>m()),[R,v]=o.useState(),u=o.useMemo(()=>{var c;return((c=s==null?void 0:s.method)==null?void 0:c.toUpperCase())??"GET"},[s])==="GET",k=_.default(u);o.useEffect(()=>{u&&k&&w(c=>c+1)},[u,k]),o.useEffect(()=>{if(!e||!n){y(!1),g(void 0);return}const c=m();y(!0),c===void 0?(g(void 0),v(void 0)):(g(c),v(void 0));const C=z(n,S=>{g(S),v(void 0)});return()=>{C()}},[e,n,m]),o.useEffect(()=>{if(!e||!n||!u)return;const c=H(n,()=>{w(C=>C+1)});return()=>{c()}},[e,n,u]),o.useEffect(()=>{if(!e||!n||p<0||!f){a(!1);return}if(u&&m()!==void 0){a(!1);return}let c=!0;const C=J(n),S=()=>J(n)===C;v(void 0),a(!0);let q=u&&h.has(n)?h.get(n):void 0;if(!q){const i=fetch(e,s);u&&(h.set(n,i),i.finally(()=>{h.get(n)===i&&h.delete(n)})),q=i}return q.then(async i=>{if(c){const j=t?await t(i):i;if(!S())return;u&&B(n,j),g(j),v(void 0)}}).catch(i=>{c&&S()&&(v(i),g(void 0))}).finally(()=>{c&&S()&&a(!1)}),()=>{c=!1}},[e,n,p,f,u,m,s,t]);const I=o.useCallback(()=>{u&&n&&G(n)},[u,n]);return o.useMemo(()=>({isReady:b,isLoading:d,response:L,error:R,purge:I}),[b,d,L,R,I])}function $(e,s,t,r){const l=o.useCallback(async f=>O(f,t),[t]);return U(e,s,l,r)}exports.default=U;exports.purgeCache=N;exports.useFetcher=T;exports.useJsonFetch=$;exports.useJsonFetcher=P;
|
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
import { useState as d, useMemo as b, useCallback as F, useEffect as S } from "react";
|
|
2
|
-
import
|
|
3
|
-
const g = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Map(),
|
|
2
|
+
import O from "./use-is-document-focused.mjs";
|
|
3
|
+
const g = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Map(), B = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(), E = /* @__PURE__ */ new Map();
|
|
4
4
|
function q(e) {
|
|
5
|
-
return
|
|
5
|
+
return B.get(e) ?? 0;
|
|
6
6
|
}
|
|
7
|
-
function
|
|
7
|
+
function T(e, n) {
|
|
8
8
|
let t = I.get(e);
|
|
9
9
|
return t || (t = /* @__PURE__ */ new Set(), I.set(e, t)), t.add(n), () => {
|
|
10
10
|
t == null || t.delete(n), t && t.size === 0 && I.delete(e);
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
|
-
function
|
|
13
|
+
function x(e, n) {
|
|
14
14
|
L.set(e, { timestamp: Date.now(), response: n });
|
|
15
15
|
const t = I.get(e);
|
|
16
16
|
if (t)
|
|
17
17
|
for (const c of t)
|
|
18
18
|
c(n);
|
|
19
19
|
}
|
|
20
|
-
function
|
|
20
|
+
function K(e, n) {
|
|
21
21
|
let t = E.get(e);
|
|
22
22
|
return t || (t = /* @__PURE__ */ new Set(), E.set(e, t)), t.add(n), () => {
|
|
23
23
|
t == null || t.delete(n), t && t.size === 0 && E.delete(e);
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
26
|
+
function H(e) {
|
|
27
|
+
B.set(e, q(e) + 1), L.delete(e), g.delete(e), queueMicrotask(() => {
|
|
28
28
|
const n = E.get(e);
|
|
29
29
|
if (n)
|
|
30
30
|
for (const t of n)
|
|
31
31
|
t();
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
|
-
function
|
|
34
|
+
function W(...e) {
|
|
35
35
|
for (const n of /* @__PURE__ */ new Set([
|
|
36
36
|
...L.keys(),
|
|
37
37
|
...E.keys(),
|
|
@@ -40,27 +40,25 @@ function V(...e) {
|
|
|
40
40
|
const { url: t } = JSON.parse(n);
|
|
41
41
|
e.some(
|
|
42
42
|
(c) => typeof c == "function" ? c(t) : c === t
|
|
43
|
-
) &&
|
|
43
|
+
) && H(n);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
async function
|
|
47
|
-
if (!e.ok)
|
|
48
|
-
throw new Error(`HTTP ${e.status}`);
|
|
46
|
+
async function N(e, n) {
|
|
49
47
|
if (e.status === 204)
|
|
50
48
|
return null;
|
|
51
49
|
const t = await e.clone().json();
|
|
52
50
|
return n ? n(t) : t;
|
|
53
51
|
}
|
|
54
|
-
function
|
|
55
|
-
const [n, t] = d(!1), [c, h] = d(), [f,
|
|
52
|
+
function P(e) {
|
|
53
|
+
const [n, t] = d(!1), [c, h] = d(), [f, v] = d(), w = F(
|
|
56
54
|
async (M, R) => {
|
|
57
55
|
try {
|
|
58
|
-
|
|
56
|
+
v(void 0), t(!0);
|
|
59
57
|
const u = await fetch(M, R), i = e ? await e(u) : u;
|
|
60
58
|
return h(i), i;
|
|
61
59
|
} catch (u) {
|
|
62
60
|
const i = u instanceof Error ? u : new Error("Unknown error");
|
|
63
|
-
throw
|
|
61
|
+
throw v(i), h(void 0), i;
|
|
64
62
|
} finally {
|
|
65
63
|
t(!1);
|
|
66
64
|
}
|
|
@@ -68,19 +66,19 @@ function x(e) {
|
|
|
68
66
|
[e]
|
|
69
67
|
);
|
|
70
68
|
return b(
|
|
71
|
-
() => ({ isLoading: n, fetch:
|
|
72
|
-
[n,
|
|
69
|
+
() => ({ isLoading: n, fetch: w, response: c, error: f }),
|
|
70
|
+
[n, w, c, f]
|
|
73
71
|
);
|
|
74
72
|
}
|
|
75
|
-
function
|
|
73
|
+
function X(e) {
|
|
76
74
|
const n = F(
|
|
77
|
-
async (t) =>
|
|
75
|
+
async (t) => N(t, e),
|
|
78
76
|
[e]
|
|
79
77
|
);
|
|
80
|
-
return
|
|
78
|
+
return P(n);
|
|
81
79
|
}
|
|
82
|
-
function
|
|
83
|
-
const { cacheLifetime: h = 1e3 * 30, shouldFetch: f = !0 } = c ?? {}, [
|
|
80
|
+
function k(e, n, t, c) {
|
|
81
|
+
const { cacheLifetime: h = 1e3 * 30, shouldFetch: f = !0 } = c ?? {}, [v, w] = d(0), [M, R] = d(() => !!e), [u, i] = d(() => !!e && f), s = b(
|
|
84
82
|
() => e ? JSON.stringify({ url: e, options: n }) : "",
|
|
85
83
|
[e, n]
|
|
86
84
|
), m = F(() => {
|
|
@@ -94,24 +92,24 @@ function K(e, n, t, c) {
|
|
|
94
92
|
}
|
|
95
93
|
return o.response;
|
|
96
94
|
}
|
|
97
|
-
}, [s, h]), [D, l] = d(() => m()), [
|
|
95
|
+
}, [s, h]), [D, l] = d(() => m()), [j, p] = d(), r = b(
|
|
98
96
|
() => {
|
|
99
97
|
var o;
|
|
100
98
|
return ((o = n == null ? void 0 : n.method) == null ? void 0 : o.toUpperCase()) ?? "GET";
|
|
101
99
|
},
|
|
102
100
|
[n]
|
|
103
|
-
) === "GET",
|
|
101
|
+
) === "GET", G = O(r);
|
|
104
102
|
S(() => {
|
|
105
|
-
r &&
|
|
106
|
-
}, [r,
|
|
103
|
+
r && G && w((o) => o + 1);
|
|
104
|
+
}, [r, G]), S(() => {
|
|
107
105
|
if (!e || !s) {
|
|
108
106
|
R(!1), l(void 0);
|
|
109
107
|
return;
|
|
110
108
|
}
|
|
111
109
|
const o = m();
|
|
112
|
-
R(!0), o === void 0 ? (l(void 0),
|
|
113
|
-
const y =
|
|
114
|
-
l(C),
|
|
110
|
+
R(!0), o === void 0 ? (l(void 0), p(void 0)) : (l(o), p(void 0));
|
|
111
|
+
const y = T(s, (C) => {
|
|
112
|
+
l(C), p(void 0);
|
|
115
113
|
});
|
|
116
114
|
return () => {
|
|
117
115
|
y();
|
|
@@ -119,14 +117,14 @@ function K(e, n, t, c) {
|
|
|
119
117
|
}, [e, s, m]), S(() => {
|
|
120
118
|
if (!e || !s || !r)
|
|
121
119
|
return;
|
|
122
|
-
const o =
|
|
123
|
-
|
|
120
|
+
const o = K(s, () => {
|
|
121
|
+
w((y) => y + 1);
|
|
124
122
|
});
|
|
125
123
|
return () => {
|
|
126
124
|
o();
|
|
127
125
|
};
|
|
128
126
|
}, [e, s, r]), S(() => {
|
|
129
|
-
if (!e || !s ||
|
|
127
|
+
if (!e || !s || v < 0 || !f) {
|
|
130
128
|
i(!1);
|
|
131
129
|
return;
|
|
132
130
|
}
|
|
@@ -136,7 +134,7 @@ function K(e, n, t, c) {
|
|
|
136
134
|
}
|
|
137
135
|
let o = !0;
|
|
138
136
|
const y = q(s), C = () => q(s) === y;
|
|
139
|
-
|
|
137
|
+
p(void 0), i(!0);
|
|
140
138
|
let J = r && g.has(s) ? g.get(s) : void 0;
|
|
141
139
|
if (!J) {
|
|
142
140
|
const a = fetch(e, n);
|
|
@@ -146,38 +144,38 @@ function K(e, n, t, c) {
|
|
|
146
144
|
}
|
|
147
145
|
return J.then(async (a) => {
|
|
148
146
|
if (o) {
|
|
149
|
-
const
|
|
147
|
+
const z = t ? await t(a) : a;
|
|
150
148
|
if (!C())
|
|
151
149
|
return;
|
|
152
|
-
r &&
|
|
150
|
+
r && x(s, z), l(z), p(void 0);
|
|
153
151
|
}
|
|
154
152
|
}).catch((a) => {
|
|
155
|
-
o && C() && (
|
|
153
|
+
o && C() && (p(a), l(void 0));
|
|
156
154
|
}).finally(() => {
|
|
157
155
|
o && C() && i(!1);
|
|
158
156
|
}), () => {
|
|
159
157
|
o = !1;
|
|
160
158
|
};
|
|
161
|
-
}, [e, s,
|
|
162
|
-
const
|
|
163
|
-
r && s &&
|
|
159
|
+
}, [e, s, v, f, r, m, n, t]);
|
|
160
|
+
const U = F(() => {
|
|
161
|
+
r && s && H(s);
|
|
164
162
|
}, [r, s]);
|
|
165
163
|
return b(
|
|
166
|
-
() => ({ isReady: M, isLoading: u, response: D, error:
|
|
167
|
-
[M, u, D,
|
|
164
|
+
() => ({ isReady: M, isLoading: u, response: D, error: j, purge: U }),
|
|
165
|
+
[M, u, D, j, U]
|
|
168
166
|
);
|
|
169
167
|
}
|
|
170
|
-
function
|
|
168
|
+
function Y(e, n, t, c) {
|
|
171
169
|
const h = F(
|
|
172
|
-
async (f) =>
|
|
170
|
+
async (f) => N(f, t),
|
|
173
171
|
[t]
|
|
174
172
|
);
|
|
175
|
-
return
|
|
173
|
+
return k(e, n, h, c);
|
|
176
174
|
}
|
|
177
175
|
export {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
176
|
+
k as default,
|
|
177
|
+
W as purgeCache,
|
|
178
|
+
P as useFetcher,
|
|
179
|
+
Y as useJsonFetch,
|
|
180
|
+
X as useJsonFetcher
|
|
183
181
|
};
|