@kakadu/components 3.7.2 → 3.7.3
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.
|
@@ -15,11 +15,13 @@ type FetchParameters = {
|
|
|
15
15
|
cacheLifetime?: number;
|
|
16
16
|
};
|
|
17
17
|
export default function useFetch<Type>(url?: string, options?: RequestInit, handler?: (response: Response) => PromiseOrValue<Type | undefined>, parameters?: FetchParameters): {
|
|
18
|
+
isReady: boolean;
|
|
18
19
|
isLoading: boolean;
|
|
19
20
|
response: Type | undefined;
|
|
20
21
|
error: Error | undefined;
|
|
21
22
|
};
|
|
22
23
|
export declare function useJsonFetch<Type = unknown>(url?: string, options?: RequestInit, handler?: (json: unknown) => PromiseOrValue<Type | undefined>, parameters?: FetchParameters): {
|
|
24
|
+
isReady: boolean;
|
|
23
25
|
isLoading: boolean;
|
|
24
26
|
response: Type | undefined;
|
|
25
27
|
error: Error | undefined;
|
package/build/hooks/use-fetch.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("react"),b=new Map,M=new Map,C=new Map;function J(e,s){let t=C.get(e);return t||(t=new Set,C.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&C.delete(e)}}function R(e,s){M.set(e,{timestamp:Date.now(),response:s});const t=C.get(e);if(t)for(const r of t)r(s)}function j(e){const[s,t]=o.useState(!1),[r,l]=o.useState(),[f,d]=o.useState(),v=o.useCallback(async(E,S)=>{try{d(void 0),t(!0);const a=await fetch(E,S),n=e?await e(a):a;return l(n),n}catch(a){const n=a instanceof Error?a:new Error("Unknown error");throw d(n),n}finally{t(!1)}},[e]);return o.useMemo(()=>({isLoading:s,fetch:v,response:r,error:f}),[s,v,r,f])}function k(e){const s=o.useCallback(async t=>{const r=await t.json();return e?e(r):r},[e]);return j(s)}function q(e,s,t,r){const{cacheLifetime:l=1e3*60}=r??{},[f,d]=o.useState(0),[v,E]=o.useState(()=>!!e),[S,a]=o.useState(()=>!!e),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]),[F,g]=o.useState(()=>m()),[L,h]=o.useState(),y=o.useMemo(()=>{var c;return((c=s==null?void 0:s.method)==null?void 0:c.toUpperCase())??"GET"},[s]);return o.useEffect(()=>{if(y!=="GET")return;const c=()=>{document.hidden||d(u=>u+1)};return document.addEventListener("visibilitychange",c),()=>{document.removeEventListener("visibilitychange",c)}},[y]),o.useEffect(()=>{if(!e||!n){E(!1),g(void 0);return}const c=m();E(!0),c===void 0?(g(void 0),h(void 0)):(g(c),h(void 0));const u=J(n,w=>{g(w),h(void 0)});return()=>{u()}},[e,n,m]),o.useEffect(()=>{if(!e||!n){a(!1);return}if(f<0)return;const c=y==="GET";if(c&&m()!==void 0){a(!1);return}let u=!0;h(void 0),a(!0);let w=c&&b.has(n)?b.get(n):void 0;if(!w){const i=fetch(e,s);b.set(n,i),i.then(()=>{b.delete(n)}),w=i}return w.then(async i=>{if(u){const p=t?await t(i):i;c&&R(n,p),g(p),h(void 0)}}).catch(i=>{u&&h(i)}).finally(()=>{u&&a(!1)}),()=>{u=!1}},[e,n,f,y,m,s,t]),o.useMemo(()=>({isReady:v,isLoading:S,response:F,error:L}),[v,S,F,L])}function G(e,s,t,r){const l=o.useCallback(async f=>{const d=await f.json();return t?t(d):d},[t]);return q(e,s,l,r)}exports.default=q;exports.useFetcher=j;exports.useJsonFetch=G;exports.useJsonFetcher=k;
|
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
const
|
|
1
|
+
import { useState as f, useMemo as C, useCallback as F, useEffect as R } from "react";
|
|
2
|
+
const L = /* @__PURE__ */ new Map(), j = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new Map();
|
|
3
3
|
function q(e, n) {
|
|
4
|
-
let t =
|
|
5
|
-
return t || (t = /* @__PURE__ */ new Set(),
|
|
6
|
-
t == null || t.delete(n), t && t.size === 0 &&
|
|
4
|
+
let t = b.get(e);
|
|
5
|
+
return t || (t = /* @__PURE__ */ new Set(), b.set(e, t)), t.add(n), () => {
|
|
6
|
+
t == null || t.delete(n), t && t.size === 0 && b.delete(e);
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
function J(e, n) {
|
|
10
10
|
j.set(e, { timestamp: Date.now(), response: n });
|
|
11
|
-
const t =
|
|
11
|
+
const t = b.get(e);
|
|
12
12
|
if (t)
|
|
13
13
|
for (const r of t)
|
|
14
14
|
r(n);
|
|
15
15
|
}
|
|
16
|
-
function
|
|
17
|
-
const [n, t] =
|
|
18
|
-
async (
|
|
16
|
+
function S(e) {
|
|
17
|
+
const [n, t] = f(!1), [r, h] = f(), [d, u] = f(), l = F(
|
|
18
|
+
async (E, y) => {
|
|
19
19
|
try {
|
|
20
|
-
|
|
21
|
-
const c = await fetch(
|
|
22
|
-
return
|
|
20
|
+
u(void 0), t(!0);
|
|
21
|
+
const c = await fetch(E, y), s = e ? await e(c) : c;
|
|
22
|
+
return h(s), s;
|
|
23
23
|
} catch (c) {
|
|
24
|
-
const
|
|
25
|
-
throw
|
|
24
|
+
const s = c instanceof Error ? c : new Error("Unknown error");
|
|
25
|
+
throw u(s), s;
|
|
26
26
|
} finally {
|
|
27
27
|
t(!1);
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
[e]
|
|
31
31
|
);
|
|
32
|
-
return
|
|
33
|
-
() => ({ isLoading: n, fetch: l, response: r, error:
|
|
34
|
-
[n, l, r,
|
|
32
|
+
return C(
|
|
33
|
+
() => ({ isLoading: n, fetch: l, response: r, error: d }),
|
|
34
|
+
[n, l, r, d]
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
|
-
function
|
|
38
|
-
const n =
|
|
37
|
+
function B(e) {
|
|
38
|
+
const n = F(
|
|
39
39
|
async (t) => {
|
|
40
40
|
const r = await t.json();
|
|
41
41
|
return e ? e(r) : r;
|
|
42
42
|
},
|
|
43
43
|
[e]
|
|
44
44
|
);
|
|
45
|
-
return
|
|
45
|
+
return S(n);
|
|
46
46
|
}
|
|
47
|
-
function
|
|
48
|
-
const { cacheLifetime:
|
|
47
|
+
function T(e, n, t, r) {
|
|
48
|
+
const { cacheLifetime: h = 1e3 * 60 } = r ?? {}, [d, u] = f(0), [l, E] = f(() => !!e), [y, c] = f(() => !!e), s = C(
|
|
49
49
|
() => e ? JSON.stringify({ url: e, options: n }) : "",
|
|
50
50
|
[e, n]
|
|
51
|
-
),
|
|
51
|
+
), m = F(() => {
|
|
52
52
|
if (!s)
|
|
53
53
|
return;
|
|
54
54
|
const o = j.get(s);
|
|
55
55
|
if (o) {
|
|
56
|
-
if (Date.now() - o.timestamp >
|
|
56
|
+
if (Date.now() - o.timestamp > h) {
|
|
57
57
|
j.delete(s);
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
60
|
return o.response;
|
|
61
61
|
}
|
|
62
|
-
}, [s,
|
|
62
|
+
}, [s, h]), [G, w] = f(() => m()), [I, v] = f(), p = C(
|
|
63
63
|
() => {
|
|
64
64
|
var o;
|
|
65
65
|
return ((o = n == null ? void 0 : n.method) == null ? void 0 : o.toUpperCase()) ?? "GET";
|
|
66
66
|
},
|
|
67
67
|
[n]
|
|
68
68
|
);
|
|
69
|
-
return
|
|
70
|
-
if (
|
|
69
|
+
return R(() => {
|
|
70
|
+
if (p !== "GET")
|
|
71
71
|
return;
|
|
72
72
|
const o = () => {
|
|
73
|
-
document.hidden ||
|
|
73
|
+
document.hidden || u((a) => a + 1);
|
|
74
74
|
};
|
|
75
75
|
return document.addEventListener("visibilitychange", o), () => {
|
|
76
76
|
document.removeEventListener("visibilitychange", o);
|
|
77
77
|
};
|
|
78
|
-
}, [
|
|
78
|
+
}, [p]), R(() => {
|
|
79
79
|
if (!e || !s) {
|
|
80
|
-
|
|
80
|
+
E(!1), w(void 0);
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
|
-
const o =
|
|
84
|
-
o === void 0 ? (
|
|
85
|
-
const
|
|
86
|
-
g
|
|
83
|
+
const o = m();
|
|
84
|
+
E(!0), o === void 0 ? (w(void 0), v(void 0)) : (w(o), v(void 0));
|
|
85
|
+
const a = q(s, (g) => {
|
|
86
|
+
w(g), v(void 0);
|
|
87
87
|
});
|
|
88
88
|
return () => {
|
|
89
|
-
|
|
89
|
+
a();
|
|
90
90
|
};
|
|
91
|
-
}, [e, s,
|
|
91
|
+
}, [e, s, m]), R(() => {
|
|
92
92
|
if (!e || !s) {
|
|
93
|
-
|
|
93
|
+
c(!1);
|
|
94
94
|
return;
|
|
95
95
|
}
|
|
96
|
-
if (
|
|
96
|
+
if (d < 0)
|
|
97
97
|
return;
|
|
98
|
-
const o =
|
|
99
|
-
if (o &&
|
|
100
|
-
|
|
98
|
+
const o = p === "GET";
|
|
99
|
+
if (o && m() !== void 0) {
|
|
100
|
+
c(!1);
|
|
101
101
|
return;
|
|
102
102
|
}
|
|
103
|
-
let
|
|
104
|
-
|
|
105
|
-
let
|
|
106
|
-
if (!
|
|
103
|
+
let a = !0;
|
|
104
|
+
v(void 0), c(!0);
|
|
105
|
+
let g = o && L.has(s) ? L.get(s) : void 0;
|
|
106
|
+
if (!g) {
|
|
107
107
|
const i = fetch(e, n);
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}),
|
|
108
|
+
L.set(s, i), i.then(() => {
|
|
109
|
+
L.delete(s);
|
|
110
|
+
}), g = i;
|
|
111
111
|
}
|
|
112
|
-
return
|
|
113
|
-
if (
|
|
112
|
+
return g.then(async (i) => {
|
|
113
|
+
if (a) {
|
|
114
114
|
const M = t ? await t(i) : i;
|
|
115
|
-
o &&
|
|
115
|
+
o && J(s, M), w(M), v(void 0);
|
|
116
116
|
}
|
|
117
117
|
}).catch((i) => {
|
|
118
|
-
|
|
118
|
+
a && v(i);
|
|
119
119
|
}).finally(() => {
|
|
120
|
-
|
|
120
|
+
a && c(!1);
|
|
121
121
|
}), () => {
|
|
122
|
-
|
|
122
|
+
a = !1;
|
|
123
123
|
};
|
|
124
|
-
}, [e, s,
|
|
125
|
-
() => ({
|
|
126
|
-
[l,
|
|
124
|
+
}, [e, s, d, p, m, n, t]), C(
|
|
125
|
+
() => ({ isReady: l, isLoading: y, response: G, error: I }),
|
|
126
|
+
[l, y, G, I]
|
|
127
127
|
);
|
|
128
128
|
}
|
|
129
|
-
function
|
|
130
|
-
const
|
|
131
|
-
async (
|
|
132
|
-
const
|
|
133
|
-
return t ? t(
|
|
129
|
+
function D(e, n, t, r) {
|
|
130
|
+
const h = F(
|
|
131
|
+
async (d) => {
|
|
132
|
+
const u = await d.json();
|
|
133
|
+
return t ? t(u) : u;
|
|
134
134
|
},
|
|
135
135
|
[t]
|
|
136
136
|
);
|
|
137
|
-
return
|
|
137
|
+
return T(e, n, h, r);
|
|
138
138
|
}
|
|
139
139
|
export {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
140
|
+
T as default,
|
|
141
|
+
S as useFetcher,
|
|
142
|
+
D as useJsonFetch,
|
|
143
|
+
B as useJsonFetcher
|
|
144
144
|
};
|