react-easy-loading 0.3.0 → 0.3.2
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/dist/create-local-loading.d.ts.map +1 -1
- package/dist/main.esm.js +182 -167
- package/dist/main.min.js +2 -2
- package/package.json +15 -15
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-local-loading.d.ts","sourceRoot":"","sources":["../src/create-local-loading.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,oBAAoB,EAAE,KAAK,OAAO,EAAC,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"create-local-loading.d.ts","sourceRoot":"","sources":["../src/create-local-loading.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,oBAAoB,EAAE,KAAK,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAOxF,eAAO,MAAM,qBAAqB,GAAI,MAAM,SAAS,OAAO,EAAE,GAAG,MAAM,EAAE,EAAE,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,GAAG,EAAE,uBAAsB,oBAAoB,GAAC,oBAAoB,CAAC,cAAc,CAAM,KAAG,OAAO,CAAC,MAAM,EAAE,OAAO,CAevO,CAAA"}
|
package/dist/main.esm.js
CHANGED
|
@@ -1,53 +1,61 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-easy-loading v0.3.
|
|
2
|
+
* react-easy-loading v0.3.2
|
|
3
3
|
* (c) Hichem Taboukouyout
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
* Github: github.com/HichemTab-tech/react-easy-loading
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { jsx as p, jsxs as
|
|
9
|
-
import { createContext as
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
},
|
|
13
|
-
|
|
8
|
+
import { jsx as p, jsxs as I, Fragment as S } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as B, useEffect as C, useRef as k, useMemo as A, useSyncExternalStore as W, useContext as z } from "react";
|
|
10
|
+
const V = {}, j = {}, ae = (a, e) => {
|
|
11
|
+
V[a] = e;
|
|
12
|
+
}, oe = (a, e) => {
|
|
13
|
+
j[a] = e;
|
|
14
14
|
};
|
|
15
|
-
let
|
|
15
|
+
let R = /* @__PURE__ */ p("div", { children: "Loading..." }), M = (a, e) => /* @__PURE__ */ I("div", { children: [
|
|
16
16
|
/* @__PURE__ */ p("h3", { children: "Errors:" }),
|
|
17
|
-
/* @__PURE__ */ p("ul", { children:
|
|
17
|
+
/* @__PURE__ */ p("ul", { children: a?.map((s, i) => /* @__PURE__ */ p("li", { children: s }, i)) }),
|
|
18
18
|
/* @__PURE__ */ p("button", { onClick: e, disabled: !e, children: "Retry" })
|
|
19
19
|
] });
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
},
|
|
25
|
-
function
|
|
26
|
-
return
|
|
20
|
+
const ue = (a) => {
|
|
21
|
+
R = a;
|
|
22
|
+
}, ce = (a) => {
|
|
23
|
+
M = a;
|
|
24
|
+
}, N = () => R, T = () => M, P = (a, e) => $(N, V, a, e);
|
|
25
|
+
function F(a, e) {
|
|
26
|
+
return $(T, j, a, e);
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
return i ? typeof i == "string" ? e[i] ?? i : i : i === null ? null : s ? typeof s == "string" ? e[s] ?? s : s :
|
|
28
|
+
function $(a, e, s, i) {
|
|
29
|
+
return i ? typeof i == "string" ? e[i] ?? i : i : i === null ? null : s ? typeof s == "string" ? e[s] ?? s : s : a();
|
|
30
30
|
}
|
|
31
|
-
function
|
|
32
|
-
return typeof
|
|
31
|
+
function b(a, ...e) {
|
|
32
|
+
return typeof a == "function" ? a(...e) : a;
|
|
33
|
+
}
|
|
34
|
+
const K = (...a) => {
|
|
35
|
+
}, D = (a) => {
|
|
36
|
+
if (!a) throw new Error("Value is empty");
|
|
37
|
+
return a;
|
|
38
|
+
}, U = () => Math.random().toString(36).substring(2, 15), q = B(void 0), G = () => z(q), _ = [], w = /* @__PURE__ */ Symbol.for("react-shared-states.manager");
|
|
39
|
+
function H(a, e = () => null) {
|
|
40
|
+
const s = globalThis;
|
|
41
|
+
return s[w] || (s[w] = {}), s[w][a] || (s[w][a] = new d(e)), s[w][a];
|
|
33
42
|
}
|
|
34
|
-
const U = (...o) => {
|
|
35
|
-
}, b = (o) => {
|
|
36
|
-
if (!o) throw new Error("Value is empty");
|
|
37
|
-
return o;
|
|
38
|
-
}, q = () => Math.random().toString(36).substring(2, 15), N = F(void 0), T = () => z(N), A = [];
|
|
39
43
|
class d {
|
|
40
44
|
constructor(e = () => null) {
|
|
41
45
|
this.defaultValue = e;
|
|
42
46
|
}
|
|
43
47
|
data = /* @__PURE__ */ new Map();
|
|
48
|
+
static INSTANCES = /* @__PURE__ */ new Map();
|
|
49
|
+
static getInstance(e, s = () => null) {
|
|
50
|
+
return H(e, s);
|
|
51
|
+
}
|
|
44
52
|
addListener(e, s, i) {
|
|
45
53
|
const r = d.prefix(e, s), t = this.data.get(r);
|
|
46
54
|
t && t.listeners.push(i);
|
|
47
55
|
}
|
|
48
56
|
removeListener(e, s, i) {
|
|
49
57
|
const r = d.prefix(e, s), t = this.data.get(r);
|
|
50
|
-
t && (t.listeners = t.listeners.filter((
|
|
58
|
+
t && (t.listeners = t.listeners.filter((o) => o !== i));
|
|
51
59
|
}
|
|
52
60
|
callListeners(e, s) {
|
|
53
61
|
const i = d.prefix(e, s), r = this.data.get(i);
|
|
@@ -63,11 +71,11 @@ class d {
|
|
|
63
71
|
}
|
|
64
72
|
createStatic(e, s, i) {
|
|
65
73
|
const r = i ?? "_global", t = {
|
|
66
|
-
key:
|
|
74
|
+
key: U(),
|
|
67
75
|
prefix: r,
|
|
68
76
|
...e
|
|
69
77
|
};
|
|
70
|
-
return
|
|
78
|
+
return _.push(t), this.init(t.key, t.prefix, s, !0), this.defaultValue = () => s, t;
|
|
71
79
|
}
|
|
72
80
|
initStatic(e) {
|
|
73
81
|
const { key: s, prefix: i } = e;
|
|
@@ -75,16 +83,16 @@ class d {
|
|
|
75
83
|
}
|
|
76
84
|
clearAll(e = !1, s = !1) {
|
|
77
85
|
this.data.forEach((i, r) => {
|
|
78
|
-
const [t,
|
|
79
|
-
this.clear(
|
|
86
|
+
const [t, o] = d.extractPrefix(r);
|
|
87
|
+
this.clear(o, t, e, s);
|
|
80
88
|
});
|
|
81
89
|
}
|
|
82
90
|
clear(e, s, i = !1, r = !1) {
|
|
83
91
|
const t = d.prefix(e, s);
|
|
84
92
|
i || this.callListeners(e, s);
|
|
85
|
-
const
|
|
86
|
-
if (
|
|
87
|
-
const u =
|
|
93
|
+
const o = this.data.get(t);
|
|
94
|
+
if (o && (this.data.delete(t), o.isStatic && !r)) {
|
|
95
|
+
const u = _.find((n) => n.key === e && n.prefix === s);
|
|
88
96
|
u && this.initStatic(u);
|
|
89
97
|
}
|
|
90
98
|
}
|
|
@@ -109,14 +117,14 @@ class d {
|
|
|
109
117
|
return [s[0], s.slice(1).join("//")];
|
|
110
118
|
}
|
|
111
119
|
useEffect(e, s, i = null) {
|
|
112
|
-
|
|
113
|
-
i?.(),
|
|
120
|
+
C(() => () => {
|
|
121
|
+
i?.(), K(`[${d.prefix(e, s)}]`, "unmount effect");
|
|
114
122
|
const r = this.get(e, s);
|
|
115
123
|
r && r.listeners?.length === 0 && this.clear(e, s);
|
|
116
124
|
}, [e, s]);
|
|
117
125
|
}
|
|
118
126
|
}
|
|
119
|
-
class
|
|
127
|
+
class J {
|
|
120
128
|
constructor(e) {
|
|
121
129
|
this.sharedData = e;
|
|
122
130
|
}
|
|
@@ -126,9 +134,9 @@ class G {
|
|
|
126
134
|
const { key: u, prefix: n } = e;
|
|
127
135
|
i = u, r = n;
|
|
128
136
|
} else
|
|
129
|
-
i =
|
|
130
|
-
const t = r || "_global",
|
|
131
|
-
return
|
|
137
|
+
i = D(e);
|
|
138
|
+
const t = r || "_global", o = this.sharedData.get(i, t);
|
|
139
|
+
return o ? { value: o.value, key: i, prefix: t } : {
|
|
132
140
|
key: i,
|
|
133
141
|
prefix: t,
|
|
134
142
|
value: void 0
|
|
@@ -143,9 +151,9 @@ class G {
|
|
|
143
151
|
const { key: u, prefix: n } = e;
|
|
144
152
|
r = u, t = n;
|
|
145
153
|
} else
|
|
146
|
-
r =
|
|
147
|
-
const
|
|
148
|
-
this.sharedData.init(r,
|
|
154
|
+
r = D(e);
|
|
155
|
+
const o = t || "_global";
|
|
156
|
+
this.sharedData.init(r, o, s), this.sharedData.setValue(r, o, s), this.sharedData.callListeners(r, o);
|
|
149
157
|
}
|
|
150
158
|
update(e, s, i) {
|
|
151
159
|
const r = this._get(e, i);
|
|
@@ -154,16 +162,19 @@ class G {
|
|
|
154
162
|
this.set(r.key, t, r.prefix);
|
|
155
163
|
}
|
|
156
164
|
}
|
|
165
|
+
// noinspection JSUnusedGlobalSymbols
|
|
157
166
|
clearAll() {
|
|
158
167
|
this.sharedData.clearAll();
|
|
159
168
|
}
|
|
169
|
+
// noinspection JSUnusedGlobalSymbols
|
|
160
170
|
clearScope(e) {
|
|
161
171
|
const s = e || "_global";
|
|
162
172
|
this.sharedData.data.forEach((i, r) => {
|
|
163
|
-
const [t,
|
|
164
|
-
t === s && (this.sharedData.clear(
|
|
173
|
+
const [t, o] = d.extractPrefix(r);
|
|
174
|
+
t === s && (this.sharedData.clear(o, t), this.sharedData.callListeners(o, t));
|
|
165
175
|
});
|
|
166
176
|
}
|
|
177
|
+
// noinspection JSUnusedGlobalSymbols
|
|
167
178
|
resolve(e) {
|
|
168
179
|
const { key: s, prefix: i } = e;
|
|
169
180
|
return this.get(s, i);
|
|
@@ -172,10 +183,12 @@ class G {
|
|
|
172
183
|
let i, r;
|
|
173
184
|
typeof e == "string" ? (i = e, r = s || "_global") : (i = e.key, r = e.prefix), this.sharedData.clear(i, r);
|
|
174
185
|
}
|
|
186
|
+
// noinspection JSUnusedGlobalSymbols
|
|
175
187
|
has(e, s = "_global") {
|
|
176
188
|
const i = s || "_global";
|
|
177
189
|
return !!this.sharedData.has(e, i);
|
|
178
190
|
}
|
|
191
|
+
// noinspection JSUnusedGlobalSymbols
|
|
179
192
|
getAll() {
|
|
180
193
|
const e = {};
|
|
181
194
|
return this.sharedData.data.forEach((s, i) => {
|
|
@@ -190,122 +203,124 @@ class G {
|
|
|
190
203
|
};
|
|
191
204
|
}
|
|
192
205
|
}
|
|
193
|
-
const
|
|
194
|
-
const e =
|
|
206
|
+
const Q = (a) => {
|
|
207
|
+
const e = G();
|
|
195
208
|
return {
|
|
196
|
-
prefix:
|
|
209
|
+
prefix: a ?? e?.scopeName ?? "_global"
|
|
197
210
|
};
|
|
198
211
|
};
|
|
199
|
-
function
|
|
200
|
-
return
|
|
212
|
+
function X(a) {
|
|
213
|
+
return a && a.__esModule && Object.prototype.hasOwnProperty.call(a, "default") ? a.default : a;
|
|
201
214
|
}
|
|
202
|
-
var
|
|
203
|
-
function
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
var
|
|
207
|
-
function r(t,
|
|
208
|
-
if (t ===
|
|
209
|
-
if (t &&
|
|
210
|
-
if (t.constructor !==
|
|
211
|
-
var u, n,
|
|
215
|
+
var L, O;
|
|
216
|
+
function Y() {
|
|
217
|
+
if (O) return L;
|
|
218
|
+
O = 1;
|
|
219
|
+
var a = typeof Element < "u", e = typeof Map == "function", s = typeof Set == "function", i = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
|
|
220
|
+
function r(t, o) {
|
|
221
|
+
if (t === o) return !0;
|
|
222
|
+
if (t && o && typeof t == "object" && typeof o == "object") {
|
|
223
|
+
if (t.constructor !== o.constructor) return !1;
|
|
224
|
+
var u, n, c;
|
|
212
225
|
if (Array.isArray(t)) {
|
|
213
|
-
if (u = t.length, u !=
|
|
226
|
+
if (u = t.length, u != o.length) return !1;
|
|
214
227
|
for (n = u; n-- !== 0; )
|
|
215
|
-
if (!r(t[n],
|
|
228
|
+
if (!r(t[n], o[n])) return !1;
|
|
216
229
|
return !0;
|
|
217
230
|
}
|
|
218
|
-
var
|
|
219
|
-
if (e && t instanceof Map &&
|
|
220
|
-
if (t.size !==
|
|
221
|
-
for (
|
|
222
|
-
if (!
|
|
223
|
-
for (
|
|
224
|
-
if (!r(n.value[1],
|
|
231
|
+
var l;
|
|
232
|
+
if (e && t instanceof Map && o instanceof Map) {
|
|
233
|
+
if (t.size !== o.size) return !1;
|
|
234
|
+
for (l = t.entries(); !(n = l.next()).done; )
|
|
235
|
+
if (!o.has(n.value[0])) return !1;
|
|
236
|
+
for (l = t.entries(); !(n = l.next()).done; )
|
|
237
|
+
if (!r(n.value[1], o.get(n.value[0]))) return !1;
|
|
225
238
|
return !0;
|
|
226
239
|
}
|
|
227
|
-
if (s && t instanceof Set &&
|
|
228
|
-
if (t.size !==
|
|
229
|
-
for (
|
|
230
|
-
if (!
|
|
240
|
+
if (s && t instanceof Set && o instanceof Set) {
|
|
241
|
+
if (t.size !== o.size) return !1;
|
|
242
|
+
for (l = t.entries(); !(n = l.next()).done; )
|
|
243
|
+
if (!o.has(n.value[0])) return !1;
|
|
231
244
|
return !0;
|
|
232
245
|
}
|
|
233
|
-
if (i && ArrayBuffer.isView(t) && ArrayBuffer.isView(
|
|
234
|
-
if (u = t.length, u !=
|
|
246
|
+
if (i && ArrayBuffer.isView(t) && ArrayBuffer.isView(o)) {
|
|
247
|
+
if (u = t.length, u != o.length) return !1;
|
|
235
248
|
for (n = u; n-- !== 0; )
|
|
236
|
-
if (t[n] !==
|
|
249
|
+
if (t[n] !== o[n]) return !1;
|
|
237
250
|
return !0;
|
|
238
251
|
}
|
|
239
|
-
if (t.constructor === RegExp) return t.source ===
|
|
240
|
-
if (t.valueOf !== Object.prototype.valueOf && typeof t.valueOf == "function" && typeof
|
|
241
|
-
if (t.toString !== Object.prototype.toString && typeof t.toString == "function" && typeof
|
|
242
|
-
if (
|
|
252
|
+
if (t.constructor === RegExp) return t.source === o.source && t.flags === o.flags;
|
|
253
|
+
if (t.valueOf !== Object.prototype.valueOf && typeof t.valueOf == "function" && typeof o.valueOf == "function") return t.valueOf() === o.valueOf();
|
|
254
|
+
if (t.toString !== Object.prototype.toString && typeof t.toString == "function" && typeof o.toString == "function") return t.toString() === o.toString();
|
|
255
|
+
if (c = Object.keys(t), u = c.length, u !== Object.keys(o).length) return !1;
|
|
243
256
|
for (n = u; n-- !== 0; )
|
|
244
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
245
|
-
if (
|
|
257
|
+
if (!Object.prototype.hasOwnProperty.call(o, c[n])) return !1;
|
|
258
|
+
if (a && t instanceof Element) return !1;
|
|
246
259
|
for (n = u; n-- !== 0; )
|
|
247
|
-
if (!((
|
|
260
|
+
if (!((c[n] === "_owner" || c[n] === "__v" || c[n] === "__o") && t.$$typeof) && !r(t[c[n]], o[c[n]]))
|
|
248
261
|
return !1;
|
|
249
262
|
return !0;
|
|
250
263
|
}
|
|
251
|
-
return t !== t &&
|
|
264
|
+
return t !== t && o !== o;
|
|
252
265
|
}
|
|
253
|
-
return
|
|
266
|
+
return L = function(t, o) {
|
|
254
267
|
try {
|
|
255
|
-
return r(t,
|
|
268
|
+
return r(t, o);
|
|
256
269
|
} catch (u) {
|
|
257
270
|
if ((u.message || "").match(/stack|recursion/i))
|
|
258
271
|
return console.warn("react-fast-compare cannot handle circular refs"), !1;
|
|
259
272
|
throw u;
|
|
260
273
|
}
|
|
261
|
-
},
|
|
274
|
+
}, L;
|
|
262
275
|
}
|
|
263
|
-
var
|
|
264
|
-
const
|
|
265
|
-
function E(
|
|
276
|
+
var Z = Y();
|
|
277
|
+
const ee = /* @__PURE__ */ X(Z), x = d.getInstance("sharedStatesManager"), h = new J(x), te = (a, e) => x.createStatic({ initialValue: a }, a, e);
|
|
278
|
+
function E(a, e, s) {
|
|
266
279
|
let i, r = s;
|
|
267
|
-
if (typeof
|
|
268
|
-
const { key:
|
|
269
|
-
i =
|
|
280
|
+
if (typeof a != "string") {
|
|
281
|
+
const { key: l, prefix: f } = a;
|
|
282
|
+
i = l, r = f;
|
|
270
283
|
} else
|
|
271
|
-
i =
|
|
272
|
-
const { prefix: t } =
|
|
273
|
-
x.removeListener(i, t,
|
|
274
|
-
}), [i, t]), n =
|
|
275
|
-
const
|
|
276
|
-
return
|
|
277
|
-
}, [i, t, e]),
|
|
278
|
-
return x.useEffect(i, t),
|
|
284
|
+
i = D(a);
|
|
285
|
+
const { prefix: t } = Q(r), o = k(void 0), u = A(() => (l) => (x.init(i, t, void 0), x.addListener(i, t, l), () => {
|
|
286
|
+
x.removeListener(i, t, l);
|
|
287
|
+
}), [i, t]), n = A(() => () => {
|
|
288
|
+
const l = x.get(i, t)?.value, f = e(l);
|
|
289
|
+
return ee(o.current, f) ? o.current : (o.current = f, f);
|
|
290
|
+
}, [i, t, e]), c = W(u, n);
|
|
291
|
+
return x.useEffect(i, t), c;
|
|
279
292
|
}
|
|
280
|
-
|
|
281
|
-
|
|
293
|
+
d.getInstance("SharedFunctionValue");
|
|
294
|
+
d.getInstance("SharedSubscription");
|
|
295
|
+
const m = (a, e) => Array.isArray(e) && e.includes(a) || a === e, re = ({
|
|
296
|
+
initialState: a = "loading",
|
|
282
297
|
defaultFallback: e,
|
|
283
298
|
defaultErrorFallback: s,
|
|
284
299
|
defaultRetry: i
|
|
285
300
|
} = {}) => {
|
|
286
|
-
const r =
|
|
287
|
-
state:
|
|
301
|
+
const r = te({
|
|
302
|
+
state: a,
|
|
288
303
|
retry: i
|
|
289
304
|
}), t = (n) => {
|
|
290
|
-
let
|
|
291
|
-
return new Promise((
|
|
305
|
+
let c = u.get();
|
|
306
|
+
return new Promise((l, f) => {
|
|
292
307
|
if (m(u.get(), n)) {
|
|
293
|
-
|
|
308
|
+
l();
|
|
294
309
|
return;
|
|
295
310
|
}
|
|
296
311
|
const g = h.subscribe(r, () => {
|
|
297
|
-
m(u.get(), n) ? (g(),
|
|
312
|
+
m(u.get(), n) ? (g(), l()) : c !== u.get() && f();
|
|
298
313
|
});
|
|
299
314
|
});
|
|
300
|
-
},
|
|
301
|
-
return (
|
|
315
|
+
}, o = function(n, c = () => []) {
|
|
316
|
+
return (l) => E(r, (g) => m(g.state, n)) ? /* @__PURE__ */ p(S, { children: b(l.children, ...c()) }) : null;
|
|
302
317
|
}, u = {
|
|
303
318
|
//hooks
|
|
304
319
|
use: () => E(r, (n) => n.state),
|
|
305
320
|
useIsLoading: () => E(r, (n) => n.state) === "loading",
|
|
306
321
|
useErrors: () => E(r, (n) => n.errors),
|
|
307
322
|
useContext: () => E(r, (n) => n.context),
|
|
308
|
-
set: (n) => h.update(r, (
|
|
323
|
+
set: (n) => h.update(r, (c) => ({ ...c, state: n })),
|
|
309
324
|
retry: (n) => (h.get(r).retry ?? n)?.(),
|
|
310
325
|
// getters
|
|
311
326
|
isLoading: () => h.get(r).state === "loading",
|
|
@@ -320,25 +335,25 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
|
|
|
320
335
|
getContext: () => h.get(r).context,
|
|
321
336
|
// setters
|
|
322
337
|
reset: () => h.set(r, {
|
|
323
|
-
state:
|
|
338
|
+
state: a
|
|
324
339
|
}),
|
|
325
340
|
addError: (n) => {
|
|
326
|
-
h.update(r, (
|
|
327
|
-
...
|
|
328
|
-
errors: [...
|
|
341
|
+
h.update(r, (c) => ({
|
|
342
|
+
...c,
|
|
343
|
+
errors: [...c.errors ?? [], n]
|
|
329
344
|
}));
|
|
330
345
|
},
|
|
331
346
|
setContext: (n) => {
|
|
332
|
-
h.update(r, (
|
|
347
|
+
h.update(r, (c) => ({ ...c, context: n }));
|
|
333
348
|
},
|
|
334
349
|
setErrors: (n) => {
|
|
335
|
-
h.update(r, (
|
|
350
|
+
h.update(r, (c) => ({ ...c, errors: n }));
|
|
336
351
|
},
|
|
337
352
|
clearErrors: () => {
|
|
338
353
|
h.update(r, (n) => ({ ...n, errors: void 0 }));
|
|
339
354
|
},
|
|
340
355
|
// set a retry function
|
|
341
|
-
setRetry: (n) => h.update(r, (
|
|
356
|
+
setRetry: (n) => h.update(r, (c) => ({ ...c, retry: n })),
|
|
342
357
|
// promises
|
|
343
358
|
whenLoaded: () => t("success").catch((n) => {
|
|
344
359
|
throw new Error("loading failed", { cause: n });
|
|
@@ -346,25 +361,25 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
|
|
|
346
361
|
whenFinished: () => t(["success", "error"]),
|
|
347
362
|
// wrap a promise
|
|
348
363
|
wrap(n) {
|
|
349
|
-
const
|
|
364
|
+
const c = () => new Promise((l, f) => {
|
|
350
365
|
u.clearErrors(), u.set("loading"), n().then((g) => {
|
|
351
|
-
u.set("success"),
|
|
366
|
+
u.set("success"), l(g);
|
|
352
367
|
}).catch((g) => {
|
|
353
|
-
u.set("error"), g && u.setErrors([g]),
|
|
368
|
+
u.set("error"), g && u.setErrors([g]), f(g);
|
|
354
369
|
});
|
|
355
370
|
});
|
|
356
|
-
return u.setRetry(
|
|
371
|
+
return u.setRetry(c), c();
|
|
357
372
|
},
|
|
358
373
|
wrapWithControl(n) {
|
|
359
|
-
const
|
|
374
|
+
const c = {
|
|
360
375
|
thens: [],
|
|
361
376
|
catchs: [],
|
|
362
377
|
finallies: []
|
|
363
|
-
},
|
|
364
|
-
then: (...
|
|
365
|
-
catch_: (...
|
|
366
|
-
finally_: (...
|
|
367
|
-
start: () => new Promise((
|
|
378
|
+
}, l = {
|
|
379
|
+
then: (...f) => (c.thens.push(f), l),
|
|
380
|
+
catch_: (...f) => (c.catchs.push(f), l),
|
|
381
|
+
finally_: (...f) => (c.finallies.push(f), l),
|
|
382
|
+
start: () => new Promise((f, g) => {
|
|
368
383
|
u.clearErrors(), u.set("loading");
|
|
369
384
|
let v = n();
|
|
370
385
|
v.then(() => {
|
|
@@ -372,29 +387,29 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
|
|
|
372
387
|
}), v.catch((y) => {
|
|
373
388
|
u.set("error"), y && u.setErrors([y]);
|
|
374
389
|
});
|
|
375
|
-
for (const y of
|
|
390
|
+
for (const y of c.thens)
|
|
376
391
|
v.then(...y);
|
|
377
|
-
for (const y of
|
|
392
|
+
for (const y of c.catchs)
|
|
378
393
|
v.catch(...y);
|
|
379
|
-
for (const y of
|
|
394
|
+
for (const y of c.finallies)
|
|
380
395
|
v.finally(...y);
|
|
381
|
-
v.then(
|
|
396
|
+
v.then(f, g);
|
|
382
397
|
})
|
|
383
398
|
};
|
|
384
|
-
return u.setRetry(
|
|
399
|
+
return u.setRetry(l.start), l;
|
|
385
400
|
},
|
|
386
401
|
// components
|
|
387
402
|
// - show when
|
|
388
|
-
ShowWhenLoaded: ({ children: n, fallback:
|
|
389
|
-
const
|
|
390
|
-
return
|
|
403
|
+
ShowWhenLoaded: ({ children: n, fallback: c, errorFallback: l }) => {
|
|
404
|
+
const f = E(r, (g) => g.state);
|
|
405
|
+
return f === "idle" ? null : (c = P(e, c), l = F(s, l === !0 ? void 0 : l), f === "loading" ? /* @__PURE__ */ p(S, { children: c }) : f === "error" ? /* @__PURE__ */ p(S, { children: b(l, u.getErrors() ?? [], () => u.retry()) }) : /* @__PURE__ */ p(S, { children: n }));
|
|
391
406
|
},
|
|
392
|
-
ShowWhileLoading:
|
|
393
|
-
ShowWhenError:
|
|
394
|
-
ShowWhenFinish:
|
|
395
|
-
ShowWhen: ({ children: n, state:
|
|
396
|
-
const
|
|
397
|
-
return /* @__PURE__ */ p(
|
|
407
|
+
ShowWhileLoading: o("loading"),
|
|
408
|
+
ShowWhenError: o("error", () => [u.getErrors() ?? [], () => u.retry()]),
|
|
409
|
+
ShowWhenFinish: o(["error", "success"], () => [u.getErrors(), () => u.retry()]),
|
|
410
|
+
ShowWhen: ({ children: n, state: c }) => {
|
|
411
|
+
const l = o(c);
|
|
412
|
+
return /* @__PURE__ */ p(l, { children: n });
|
|
398
413
|
},
|
|
399
414
|
destroy: () => {
|
|
400
415
|
h.clear(r);
|
|
@@ -408,38 +423,38 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
|
|
|
408
423
|
__get__id: () => `loading_id//${r.prefix}/${r.key}`
|
|
409
424
|
};
|
|
410
425
|
return u;
|
|
411
|
-
},
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
return
|
|
416
|
-
e.current.destroy();
|
|
426
|
+
}, se = (a) => re(typeof a == "string" ? {
|
|
427
|
+
initialState: a
|
|
428
|
+
} : a), le = (a = {}) => {
|
|
429
|
+
const e = k(void 0);
|
|
430
|
+
return e.current || (e.current = se(a)), C(() => () => {
|
|
431
|
+
e.current.destroy(), e.current = void 0;
|
|
417
432
|
}, []), e.current;
|
|
418
|
-
},
|
|
419
|
-
const r =
|
|
420
|
-
return e = P(void 0, e), r.some((t) => t === "loading") ? e : r.every((t) => t !== "loading") ? r.some((t) => t === "error") ? (s =
|
|
433
|
+
}, fe = ({ of: a, fallback: e, errorFallback: s, children: i }) => {
|
|
434
|
+
const r = a.map((t) => t.use());
|
|
435
|
+
return e = P(void 0, e), r.some((t) => t === "loading") ? e : r.every((t) => t !== "loading") ? r.some((t) => t === "error") ? (s = F(void 0, s), /* @__PURE__ */ p(S, { children: b(
|
|
421
436
|
s,
|
|
422
|
-
|
|
437
|
+
a.reduce((t, o) => [...t, ...o.getErrors() ?? []], []),
|
|
423
438
|
() => {
|
|
424
|
-
|
|
439
|
+
a.map((t) => {
|
|
425
440
|
t.isError() && t.retry();
|
|
426
441
|
});
|
|
427
442
|
}
|
|
428
|
-
) })) : /* @__PURE__ */ p(
|
|
443
|
+
) })) : /* @__PURE__ */ p(S, { children: i }) : null;
|
|
429
444
|
};
|
|
430
445
|
export {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
446
|
+
fe as AllLoaded,
|
|
447
|
+
re as createLoading,
|
|
448
|
+
j as errorFallbacks,
|
|
449
|
+
V as fallbacks,
|
|
450
|
+
T as getDefaultErrorFallback,
|
|
451
|
+
N as getDefaultFallback,
|
|
452
|
+
b as reactNodeOrFunctionValue,
|
|
453
|
+
ce as registerDefaultErrorFallback,
|
|
454
|
+
ue as registerDefaultFallback,
|
|
455
|
+
oe as registerErrorFallback,
|
|
456
|
+
ae as registerFallback,
|
|
457
|
+
F as resolveErrorFallback,
|
|
443
458
|
P as resolveLoadingFallback,
|
|
444
|
-
|
|
459
|
+
le as useCreateLocalLoading
|
|
445
460
|
};
|
package/dist/main.min.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-easy-loading v0.3.
|
|
2
|
+
* react-easy-loading v0.3.2
|
|
3
3
|
* (c) Hichem Taboukouyout
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
* Github: github.com/HichemTab-tech/react-easy-loading
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
(function(d,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],u):(d=typeof globalThis<"u"?globalThis:d||self,u(d.ReactEasyLoading={},d.jsxRuntime,d.React))})(this,(function(d,u,v){"use strict";const D={},k={},W=(o,e)=>{D[o]=e},T=(o,e)=>{k[o]=e};let O=u.jsx("div",{children:"Loading..."}),C=(o,e)=>u.jsxs("div",{children:[u.jsx("h3",{children:"Errors:"}),u.jsx("ul",{children:o?.map((s,i)=>u.jsx("li",{children:s},i))}),u.jsx("button",{onClick:e,disabled:!e,children:"Retry"})]});const q=o=>{O=o},z=o=>{C=o},V=()=>O,j=()=>C,F=(o,e)=>P(V,D,o,e);function A(o,e){return P(j,k,o,e)}function P(o,e,s,i){return i?typeof i=="string"?e[i]??i:i:i===null?null:s?typeof s=="string"?e[s]??s:s:o()}function w(o,...e){return typeof o=="function"?o(...e):o}const I=(...o)=>{},_=o=>{if(!o)throw new Error("Value is empty");return o},K=()=>Math.random().toString(36).substring(2,15),N=v.createContext(void 0),U=()=>v.useContext(N),M=[];class y{constructor(e=()=>null){this.defaultValue=e}data=new Map;addListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&t.listeners.push(i)}removeListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.listeners=t.listeners.filter(a=>a!==i))}callListeners(e,s){const i=y.prefix(e,s),r=this.data.get(i);r&&r.listeners.forEach(t=>t())}init(e,s,i,r=!1){const t=y.prefix(e,s);this.data.has(t)||this.data.set(t,{value:i,isStatic:r||void 0,listeners:[]})}createStatic(e,s,i){const r=i??"_global",t={key:K(),prefix:r,...e};return M.push(t),this.init(t.key,t.prefix,s,!0),this.defaultValue=()=>s,t}initStatic(e){const{key:s,prefix:i}=e;this.init(s,i,this.defaultValue(),!0)}clearAll(e=!1,s=!1){this.data.forEach((i,r)=>{const[t,a]=y.extractPrefix(r);this.clear(a,t,e,s)})}clear(e,s,i=!1,r=!1){const t=y.prefix(e,s);i||this.callListeners(e,s);const a=this.data.get(t);if(a&&(this.data.delete(t),a.isStatic&&!r)){const l=M.find(n=>n.key===e&&n.prefix===s);l&&this.initStatic(l)}}get(e,s){let i=this.has(e,s);if(i)return this.data.get(i)}setValue(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.value=i,this.data.set(r,t))}has(e,s){return this.data.has(y.prefix(e,s))?y.prefix(e,s):this.data.has(y.prefix(e,"_global"))?y.prefix(e,"_global"):void 0}static prefix(e,s){if(e.includes("//"))throw new Error("key cannot contain '//'");return`${s}//${e}`}static extractPrefix(e){const s=e.split("//");return[s[0],s.slice(1).join("//")]}useEffect(e,s,i=null){v.useEffect(()=>()=>{i?.(),I(`[${y.prefix(e,s)}]`,"unmount effect");const r=this.get(e,s);r&&r.listeners?.length===0&&this.clear(e,s)},[e,s])}}class G{constructor(e){this.sharedData=e}_get(e,s){let i,r=s;if(typeof e!="string"){const{key:l,prefix:n}=e;i=l,r=n}else i=_(e);const t=r||"_global",a=this.sharedData.get(i,t);return a?{value:a.value,key:i,prefix:t}:{key:i,prefix:t,value:void 0}}get(e,s){return this._get(e,s).value}set(e,s,i){let r,t=i;if(typeof e!="string"){const{key:l,prefix:n}=e;r=l,t=n}else r=_(e);const a=t||"_global";this.sharedData.init(r,a,s),this.sharedData.setValue(r,a,s),this.sharedData.callListeners(r,a)}update(e,s,i){const r=this._get(e,i);if(r){const t=s(r.value);this.set(r.key,t,r.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(e){const s=e||"_global";this.sharedData.data.forEach((i,r)=>{const[t,a]=y.extractPrefix(r);t===s&&(this.sharedData.clear(a,t),this.sharedData.callListeners(a,t))})}resolve(e){const{key:s,prefix:i}=e;return this.get(s,i)}clear(e,s){let i,r;typeof e=="string"?(i=e,r=s||"_global"):(i=e.key,r=e.prefix),this.sharedData.clear(i,r)}has(e,s="_global"){const i=s||"_global";return!!this.sharedData.has(e,i)}getAll(){const e={};return this.sharedData.data.forEach((s,i)=>{const[r,t]=y.extractPrefix(i);e[r]=e[r]||{},e[r][t]=s.value}),e}subscribe(e,s,i){let r,t;return typeof e=="string"?(r=e,t=i||"_global"):(r=e.key,t=e.prefix),this.sharedData.addListener(r,t,s),()=>{this.sharedData.removeListener(r,t,s)}}}const H=o=>{const e=U();return{prefix:o??e?.scopeName??"_global"}};function J(o){return o&&o.__esModule&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o}var m,B;function Q(){if(B)return m;B=1;var o=typeof Element<"u",e=typeof Map=="function",s=typeof Set=="function",i=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function r(t,a){if(t===a)return!0;if(t&&a&&typeof t=="object"&&typeof a=="object"){if(t.constructor!==a.constructor)return!1;var l,n,f;if(Array.isArray(t)){if(l=t.length,l!=a.length)return!1;for(n=l;n--!==0;)if(!r(t[n],a[n]))return!1;return!0}var c;if(e&&t instanceof Map&&a instanceof Map){if(t.size!==a.size)return!1;for(c=t.entries();!(n=c.next()).done;)if(!a.has(n.value[0]))return!1;for(c=t.entries();!(n=c.next()).done;)if(!r(n.value[1],a.get(n.value[0])))return!1;return!0}if(s&&t instanceof Set&&a instanceof Set){if(t.size!==a.size)return!1;for(c=t.entries();!(n=c.next()).done;)if(!a.has(n.value[0]))return!1;return!0}if(i&&ArrayBuffer.isView(t)&&ArrayBuffer.isView(a)){if(l=t.length,l!=a.length)return!1;for(n=l;n--!==0;)if(t[n]!==a[n])return!1;return!0}if(t.constructor===RegExp)return t.source===a.source&&t.flags===a.flags;if(t.valueOf!==Object.prototype.valueOf&&typeof t.valueOf=="function"&&typeof a.valueOf=="function")return t.valueOf()===a.valueOf();if(t.toString!==Object.prototype.toString&&typeof t.toString=="function"&&typeof a.toString=="function")return t.toString()===a.toString();if(f=Object.keys(t),l=f.length,l!==Object.keys(a).length)return!1;for(n=l;n--!==0;)if(!Object.prototype.hasOwnProperty.call(a,f[n]))return!1;if(o&&t instanceof Element)return!1;for(n=l;n--!==0;)if(!((f[n]==="_owner"||f[n]==="__v"||f[n]==="__o")&&t.$$typeof)&&!r(t[f[n]],a[f[n]]))return!1;return!0}return t!==t&&a!==a}return m=function(t,a){try{return r(t,a)}catch(l){if((l.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw l}},m}var X=Q();const Y=J(X),x=new y,g=new G(x),Z=(o,e)=>x.createStatic({initialValue:o},o,e);function b(o,e,s){let i,r=s;if(typeof o!="string"){const{key:c,prefix:h}=o;i=c,r=h}else i=_(o);const{prefix:t}=H(r),a=v.useRef(void 0),l=v.useMemo(()=>c=>(x.init(i,t,void 0),x.addListener(i,t,c),()=>{x.removeListener(i,t,c)}),[i,t]),n=v.useMemo(()=>()=>{const c=x.get(i,t)?.value,h=e(c);return Y(a.current,h)?a.current:(a.current=h,h)},[i,t,e]),f=v.useSyncExternalStore(l,n);return x.useEffect(i,t),f}const S=(o,e)=>Array.isArray(e)&&e.includes(o)||o===e,$=({initialState:o="loading",defaultFallback:e,defaultErrorFallback:s,defaultRetry:i}={})=>{const r=Z({state:o,retry:i}),t=n=>{let f=l.get();return new Promise((c,h)=>{if(S(l.get(),n)){c();return}const p=g.subscribe(r,()=>{S(l.get(),n)?(p(),c()):f!==l.get()&&h()})})},a=function(n,f=()=>[]){return c=>b(r,p=>S(p.state,n))?u.jsx(u.Fragment,{children:w(c.children,...f())}):null},l={use:()=>b(r,n=>n.state),useIsLoading:()=>b(r,n=>n.state)==="loading",useErrors:()=>b(r,n=>n.errors),useContext:()=>b(r,n=>n.context),set:n=>g.update(r,f=>({...f,state:n})),retry:n=>(g.get(r).retry??n)?.(),isLoading:()=>g.get(r).state==="loading",isIdle:()=>g.get(r).state==="idle",isSuccess:()=>g.get(r).state==="success",isError:()=>g.get(r).state==="error",isFinished:()=>g.get(r).state==="success"||g.get(r).state==="error",is:n=>S(l.get(),n),hasErrors:()=>g.get(r).errors!==void 0,get:()=>g.get(r).state,getErrors:()=>g.get(r).errors,getContext:()=>g.get(r).context,reset:()=>g.set(r,{state:o}),addError:n=>{g.update(r,f=>({...f,errors:[...f.errors??[],n]}))},setContext:n=>{g.update(r,f=>({...f,context:n}))},setErrors:n=>{g.update(r,f=>({...f,errors:n}))},clearErrors:()=>{g.update(r,n=>({...n,errors:void 0}))},setRetry:n=>g.update(r,f=>({...f,retry:n})),whenLoaded:()=>t("success").catch(n=>{throw new Error("loading failed",{cause:n})}),whenFinished:()=>t(["success","error"]),wrap(n){const f=()=>new Promise((c,h)=>{l.clearErrors(),l.set("loading"),n().then(p=>{l.set("success"),c(p)}).catch(p=>{l.set("error"),p&&l.setErrors([p]),h(p)})});return l.setRetry(f),f()},wrapWithControl(n){const f={thens:[],catchs:[],finallies:[]},c={then:(...h)=>(f.thens.push(h),c),catch_:(...h)=>(f.catchs.push(h),c),finally_:(...h)=>(f.finallies.push(h),c),start:()=>new Promise((h,p)=>{l.clearErrors(),l.set("loading");let L=n();L.then(()=>{l.set("success")}),L.catch(E=>{l.set("error"),E&&l.setErrors([E])});for(const E of f.thens)L.then(...E);for(const E of f.catchs)L.catch(...E);for(const E of f.finallies)L.finally(...E);L.then(h,p)})};return l.setRetry(c.start),c},ShowWhenLoaded:({children:n,fallback:f,errorFallback:c})=>{const h=b(r,p=>p.state);return h==="idle"?null:(f=F(e,f),c=A(s,c===!0?void 0:c),h==="loading"?u.jsx(u.Fragment,{children:f}):h==="error"?u.jsx(u.Fragment,{children:w(c,l.getErrors()??[],()=>l.retry())}):u.jsx(u.Fragment,{children:n}))},ShowWhileLoading:a("loading"),ShowWhenError:a("error",()=>[l.getErrors()??[],()=>l.retry()]),ShowWhenFinish:a(["error","success"],()=>[l.getErrors(),()=>l.retry()]),ShowWhen:({children:n,state:f})=>{const c=a(f);return u.jsx(c,{children:n})},destroy:()=>{g.clear(r)},__get__id:()=>`loading_id//${r.prefix}/${r.key}`};return l},R=(o={})=>{const e=v.useRef($(typeof o=="string"?{initialState:o}:o));return v.useEffect(()=>()=>{e.current.destroy()},[]),e.current},ee=({of:o,fallback:e,errorFallback:s,children:i})=>{const r=o.map(t=>t.use());return e=F(void 0,e),r.some(t=>t==="loading")?e:r.every(t=>t!=="loading")?r.some(t=>t==="error")?(s=A(void 0,s),u.jsx(u.Fragment,{children:w(s,o.reduce((t,a)=>[...t,...a.getErrors()??[]],[]),()=>{o.map(t=>{t.isError()&&t.retry()})})})):u.jsx(u.Fragment,{children:i}):null};d.AllLoaded=ee,d.createLoading=$,d.errorFallbacks=k,d.fallbacks=D,d.getDefaultErrorFallback=j,d.getDefaultFallback=V,d.reactNodeOrFunctionValue=w,d.registerDefaultErrorFallback=z,d.registerDefaultFallback=q,d.registerErrorFallback=T,d.registerFallback=W,d.resolveErrorFallback=A,d.resolveLoadingFallback=F,d.useCreateLocalLoading=R,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
|
8
|
+
(function(d,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],f):(d=typeof globalThis<"u"?globalThis:d||self,f(d.ReactEasyLoading={},d.jsxRuntime,d.React))})(this,(function(d,f,v){"use strict";const F={},k={},B=(a,e)=>{F[a]=e},N=(a,e)=>{k[a]=e};let C=f.jsx("div",{children:"Loading..."}),V=(a,e)=>f.jsxs("div",{children:[f.jsx("h3",{children:"Errors:"}),f.jsx("ul",{children:a?.map((s,i)=>f.jsx("li",{children:s},i))}),f.jsx("button",{onClick:e,disabled:!e,children:"Retry"})]});const W=a=>{C=a},z=a=>{V=a},M=()=>C,P=()=>V,A=(a,e)=>j(M,F,a,e);function m(a,e){return j(P,k,a,e)}function j(a,e,s,i){return i?typeof i=="string"?e[i]??i:i:i===null?null:s?typeof s=="string"?e[s]??s:s:a()}function L(a,...e){return typeof a=="function"?a(...e):a}const q=(...a)=>{},_=a=>{if(!a)throw new Error("Value is empty");return a},K=()=>Math.random().toString(36).substring(2,15),U=v.createContext(void 0),G=()=>v.useContext(U),T=[],w=Symbol.for("react-shared-states.manager");function H(a,e=()=>null){const s=globalThis;return s[w]||(s[w]={}),s[w][a]||(s[w][a]=new y(e)),s[w][a]}class y{constructor(e=()=>null){this.defaultValue=e}data=new Map;static INSTANCES=new Map;static getInstance(e,s=()=>null){return H(e,s)}addListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&t.listeners.push(i)}removeListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.listeners=t.listeners.filter(o=>o!==i))}callListeners(e,s){const i=y.prefix(e,s),r=this.data.get(i);r&&r.listeners.forEach(t=>t())}init(e,s,i,r=!1){const t=y.prefix(e,s);this.data.has(t)||this.data.set(t,{value:i,isStatic:r||void 0,listeners:[]})}createStatic(e,s,i){const r=i??"_global",t={key:K(),prefix:r,...e};return T.push(t),this.init(t.key,t.prefix,s,!0),this.defaultValue=()=>s,t}initStatic(e){const{key:s,prefix:i}=e;this.init(s,i,this.defaultValue(),!0)}clearAll(e=!1,s=!1){this.data.forEach((i,r)=>{const[t,o]=y.extractPrefix(r);this.clear(o,t,e,s)})}clear(e,s,i=!1,r=!1){const t=y.prefix(e,s);i||this.callListeners(e,s);const o=this.data.get(t);if(o&&(this.data.delete(t),o.isStatic&&!r)){const l=T.find(n=>n.key===e&&n.prefix===s);l&&this.initStatic(l)}}get(e,s){let i=this.has(e,s);if(i)return this.data.get(i)}setValue(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.value=i,this.data.set(r,t))}has(e,s){return this.data.has(y.prefix(e,s))?y.prefix(e,s):this.data.has(y.prefix(e,"_global"))?y.prefix(e,"_global"):void 0}static prefix(e,s){if(e.includes("//"))throw new Error("key cannot contain '//'");return`${s}//${e}`}static extractPrefix(e){const s=e.split("//");return[s[0],s.slice(1).join("//")]}useEffect(e,s,i=null){v.useEffect(()=>()=>{i?.(),q(`[${y.prefix(e,s)}]`,"unmount effect");const r=this.get(e,s);r&&r.listeners?.length===0&&this.clear(e,s)},[e,s])}}class J{constructor(e){this.sharedData=e}_get(e,s){let i,r=s;if(typeof e!="string"){const{key:l,prefix:n}=e;i=l,r=n}else i=_(e);const t=r||"_global",o=this.sharedData.get(i,t);return o?{value:o.value,key:i,prefix:t}:{key:i,prefix:t,value:void 0}}get(e,s){return this._get(e,s).value}set(e,s,i){let r,t=i;if(typeof e!="string"){const{key:l,prefix:n}=e;r=l,t=n}else r=_(e);const o=t||"_global";this.sharedData.init(r,o,s),this.sharedData.setValue(r,o,s),this.sharedData.callListeners(r,o)}update(e,s,i){const r=this._get(e,i);if(r){const t=s(r.value);this.set(r.key,t,r.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(e){const s=e||"_global";this.sharedData.data.forEach((i,r)=>{const[t,o]=y.extractPrefix(r);t===s&&(this.sharedData.clear(o,t),this.sharedData.callListeners(o,t))})}resolve(e){const{key:s,prefix:i}=e;return this.get(s,i)}clear(e,s){let i,r;typeof e=="string"?(i=e,r=s||"_global"):(i=e.key,r=e.prefix),this.sharedData.clear(i,r)}has(e,s="_global"){const i=s||"_global";return!!this.sharedData.has(e,i)}getAll(){const e={};return this.sharedData.data.forEach((s,i)=>{const[r,t]=y.extractPrefix(i);e[r]=e[r]||{},e[r][t]=s.value}),e}subscribe(e,s,i){let r,t;return typeof e=="string"?(r=e,t=i||"_global"):(r=e.key,t=e.prefix),this.sharedData.addListener(r,t,s),()=>{this.sharedData.removeListener(r,t,s)}}}const Q=a=>{const e=G();return{prefix:a??e?.scopeName??"_global"}};function X(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var O,$;function Y(){if($)return O;$=1;var a=typeof Element<"u",e=typeof Map=="function",s=typeof Set=="function",i=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function r(t,o){if(t===o)return!0;if(t&&o&&typeof t=="object"&&typeof o=="object"){if(t.constructor!==o.constructor)return!1;var l,n,c;if(Array.isArray(t)){if(l=t.length,l!=o.length)return!1;for(n=l;n--!==0;)if(!r(t[n],o[n]))return!1;return!0}var u;if(e&&t instanceof Map&&o instanceof Map){if(t.size!==o.size)return!1;for(u=t.entries();!(n=u.next()).done;)if(!o.has(n.value[0]))return!1;for(u=t.entries();!(n=u.next()).done;)if(!r(n.value[1],o.get(n.value[0])))return!1;return!0}if(s&&t instanceof Set&&o instanceof Set){if(t.size!==o.size)return!1;for(u=t.entries();!(n=u.next()).done;)if(!o.has(n.value[0]))return!1;return!0}if(i&&ArrayBuffer.isView(t)&&ArrayBuffer.isView(o)){if(l=t.length,l!=o.length)return!1;for(n=l;n--!==0;)if(t[n]!==o[n])return!1;return!0}if(t.constructor===RegExp)return t.source===o.source&&t.flags===o.flags;if(t.valueOf!==Object.prototype.valueOf&&typeof t.valueOf=="function"&&typeof o.valueOf=="function")return t.valueOf()===o.valueOf();if(t.toString!==Object.prototype.toString&&typeof t.toString=="function"&&typeof o.toString=="function")return t.toString()===o.toString();if(c=Object.keys(t),l=c.length,l!==Object.keys(o).length)return!1;for(n=l;n--!==0;)if(!Object.prototype.hasOwnProperty.call(o,c[n]))return!1;if(a&&t instanceof Element)return!1;for(n=l;n--!==0;)if(!((c[n]==="_owner"||c[n]==="__v"||c[n]==="__o")&&t.$$typeof)&&!r(t[c[n]],o[c[n]]))return!1;return!0}return t!==t&&o!==o}return O=function(t,o){try{return r(t,o)}catch(l){if((l.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw l}},O}var Z=Y();const R=X(Z),S=y.getInstance("sharedStatesManager"),g=new J(S),ee=(a,e)=>S.createStatic({initialValue:a},a,e);function b(a,e,s){let i,r=s;if(typeof a!="string"){const{key:u,prefix:h}=a;i=u,r=h}else i=_(a);const{prefix:t}=Q(r),o=v.useRef(void 0),l=v.useMemo(()=>u=>(S.init(i,t,void 0),S.addListener(i,t,u),()=>{S.removeListener(i,t,u)}),[i,t]),n=v.useMemo(()=>()=>{const u=S.get(i,t)?.value,h=e(u);return R(o.current,h)?o.current:(o.current=h,h)},[i,t,e]),c=v.useSyncExternalStore(l,n);return S.useEffect(i,t),c}y.getInstance("SharedFunctionValue"),y.getInstance("SharedSubscription");const D=(a,e)=>Array.isArray(e)&&e.includes(a)||a===e,I=({initialState:a="loading",defaultFallback:e,defaultErrorFallback:s,defaultRetry:i}={})=>{const r=ee({state:a,retry:i}),t=n=>{let c=l.get();return new Promise((u,h)=>{if(D(l.get(),n)){u();return}const p=g.subscribe(r,()=>{D(l.get(),n)?(p(),u()):c!==l.get()&&h()})})},o=function(n,c=()=>[]){return u=>b(r,p=>D(p.state,n))?f.jsx(f.Fragment,{children:L(u.children,...c())}):null},l={use:()=>b(r,n=>n.state),useIsLoading:()=>b(r,n=>n.state)==="loading",useErrors:()=>b(r,n=>n.errors),useContext:()=>b(r,n=>n.context),set:n=>g.update(r,c=>({...c,state:n})),retry:n=>(g.get(r).retry??n)?.(),isLoading:()=>g.get(r).state==="loading",isIdle:()=>g.get(r).state==="idle",isSuccess:()=>g.get(r).state==="success",isError:()=>g.get(r).state==="error",isFinished:()=>g.get(r).state==="success"||g.get(r).state==="error",is:n=>D(l.get(),n),hasErrors:()=>g.get(r).errors!==void 0,get:()=>g.get(r).state,getErrors:()=>g.get(r).errors,getContext:()=>g.get(r).context,reset:()=>g.set(r,{state:a}),addError:n=>{g.update(r,c=>({...c,errors:[...c.errors??[],n]}))},setContext:n=>{g.update(r,c=>({...c,context:n}))},setErrors:n=>{g.update(r,c=>({...c,errors:n}))},clearErrors:()=>{g.update(r,n=>({...n,errors:void 0}))},setRetry:n=>g.update(r,c=>({...c,retry:n})),whenLoaded:()=>t("success").catch(n=>{throw new Error("loading failed",{cause:n})}),whenFinished:()=>t(["success","error"]),wrap(n){const c=()=>new Promise((u,h)=>{l.clearErrors(),l.set("loading"),n().then(p=>{l.set("success"),u(p)}).catch(p=>{l.set("error"),p&&l.setErrors([p]),h(p)})});return l.setRetry(c),c()},wrapWithControl(n){const c={thens:[],catchs:[],finallies:[]},u={then:(...h)=>(c.thens.push(h),u),catch_:(...h)=>(c.catchs.push(h),u),finally_:(...h)=>(c.finallies.push(h),u),start:()=>new Promise((h,p)=>{l.clearErrors(),l.set("loading");let x=n();x.then(()=>{l.set("success")}),x.catch(E=>{l.set("error"),E&&l.setErrors([E])});for(const E of c.thens)x.then(...E);for(const E of c.catchs)x.catch(...E);for(const E of c.finallies)x.finally(...E);x.then(h,p)})};return l.setRetry(u.start),u},ShowWhenLoaded:({children:n,fallback:c,errorFallback:u})=>{const h=b(r,p=>p.state);return h==="idle"?null:(c=A(e,c),u=m(s,u===!0?void 0:u),h==="loading"?f.jsx(f.Fragment,{children:c}):h==="error"?f.jsx(f.Fragment,{children:L(u,l.getErrors()??[],()=>l.retry())}):f.jsx(f.Fragment,{children:n}))},ShowWhileLoading:o("loading"),ShowWhenError:o("error",()=>[l.getErrors()??[],()=>l.retry()]),ShowWhenFinish:o(["error","success"],()=>[l.getErrors(),()=>l.retry()]),ShowWhen:({children:n,state:c})=>{const u=o(c);return f.jsx(u,{children:n})},destroy:()=>{g.clear(r)},__get__id:()=>`loading_id//${r.prefix}/${r.key}`};return l},te=a=>I(typeof a=="string"?{initialState:a}:a),re=(a={})=>{const e=v.useRef(void 0);return e.current||(e.current=te(a)),v.useEffect(()=>()=>{e.current.destroy(),e.current=void 0},[]),e.current},se=({of:a,fallback:e,errorFallback:s,children:i})=>{const r=a.map(t=>t.use());return e=A(void 0,e),r.some(t=>t==="loading")?e:r.every(t=>t!=="loading")?r.some(t=>t==="error")?(s=m(void 0,s),f.jsx(f.Fragment,{children:L(s,a.reduce((t,o)=>[...t,...o.getErrors()??[]],[]),()=>{a.map(t=>{t.isError()&&t.retry()})})})):f.jsx(f.Fragment,{children:i}):null};d.AllLoaded=se,d.createLoading=I,d.errorFallbacks=k,d.fallbacks=F,d.getDefaultErrorFallback=P,d.getDefaultFallback=M,d.reactNodeOrFunctionValue=L,d.registerDefaultErrorFallback=z,d.registerDefaultFallback=W,d.registerErrorFallback=N,d.registerFallback=B,d.resolveErrorFallback=m,d.resolveLoadingFallback=A,d.useCreateLocalLoading=re,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-easy-loading",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "",
|
|
6
6
|
"keywords": [
|
|
@@ -33,27 +33,27 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/HichemTab-tech/react-easy-loading#readme",
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@tailwindcss/vite": "^4.1.
|
|
36
|
+
"@tailwindcss/vite": "^4.1.18",
|
|
37
37
|
"@testing-library/dom": "^10.4.1",
|
|
38
|
-
"@testing-library/react": "^16.3.
|
|
39
|
-
"@types/node": "^25.0.
|
|
40
|
-
"@types/react": "^19.2.
|
|
41
|
-
"@types/react-dom": "^19.2.
|
|
42
|
-
"@vitejs/plugin-react": "^5.
|
|
38
|
+
"@testing-library/react": "^16.3.1",
|
|
39
|
+
"@types/node": "^25.0.3",
|
|
40
|
+
"@types/react": "^19.2.7",
|
|
41
|
+
"@types/react-dom": "^19.2.3",
|
|
42
|
+
"@vitejs/plugin-react": "^5.1.2",
|
|
43
43
|
"classnames": "^2.5.1",
|
|
44
|
-
"jsdom": "^27.
|
|
45
|
-
"react": "^19.2.
|
|
46
|
-
"react-dom": "^19.2.
|
|
44
|
+
"jsdom": "^27.4.0",
|
|
45
|
+
"react": "^19.2.3",
|
|
46
|
+
"react-dom": "^19.2.3",
|
|
47
47
|
"react-exposed-states": "^1.0.5",
|
|
48
|
-
"react-shared-states": "^2.1.
|
|
49
|
-
"tailwindcss": "^4.1.
|
|
48
|
+
"react-shared-states": "^2.1.2",
|
|
49
|
+
"tailwindcss": "^4.1.18",
|
|
50
50
|
"typescript": "^5.9.3",
|
|
51
|
-
"use-effect-skip-first": "^0.1.
|
|
52
|
-
"vite": "^7.
|
|
51
|
+
"use-effect-skip-first": "^0.1.3",
|
|
52
|
+
"vite": "^7.3.0",
|
|
53
53
|
"vite-plugin-banner": "^0.8.1",
|
|
54
54
|
"vite-plugin-css-injected-by-js": "^3.5.2",
|
|
55
55
|
"vite-plugin-dts": "^4.5.4",
|
|
56
|
-
"vitest": "^4.0.
|
|
56
|
+
"vitest": "^4.0.16"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "vite build",
|