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