react-easy-loading 0.3.5-beta → 0.3.6-beta

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