react-easy-loading 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"create-local-loading.d.ts","sourceRoot":"","sources":["../src/create-local-loading.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,oBAAoB,EAAE,KAAK,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAGxF,eAAO,MAAM,qBAAqB,GAAI,MAAM,SAAS,OAAO,EAAE,GAAG,MAAM,EAAE,EAAE,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,GAAG,EAAE,uBAAsB,oBAAoB,GAAC,oBAAoB,CAAC,cAAc,CAAM,KAAG,OAAO,CAAC,MAAM,EAAE,OAAO,CAYvO,CAAA"}
1
+ {"version":3,"file":"create-local-loading.d.ts","sourceRoot":"","sources":["../src/create-local-loading.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,oBAAoB,EAAE,KAAK,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAOxF,eAAO,MAAM,qBAAqB,GAAI,MAAM,SAAS,OAAO,EAAE,GAAG,MAAM,EAAE,EAAE,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,GAAG,EAAE,uBAAsB,oBAAoB,GAAC,oBAAoB,CAAC,cAAc,CAAM,KAAG,OAAO,CAAC,MAAM,EAAE,OAAO,CAevO,CAAA"}
package/dist/main.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * react-easy-loading v0.3.0
2
+ * react-easy-loading v0.3.1
3
3
  * (c) Hichem Taboukouyout
4
4
  * Released under the MIT License.
5
5
  * Github: github.com/HichemTab-tech/react-easy-loading
@@ -7,34 +7,34 @@
7
7
 
8
8
  import { jsx as p, jsxs as $, Fragment as w } from "react/jsx-runtime";
9
9
  import { createContext as F, useEffect as O, useRef as j, useMemo as D, useSyncExternalStore as W, useContext as z } from "react";
10
- const k = {}, C = {}, se = (o, e) => {
11
- k[o] = e;
12
- }, ne = (o, e) => {
13
- C[o] = e;
10
+ const k = {}, C = {}, ne = (a, e) => {
11
+ k[a] = e;
12
+ }, ie = (a, e) => {
13
+ C[a] = e;
14
14
  };
15
- let V = /* @__PURE__ */ p("div", { children: "Loading..." }), R = (o, e) => /* @__PURE__ */ $("div", { children: [
15
+ let V = /* @__PURE__ */ p("div", { children: "Loading..." }), R = (a, e) => /* @__PURE__ */ $("div", { children: [
16
16
  /* @__PURE__ */ p("h3", { children: "Errors:" }),
17
- /* @__PURE__ */ p("ul", { children: o?.map((s, i) => /* @__PURE__ */ p("li", { children: s }, 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 ie = (o) => {
21
- V = o;
22
- }, ae = (o) => {
23
- R = o;
24
- }, I = () => V, K = () => R, P = (o, e) => M(I, k, o, e);
25
- function B(o, e) {
26
- return M(K, C, o, e);
20
+ const ae = (a) => {
21
+ V = a;
22
+ }, oe = (a) => {
23
+ R = a;
24
+ }, I = () => V, K = () => R, P = (a, e) => M(I, k, a, e);
25
+ function B(a, e) {
26
+ return M(K, C, a, e);
27
27
  }
28
- function M(o, e, s, i) {
29
- return i ? typeof i == "string" ? e[i] ?? i : i : i === null ? null : s ? typeof s == "string" ? e[s] ?? s : s : o();
28
+ function M(a, e, s, i) {
29
+ return i ? typeof i == "string" ? e[i] ?? i : i : i === null ? null : s ? typeof s == "string" ? e[s] ?? s : s : a();
30
30
  }
31
- function L(o, ...e) {
32
- return typeof o == "function" ? o(...e) : o;
31
+ function L(a, ...e) {
32
+ return typeof a == "function" ? a(...e) : a;
33
33
  }
34
- const U = (...o) => {
35
- }, b = (o) => {
36
- if (!o) throw new Error("Value is empty");
37
- return o;
34
+ const U = (...a) => {
35
+ }, b = (a) => {
36
+ if (!a) throw new Error("Value is empty");
37
+ return a;
38
38
  }, q = () => Math.random().toString(36).substring(2, 15), N = F(void 0), T = () => z(N), A = [];
39
39
  class d {
40
40
  constructor(e = () => null) {
@@ -47,7 +47,7 @@ class d {
47
47
  }
48
48
  removeListener(e, s, i) {
49
49
  const r = d.prefix(e, s), t = this.data.get(r);
50
- t && (t.listeners = t.listeners.filter((a) => a !== i));
50
+ t && (t.listeners = t.listeners.filter((o) => o !== i));
51
51
  }
52
52
  callListeners(e, s) {
53
53
  const i = d.prefix(e, s), r = this.data.get(i);
@@ -75,15 +75,15 @@ class d {
75
75
  }
76
76
  clearAll(e = !1, s = !1) {
77
77
  this.data.forEach((i, r) => {
78
- const [t, a] = d.extractPrefix(r);
79
- this.clear(a, t, e, s);
78
+ const [t, o] = d.extractPrefix(r);
79
+ this.clear(o, t, e, s);
80
80
  });
81
81
  }
82
82
  clear(e, s, i = !1, r = !1) {
83
83
  const t = d.prefix(e, s);
84
84
  i || this.callListeners(e, s);
85
- const a = this.data.get(t);
86
- if (a && (this.data.delete(t), a.isStatic && !r)) {
85
+ const o = this.data.get(t);
86
+ if (o && (this.data.delete(t), o.isStatic && !r)) {
87
87
  const u = A.find((n) => n.key === e && n.prefix === s);
88
88
  u && this.initStatic(u);
89
89
  }
@@ -127,8 +127,8 @@ class G {
127
127
  i = u, r = n;
128
128
  } else
129
129
  i = b(e);
130
- const t = r || "_global", a = this.sharedData.get(i, t);
131
- return a ? { value: a.value, key: i, prefix: t } : {
130
+ const t = r || "_global", o = this.sharedData.get(i, t);
131
+ return o ? { value: o.value, key: i, prefix: t } : {
132
132
  key: i,
133
133
  prefix: t,
134
134
  value: void 0
@@ -144,8 +144,8 @@ class G {
144
144
  r = u, t = n;
145
145
  } else
146
146
  r = b(e);
147
- const a = t || "_global";
148
- this.sharedData.init(r, a, s), this.sharedData.setValue(r, a, s), this.sharedData.callListeners(r, a);
147
+ const o = t || "_global";
148
+ this.sharedData.init(r, o, s), this.sharedData.setValue(r, o, s), this.sharedData.callListeners(r, o);
149
149
  }
150
150
  update(e, s, i) {
151
151
  const r = this._get(e, i);
@@ -160,8 +160,8 @@ class G {
160
160
  clearScope(e) {
161
161
  const s = e || "_global";
162
162
  this.sharedData.data.forEach((i, r) => {
163
- const [t, a] = d.extractPrefix(r);
164
- t === s && (this.sharedData.clear(a, t), this.sharedData.callListeners(a, t));
163
+ const [t, o] = d.extractPrefix(r);
164
+ t === s && (this.sharedData.clear(o, t), this.sharedData.callListeners(o, t));
165
165
  });
166
166
  }
167
167
  resolve(e) {
@@ -190,69 +190,69 @@ class G {
190
190
  };
191
191
  }
192
192
  }
193
- const H = (o) => {
193
+ const H = (a) => {
194
194
  const e = T();
195
195
  return {
196
- prefix: o ?? e?.scopeName ?? "_global"
196
+ prefix: a ?? e?.scopeName ?? "_global"
197
197
  };
198
198
  };
199
- function J(o) {
200
- return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
199
+ function J(a) {
200
+ return a && a.__esModule && Object.prototype.hasOwnProperty.call(a, "default") ? a.default : a;
201
201
  }
202
202
  var S, _;
203
203
  function Q() {
204
204
  if (_) return S;
205
205
  _ = 1;
206
- var o = typeof Element < "u", e = typeof Map == "function", s = typeof Set == "function", i = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
207
- function r(t, a) {
208
- if (t === a) return !0;
209
- if (t && a && typeof t == "object" && typeof a == "object") {
210
- if (t.constructor !== a.constructor) return !1;
211
- var u, n, f;
206
+ var a = typeof Element < "u", e = typeof Map == "function", s = typeof Set == "function", i = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
207
+ function r(t, o) {
208
+ if (t === o) return !0;
209
+ if (t && o && typeof t == "object" && typeof o == "object") {
210
+ if (t.constructor !== o.constructor) return !1;
211
+ var u, n, c;
212
212
  if (Array.isArray(t)) {
213
- if (u = t.length, u != a.length) return !1;
213
+ if (u = t.length, u != o.length) return !1;
214
214
  for (n = u; n-- !== 0; )
215
- if (!r(t[n], a[n])) return !1;
215
+ if (!r(t[n], o[n])) return !1;
216
216
  return !0;
217
217
  }
218
- var c;
219
- if (e && t instanceof Map && a instanceof Map) {
220
- if (t.size !== a.size) return !1;
221
- for (c = t.entries(); !(n = c.next()).done; )
222
- if (!a.has(n.value[0])) return !1;
223
- for (c = t.entries(); !(n = c.next()).done; )
224
- if (!r(n.value[1], a.get(n.value[0]))) return !1;
218
+ var f;
219
+ if (e && t instanceof Map && o instanceof Map) {
220
+ if (t.size !== o.size) return !1;
221
+ for (f = t.entries(); !(n = f.next()).done; )
222
+ if (!o.has(n.value[0])) return !1;
223
+ for (f = t.entries(); !(n = f.next()).done; )
224
+ if (!r(n.value[1], o.get(n.value[0]))) return !1;
225
225
  return !0;
226
226
  }
227
- if (s && t instanceof Set && a instanceof Set) {
228
- if (t.size !== a.size) return !1;
229
- for (c = t.entries(); !(n = c.next()).done; )
230
- if (!a.has(n.value[0])) return !1;
227
+ if (s && t instanceof Set && o instanceof Set) {
228
+ if (t.size !== o.size) return !1;
229
+ for (f = t.entries(); !(n = f.next()).done; )
230
+ if (!o.has(n.value[0])) return !1;
231
231
  return !0;
232
232
  }
233
- if (i && ArrayBuffer.isView(t) && ArrayBuffer.isView(a)) {
234
- if (u = t.length, u != a.length) return !1;
233
+ if (i && ArrayBuffer.isView(t) && ArrayBuffer.isView(o)) {
234
+ if (u = t.length, u != o.length) return !1;
235
235
  for (n = u; n-- !== 0; )
236
- if (t[n] !== a[n]) return !1;
236
+ if (t[n] !== o[n]) return !1;
237
237
  return !0;
238
238
  }
239
- if (t.constructor === RegExp) return t.source === a.source && t.flags === a.flags;
240
- if (t.valueOf !== Object.prototype.valueOf && typeof t.valueOf == "function" && typeof a.valueOf == "function") return t.valueOf() === a.valueOf();
241
- if (t.toString !== Object.prototype.toString && typeof t.toString == "function" && typeof a.toString == "function") return t.toString() === a.toString();
242
- if (f = Object.keys(t), u = f.length, u !== Object.keys(a).length) return !1;
239
+ if (t.constructor === RegExp) return t.source === o.source && t.flags === o.flags;
240
+ if (t.valueOf !== Object.prototype.valueOf && typeof t.valueOf == "function" && typeof o.valueOf == "function") return t.valueOf() === o.valueOf();
241
+ if (t.toString !== Object.prototype.toString && typeof t.toString == "function" && typeof o.toString == "function") return t.toString() === o.toString();
242
+ if (c = Object.keys(t), u = c.length, u !== Object.keys(o).length) return !1;
243
243
  for (n = u; n-- !== 0; )
244
- if (!Object.prototype.hasOwnProperty.call(a, f[n])) return !1;
245
- if (o && t instanceof Element) return !1;
244
+ if (!Object.prototype.hasOwnProperty.call(o, c[n])) return !1;
245
+ if (a && t instanceof Element) return !1;
246
246
  for (n = u; n-- !== 0; )
247
- if (!((f[n] === "_owner" || f[n] === "__v" || f[n] === "__o") && t.$$typeof) && !r(t[f[n]], a[f[n]]))
247
+ if (!((c[n] === "_owner" || c[n] === "__v" || c[n] === "__o") && t.$$typeof) && !r(t[c[n]], o[c[n]]))
248
248
  return !1;
249
249
  return !0;
250
250
  }
251
- return t !== t && a !== a;
251
+ return t !== t && o !== o;
252
252
  }
253
- return S = function(t, a) {
253
+ return S = function(t, o) {
254
254
  try {
255
- return r(t, a);
255
+ return r(t, o);
256
256
  } catch (u) {
257
257
  if ((u.message || "").match(/stack|recursion/i))
258
258
  return console.warn("react-fast-compare cannot handle circular refs"), !1;
@@ -261,51 +261,51 @@ function Q() {
261
261
  }, S;
262
262
  }
263
263
  var X = Q();
264
- const Y = /* @__PURE__ */ J(X), x = new d(), h = new G(x), Z = (o, e) => x.createStatic({ initialValue: o }, o, e);
265
- function E(o, e, s) {
264
+ const Y = /* @__PURE__ */ J(X), x = new d(), h = new G(x), Z = (a, e) => x.createStatic({ initialValue: a }, a, e);
265
+ function E(a, e, s) {
266
266
  let i, r = s;
267
- if (typeof o != "string") {
268
- const { key: c, prefix: l } = o;
269
- i = c, r = l;
267
+ if (typeof a != "string") {
268
+ const { key: f, prefix: l } = a;
269
+ i = f, r = l;
270
270
  } else
271
- i = b(o);
272
- const { prefix: t } = H(r), a = j(void 0), u = D(() => (c) => (x.init(i, t, void 0), x.addListener(i, t, c), () => {
273
- x.removeListener(i, t, c);
271
+ i = b(a);
272
+ const { prefix: t } = H(r), o = j(void 0), u = D(() => (f) => (x.init(i, t, void 0), x.addListener(i, t, f), () => {
273
+ x.removeListener(i, t, f);
274
274
  }), [i, t]), n = D(() => () => {
275
- const c = x.get(i, t)?.value, l = e(c);
276
- return Y(a.current, l) ? a.current : (a.current = l, l);
277
- }, [i, t, e]), f = W(u, n);
278
- return x.useEffect(i, t), f;
275
+ const f = x.get(i, t)?.value, l = e(f);
276
+ return Y(o.current, l) ? o.current : (o.current = l, l);
277
+ }, [i, t, e]), c = W(u, n);
278
+ return x.useEffect(i, t), c;
279
279
  }
280
- const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
281
- initialState: o = "loading",
280
+ const m = (a, e) => Array.isArray(e) && e.includes(a) || a === e, ee = ({
281
+ initialState: a = "loading",
282
282
  defaultFallback: e,
283
283
  defaultErrorFallback: s,
284
284
  defaultRetry: i
285
285
  } = {}) => {
286
286
  const r = Z({
287
- state: o,
287
+ state: a,
288
288
  retry: i
289
289
  }), t = (n) => {
290
- let f = u.get();
291
- return new Promise((c, l) => {
290
+ let c = u.get();
291
+ return new Promise((f, l) => {
292
292
  if (m(u.get(), n)) {
293
- c();
293
+ f();
294
294
  return;
295
295
  }
296
296
  const g = h.subscribe(r, () => {
297
- m(u.get(), n) ? (g(), c()) : f !== u.get() && l();
297
+ m(u.get(), n) ? (g(), f()) : c !== u.get() && l();
298
298
  });
299
299
  });
300
- }, a = function(n, f = () => []) {
301
- return (c) => E(r, (g) => m(g.state, n)) ? /* @__PURE__ */ p(w, { children: L(c.children, ...f()) }) : null;
300
+ }, o = function(n, c = () => []) {
301
+ return (f) => E(r, (g) => m(g.state, n)) ? /* @__PURE__ */ p(w, { children: L(f.children, ...c()) }) : null;
302
302
  }, u = {
303
303
  //hooks
304
304
  use: () => E(r, (n) => n.state),
305
305
  useIsLoading: () => E(r, (n) => n.state) === "loading",
306
306
  useErrors: () => E(r, (n) => n.errors),
307
307
  useContext: () => E(r, (n) => n.context),
308
- set: (n) => h.update(r, (f) => ({ ...f, state: n })),
308
+ set: (n) => h.update(r, (c) => ({ ...c, state: n })),
309
309
  retry: (n) => (h.get(r).retry ?? n)?.(),
310
310
  // getters
311
311
  isLoading: () => h.get(r).state === "loading",
@@ -320,25 +320,25 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
320
320
  getContext: () => h.get(r).context,
321
321
  // setters
322
322
  reset: () => h.set(r, {
323
- state: o
323
+ state: a
324
324
  }),
325
325
  addError: (n) => {
326
- h.update(r, (f) => ({
327
- ...f,
328
- errors: [...f.errors ?? [], n]
326
+ h.update(r, (c) => ({
327
+ ...c,
328
+ errors: [...c.errors ?? [], n]
329
329
  }));
330
330
  },
331
331
  setContext: (n) => {
332
- h.update(r, (f) => ({ ...f, context: n }));
332
+ h.update(r, (c) => ({ ...c, context: n }));
333
333
  },
334
334
  setErrors: (n) => {
335
- h.update(r, (f) => ({ ...f, errors: n }));
335
+ h.update(r, (c) => ({ ...c, errors: n }));
336
336
  },
337
337
  clearErrors: () => {
338
338
  h.update(r, (n) => ({ ...n, errors: void 0 }));
339
339
  },
340
340
  // set a retry function
341
- setRetry: (n) => h.update(r, (f) => ({ ...f, retry: n })),
341
+ setRetry: (n) => h.update(r, (c) => ({ ...c, retry: n })),
342
342
  // promises
343
343
  whenLoaded: () => t("success").catch((n) => {
344
344
  throw new Error("loading failed", { cause: n });
@@ -346,24 +346,24 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
346
346
  whenFinished: () => t(["success", "error"]),
347
347
  // wrap a promise
348
348
  wrap(n) {
349
- const f = () => new Promise((c, l) => {
349
+ const c = () => new Promise((f, l) => {
350
350
  u.clearErrors(), u.set("loading"), n().then((g) => {
351
- u.set("success"), c(g);
351
+ u.set("success"), f(g);
352
352
  }).catch((g) => {
353
353
  u.set("error"), g && u.setErrors([g]), l(g);
354
354
  });
355
355
  });
356
- return u.setRetry(f), f();
356
+ return u.setRetry(c), c();
357
357
  },
358
358
  wrapWithControl(n) {
359
- const f = {
359
+ const c = {
360
360
  thens: [],
361
361
  catchs: [],
362
362
  finallies: []
363
- }, c = {
364
- then: (...l) => (f.thens.push(l), c),
365
- catch_: (...l) => (f.catchs.push(l), c),
366
- finally_: (...l) => (f.finallies.push(l), c),
363
+ }, f = {
364
+ then: (...l) => (c.thens.push(l), f),
365
+ catch_: (...l) => (c.catchs.push(l), f),
366
+ finally_: (...l) => (c.finallies.push(l), f),
367
367
  start: () => new Promise((l, g) => {
368
368
  u.clearErrors(), u.set("loading");
369
369
  let v = n();
@@ -372,29 +372,29 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
372
372
  }), v.catch((y) => {
373
373
  u.set("error"), y && u.setErrors([y]);
374
374
  });
375
- for (const y of f.thens)
375
+ for (const y of c.thens)
376
376
  v.then(...y);
377
- for (const y of f.catchs)
377
+ for (const y of c.catchs)
378
378
  v.catch(...y);
379
- for (const y of f.finallies)
379
+ for (const y of c.finallies)
380
380
  v.finally(...y);
381
381
  v.then(l, g);
382
382
  })
383
383
  };
384
- return u.setRetry(c.start), c;
384
+ return u.setRetry(f.start), f;
385
385
  },
386
386
  // components
387
387
  // - show when
388
- ShowWhenLoaded: ({ children: n, fallback: f, errorFallback: c }) => {
388
+ ShowWhenLoaded: ({ children: n, fallback: c, errorFallback: f }) => {
389
389
  const l = E(r, (g) => g.state);
390
- return l === "idle" ? null : (f = P(e, f), c = B(s, c === !0 ? void 0 : c), l === "loading" ? /* @__PURE__ */ p(w, { children: f }) : l === "error" ? /* @__PURE__ */ p(w, { children: L(c, u.getErrors() ?? [], () => u.retry()) }) : /* @__PURE__ */ p(w, { children: n }));
390
+ return l === "idle" ? null : (c = P(e, c), f = B(s, f === !0 ? void 0 : f), l === "loading" ? /* @__PURE__ */ p(w, { children: c }) : l === "error" ? /* @__PURE__ */ p(w, { children: L(f, u.getErrors() ?? [], () => u.retry()) }) : /* @__PURE__ */ p(w, { children: n }));
391
391
  },
392
- ShowWhileLoading: a("loading"),
393
- ShowWhenError: a("error", () => [u.getErrors() ?? [], () => u.retry()]),
394
- ShowWhenFinish: a(["error", "success"], () => [u.getErrors(), () => u.retry()]),
395
- ShowWhen: ({ children: n, state: f }) => {
396
- const c = a(f);
397
- return /* @__PURE__ */ p(c, { children: n });
392
+ ShowWhileLoading: o("loading"),
393
+ ShowWhenError: o("error", () => [u.getErrors() ?? [], () => u.retry()]),
394
+ ShowWhenFinish: o(["error", "success"], () => [u.getErrors(), () => u.retry()]),
395
+ ShowWhen: ({ children: n, state: c }) => {
396
+ const f = o(c);
397
+ return /* @__PURE__ */ p(f, { children: n });
398
398
  },
399
399
  destroy: () => {
400
400
  h.clear(r);
@@ -408,38 +408,38 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
408
408
  __get__id: () => `loading_id//${r.prefix}/${r.key}`
409
409
  };
410
410
  return u;
411
- }, oe = (o = {}) => {
412
- const e = j(ee(typeof o == "string" ? {
413
- initialState: o
414
- } : o));
415
- return O(() => () => {
416
- e.current.destroy();
411
+ }, te = (a) => ee(typeof a == "string" ? {
412
+ initialState: a
413
+ } : a), ue = (a = {}) => {
414
+ const e = j(void 0);
415
+ return e.current || (e.current = te(a)), O(() => () => {
416
+ e.current.destroy(), e.current = void 0;
417
417
  }, []), e.current;
418
- }, ue = ({ of: o, fallback: e, errorFallback: s, children: i }) => {
419
- const r = o.map((t) => t.use());
418
+ }, ce = ({ of: a, fallback: e, errorFallback: s, children: i }) => {
419
+ const r = a.map((t) => t.use());
420
420
  return e = P(void 0, e), r.some((t) => t === "loading") ? e : r.every((t) => t !== "loading") ? r.some((t) => t === "error") ? (s = B(void 0, s), /* @__PURE__ */ p(w, { children: L(
421
421
  s,
422
- o.reduce((t, a) => [...t, ...a.getErrors() ?? []], []),
422
+ a.reduce((t, o) => [...t, ...o.getErrors() ?? []], []),
423
423
  () => {
424
- o.map((t) => {
424
+ a.map((t) => {
425
425
  t.isError() && t.retry();
426
426
  });
427
427
  }
428
428
  ) })) : /* @__PURE__ */ p(w, { children: i }) : null;
429
429
  };
430
430
  export {
431
- ue as AllLoaded,
431
+ ce as AllLoaded,
432
432
  ee as createLoading,
433
433
  C as errorFallbacks,
434
434
  k as fallbacks,
435
435
  K as getDefaultErrorFallback,
436
436
  I as getDefaultFallback,
437
437
  L as reactNodeOrFunctionValue,
438
- ae as registerDefaultErrorFallback,
439
- ie as registerDefaultFallback,
440
- ne as registerErrorFallback,
441
- se as registerFallback,
438
+ oe as registerDefaultErrorFallback,
439
+ ae as registerDefaultFallback,
440
+ ie as registerErrorFallback,
441
+ ne as registerFallback,
442
442
  B as resolveErrorFallback,
443
443
  P as resolveLoadingFallback,
444
- oe as useCreateLocalLoading
444
+ ue as useCreateLocalLoading
445
445
  };
package/dist/main.min.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /*!
2
- * react-easy-loading v0.3.0
2
+ * react-easy-loading v0.3.1
3
3
  * (c) Hichem Taboukouyout
4
4
  * Released under the MIT License.
5
5
  * Github: github.com/HichemTab-tech/react-easy-loading
6
6
  */
7
7
 
8
- (function(d,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],u):(d=typeof globalThis<"u"?globalThis:d||self,u(d.ReactEasyLoading={},d.jsxRuntime,d.React))})(this,(function(d,u,v){"use strict";const D={},k={},W=(o,e)=>{D[o]=e},T=(o,e)=>{k[o]=e};let O=u.jsx("div",{children:"Loading..."}),C=(o,e)=>u.jsxs("div",{children:[u.jsx("h3",{children:"Errors:"}),u.jsx("ul",{children:o?.map((s,i)=>u.jsx("li",{children:s},i))}),u.jsx("button",{onClick:e,disabled:!e,children:"Retry"})]});const q=o=>{O=o},z=o=>{C=o},V=()=>O,j=()=>C,F=(o,e)=>P(V,D,o,e);function A(o,e){return P(j,k,o,e)}function P(o,e,s,i){return i?typeof i=="string"?e[i]??i:i:i===null?null:s?typeof s=="string"?e[s]??s:s:o()}function w(o,...e){return typeof o=="function"?o(...e):o}const I=(...o)=>{},_=o=>{if(!o)throw new Error("Value is empty");return o},K=()=>Math.random().toString(36).substring(2,15),N=v.createContext(void 0),U=()=>v.useContext(N),M=[];class y{constructor(e=()=>null){this.defaultValue=e}data=new Map;addListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&t.listeners.push(i)}removeListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.listeners=t.listeners.filter(a=>a!==i))}callListeners(e,s){const i=y.prefix(e,s),r=this.data.get(i);r&&r.listeners.forEach(t=>t())}init(e,s,i,r=!1){const t=y.prefix(e,s);this.data.has(t)||this.data.set(t,{value:i,isStatic:r||void 0,listeners:[]})}createStatic(e,s,i){const r=i??"_global",t={key:K(),prefix:r,...e};return M.push(t),this.init(t.key,t.prefix,s,!0),this.defaultValue=()=>s,t}initStatic(e){const{key:s,prefix:i}=e;this.init(s,i,this.defaultValue(),!0)}clearAll(e=!1,s=!1){this.data.forEach((i,r)=>{const[t,a]=y.extractPrefix(r);this.clear(a,t,e,s)})}clear(e,s,i=!1,r=!1){const t=y.prefix(e,s);i||this.callListeners(e,s);const a=this.data.get(t);if(a&&(this.data.delete(t),a.isStatic&&!r)){const l=M.find(n=>n.key===e&&n.prefix===s);l&&this.initStatic(l)}}get(e,s){let i=this.has(e,s);if(i)return this.data.get(i)}setValue(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.value=i,this.data.set(r,t))}has(e,s){return this.data.has(y.prefix(e,s))?y.prefix(e,s):this.data.has(y.prefix(e,"_global"))?y.prefix(e,"_global"):void 0}static prefix(e,s){if(e.includes("//"))throw new Error("key cannot contain '//'");return`${s}//${e}`}static extractPrefix(e){const s=e.split("//");return[s[0],s.slice(1).join("//")]}useEffect(e,s,i=null){v.useEffect(()=>()=>{i?.(),I(`[${y.prefix(e,s)}]`,"unmount effect");const r=this.get(e,s);r&&r.listeners?.length===0&&this.clear(e,s)},[e,s])}}class G{constructor(e){this.sharedData=e}_get(e,s){let i,r=s;if(typeof e!="string"){const{key:l,prefix:n}=e;i=l,r=n}else i=_(e);const t=r||"_global",a=this.sharedData.get(i,t);return a?{value:a.value,key:i,prefix:t}:{key:i,prefix:t,value:void 0}}get(e,s){return this._get(e,s).value}set(e,s,i){let r,t=i;if(typeof e!="string"){const{key:l,prefix:n}=e;r=l,t=n}else r=_(e);const a=t||"_global";this.sharedData.init(r,a,s),this.sharedData.setValue(r,a,s),this.sharedData.callListeners(r,a)}update(e,s,i){const r=this._get(e,i);if(r){const t=s(r.value);this.set(r.key,t,r.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(e){const s=e||"_global";this.sharedData.data.forEach((i,r)=>{const[t,a]=y.extractPrefix(r);t===s&&(this.sharedData.clear(a,t),this.sharedData.callListeners(a,t))})}resolve(e){const{key:s,prefix:i}=e;return this.get(s,i)}clear(e,s){let i,r;typeof e=="string"?(i=e,r=s||"_global"):(i=e.key,r=e.prefix),this.sharedData.clear(i,r)}has(e,s="_global"){const i=s||"_global";return!!this.sharedData.has(e,i)}getAll(){const e={};return this.sharedData.data.forEach((s,i)=>{const[r,t]=y.extractPrefix(i);e[r]=e[r]||{},e[r][t]=s.value}),e}subscribe(e,s,i){let r,t;return typeof e=="string"?(r=e,t=i||"_global"):(r=e.key,t=e.prefix),this.sharedData.addListener(r,t,s),()=>{this.sharedData.removeListener(r,t,s)}}}const H=o=>{const e=U();return{prefix:o??e?.scopeName??"_global"}};function J(o){return o&&o.__esModule&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o}var m,B;function Q(){if(B)return m;B=1;var o=typeof Element<"u",e=typeof Map=="function",s=typeof Set=="function",i=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function r(t,a){if(t===a)return!0;if(t&&a&&typeof t=="object"&&typeof a=="object"){if(t.constructor!==a.constructor)return!1;var l,n,f;if(Array.isArray(t)){if(l=t.length,l!=a.length)return!1;for(n=l;n--!==0;)if(!r(t[n],a[n]))return!1;return!0}var c;if(e&&t instanceof Map&&a instanceof Map){if(t.size!==a.size)return!1;for(c=t.entries();!(n=c.next()).done;)if(!a.has(n.value[0]))return!1;for(c=t.entries();!(n=c.next()).done;)if(!r(n.value[1],a.get(n.value[0])))return!1;return!0}if(s&&t instanceof Set&&a instanceof Set){if(t.size!==a.size)return!1;for(c=t.entries();!(n=c.next()).done;)if(!a.has(n.value[0]))return!1;return!0}if(i&&ArrayBuffer.isView(t)&&ArrayBuffer.isView(a)){if(l=t.length,l!=a.length)return!1;for(n=l;n--!==0;)if(t[n]!==a[n])return!1;return!0}if(t.constructor===RegExp)return t.source===a.source&&t.flags===a.flags;if(t.valueOf!==Object.prototype.valueOf&&typeof t.valueOf=="function"&&typeof a.valueOf=="function")return t.valueOf()===a.valueOf();if(t.toString!==Object.prototype.toString&&typeof t.toString=="function"&&typeof a.toString=="function")return t.toString()===a.toString();if(f=Object.keys(t),l=f.length,l!==Object.keys(a).length)return!1;for(n=l;n--!==0;)if(!Object.prototype.hasOwnProperty.call(a,f[n]))return!1;if(o&&t instanceof Element)return!1;for(n=l;n--!==0;)if(!((f[n]==="_owner"||f[n]==="__v"||f[n]==="__o")&&t.$$typeof)&&!r(t[f[n]],a[f[n]]))return!1;return!0}return t!==t&&a!==a}return m=function(t,a){try{return r(t,a)}catch(l){if((l.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw l}},m}var X=Q();const Y=J(X),x=new y,g=new G(x),Z=(o,e)=>x.createStatic({initialValue:o},o,e);function b(o,e,s){let i,r=s;if(typeof o!="string"){const{key:c,prefix:h}=o;i=c,r=h}else i=_(o);const{prefix:t}=H(r),a=v.useRef(void 0),l=v.useMemo(()=>c=>(x.init(i,t,void 0),x.addListener(i,t,c),()=>{x.removeListener(i,t,c)}),[i,t]),n=v.useMemo(()=>()=>{const c=x.get(i,t)?.value,h=e(c);return Y(a.current,h)?a.current:(a.current=h,h)},[i,t,e]),f=v.useSyncExternalStore(l,n);return x.useEffect(i,t),f}const S=(o,e)=>Array.isArray(e)&&e.includes(o)||o===e,$=({initialState:o="loading",defaultFallback:e,defaultErrorFallback:s,defaultRetry:i}={})=>{const r=Z({state:o,retry:i}),t=n=>{let f=l.get();return new Promise((c,h)=>{if(S(l.get(),n)){c();return}const p=g.subscribe(r,()=>{S(l.get(),n)?(p(),c()):f!==l.get()&&h()})})},a=function(n,f=()=>[]){return c=>b(r,p=>S(p.state,n))?u.jsx(u.Fragment,{children:w(c.children,...f())}):null},l={use:()=>b(r,n=>n.state),useIsLoading:()=>b(r,n=>n.state)==="loading",useErrors:()=>b(r,n=>n.errors),useContext:()=>b(r,n=>n.context),set:n=>g.update(r,f=>({...f,state:n})),retry:n=>(g.get(r).retry??n)?.(),isLoading:()=>g.get(r).state==="loading",isIdle:()=>g.get(r).state==="idle",isSuccess:()=>g.get(r).state==="success",isError:()=>g.get(r).state==="error",isFinished:()=>g.get(r).state==="success"||g.get(r).state==="error",is:n=>S(l.get(),n),hasErrors:()=>g.get(r).errors!==void 0,get:()=>g.get(r).state,getErrors:()=>g.get(r).errors,getContext:()=>g.get(r).context,reset:()=>g.set(r,{state:o}),addError:n=>{g.update(r,f=>({...f,errors:[...f.errors??[],n]}))},setContext:n=>{g.update(r,f=>({...f,context:n}))},setErrors:n=>{g.update(r,f=>({...f,errors:n}))},clearErrors:()=>{g.update(r,n=>({...n,errors:void 0}))},setRetry:n=>g.update(r,f=>({...f,retry:n})),whenLoaded:()=>t("success").catch(n=>{throw new Error("loading failed",{cause:n})}),whenFinished:()=>t(["success","error"]),wrap(n){const f=()=>new Promise((c,h)=>{l.clearErrors(),l.set("loading"),n().then(p=>{l.set("success"),c(p)}).catch(p=>{l.set("error"),p&&l.setErrors([p]),h(p)})});return l.setRetry(f),f()},wrapWithControl(n){const f={thens:[],catchs:[],finallies:[]},c={then:(...h)=>(f.thens.push(h),c),catch_:(...h)=>(f.catchs.push(h),c),finally_:(...h)=>(f.finallies.push(h),c),start:()=>new Promise((h,p)=>{l.clearErrors(),l.set("loading");let L=n();L.then(()=>{l.set("success")}),L.catch(E=>{l.set("error"),E&&l.setErrors([E])});for(const E of f.thens)L.then(...E);for(const E of f.catchs)L.catch(...E);for(const E of f.finallies)L.finally(...E);L.then(h,p)})};return l.setRetry(c.start),c},ShowWhenLoaded:({children:n,fallback:f,errorFallback:c})=>{const h=b(r,p=>p.state);return h==="idle"?null:(f=F(e,f),c=A(s,c===!0?void 0:c),h==="loading"?u.jsx(u.Fragment,{children:f}):h==="error"?u.jsx(u.Fragment,{children:w(c,l.getErrors()??[],()=>l.retry())}):u.jsx(u.Fragment,{children:n}))},ShowWhileLoading:a("loading"),ShowWhenError:a("error",()=>[l.getErrors()??[],()=>l.retry()]),ShowWhenFinish:a(["error","success"],()=>[l.getErrors(),()=>l.retry()]),ShowWhen:({children:n,state:f})=>{const c=a(f);return u.jsx(c,{children:n})},destroy:()=>{g.clear(r)},__get__id:()=>`loading_id//${r.prefix}/${r.key}`};return l},R=(o={})=>{const e=v.useRef($(typeof o=="string"?{initialState:o}:o));return v.useEffect(()=>()=>{e.current.destroy()},[]),e.current},ee=({of:o,fallback:e,errorFallback:s,children:i})=>{const r=o.map(t=>t.use());return e=F(void 0,e),r.some(t=>t==="loading")?e:r.every(t=>t!=="loading")?r.some(t=>t==="error")?(s=A(void 0,s),u.jsx(u.Fragment,{children:w(s,o.reduce((t,a)=>[...t,...a.getErrors()??[]],[]),()=>{o.map(t=>{t.isError()&&t.retry()})})})):u.jsx(u.Fragment,{children:i}):null};d.AllLoaded=ee,d.createLoading=$,d.errorFallbacks=k,d.fallbacks=D,d.getDefaultErrorFallback=j,d.getDefaultFallback=V,d.reactNodeOrFunctionValue=w,d.registerDefaultErrorFallback=z,d.registerDefaultFallback=q,d.registerErrorFallback=T,d.registerFallback=W,d.resolveErrorFallback=A,d.resolveLoadingFallback=F,d.useCreateLocalLoading=R,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
8
+ (function(d,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],u):(d=typeof globalThis<"u"?globalThis:d||self,u(d.ReactEasyLoading={},d.jsxRuntime,d.React))})(this,(function(d,u,v){"use strict";const D={},k={},W=(a,e)=>{D[a]=e},T=(a,e)=>{k[a]=e};let O=u.jsx("div",{children:"Loading..."}),C=(a,e)=>u.jsxs("div",{children:[u.jsx("h3",{children:"Errors:"}),u.jsx("ul",{children:a?.map((s,i)=>u.jsx("li",{children:s},i))}),u.jsx("button",{onClick:e,disabled:!e,children:"Retry"})]});const q=a=>{O=a},z=a=>{C=a},V=()=>O,j=()=>C,F=(a,e)=>P(V,D,a,e);function A(a,e){return P(j,k,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 I=(...a)=>{},_=a=>{if(!a)throw new Error("Value is empty");return a},K=()=>Math.random().toString(36).substring(2,15),N=v.createContext(void 0),U=()=>v.useContext(N),M=[];class y{constructor(e=()=>null){this.defaultValue=e}data=new Map;addListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&t.listeners.push(i)}removeListener(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.listeners=t.listeners.filter(o=>o!==i))}callListeners(e,s){const i=y.prefix(e,s),r=this.data.get(i);r&&r.listeners.forEach(t=>t())}init(e,s,i,r=!1){const t=y.prefix(e,s);this.data.has(t)||this.data.set(t,{value:i,isStatic:r||void 0,listeners:[]})}createStatic(e,s,i){const r=i??"_global",t={key:K(),prefix:r,...e};return M.push(t),this.init(t.key,t.prefix,s,!0),this.defaultValue=()=>s,t}initStatic(e){const{key:s,prefix:i}=e;this.init(s,i,this.defaultValue(),!0)}clearAll(e=!1,s=!1){this.data.forEach((i,r)=>{const[t,o]=y.extractPrefix(r);this.clear(o,t,e,s)})}clear(e,s,i=!1,r=!1){const t=y.prefix(e,s);i||this.callListeners(e,s);const o=this.data.get(t);if(o&&(this.data.delete(t),o.isStatic&&!r)){const l=M.find(n=>n.key===e&&n.prefix===s);l&&this.initStatic(l)}}get(e,s){let i=this.has(e,s);if(i)return this.data.get(i)}setValue(e,s,i){const r=y.prefix(e,s),t=this.data.get(r);t&&(t.value=i,this.data.set(r,t))}has(e,s){return this.data.has(y.prefix(e,s))?y.prefix(e,s):this.data.has(y.prefix(e,"_global"))?y.prefix(e,"_global"):void 0}static prefix(e,s){if(e.includes("//"))throw new Error("key cannot contain '//'");return`${s}//${e}`}static extractPrefix(e){const s=e.split("//");return[s[0],s.slice(1).join("//")]}useEffect(e,s,i=null){v.useEffect(()=>()=>{i?.(),I(`[${y.prefix(e,s)}]`,"unmount effect");const r=this.get(e,s);r&&r.listeners?.length===0&&this.clear(e,s)},[e,s])}}class G{constructor(e){this.sharedData=e}_get(e,s){let i,r=s;if(typeof e!="string"){const{key:l,prefix:n}=e;i=l,r=n}else i=_(e);const t=r||"_global",o=this.sharedData.get(i,t);return o?{value:o.value,key:i,prefix:t}:{key:i,prefix:t,value:void 0}}get(e,s){return this._get(e,s).value}set(e,s,i){let r,t=i;if(typeof e!="string"){const{key:l,prefix:n}=e;r=l,t=n}else r=_(e);const o=t||"_global";this.sharedData.init(r,o,s),this.sharedData.setValue(r,o,s),this.sharedData.callListeners(r,o)}update(e,s,i){const r=this._get(e,i);if(r){const t=s(r.value);this.set(r.key,t,r.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(e){const s=e||"_global";this.sharedData.data.forEach((i,r)=>{const[t,o]=y.extractPrefix(r);t===s&&(this.sharedData.clear(o,t),this.sharedData.callListeners(o,t))})}resolve(e){const{key:s,prefix:i}=e;return this.get(s,i)}clear(e,s){let i,r;typeof e=="string"?(i=e,r=s||"_global"):(i=e.key,r=e.prefix),this.sharedData.clear(i,r)}has(e,s="_global"){const i=s||"_global";return!!this.sharedData.has(e,i)}getAll(){const e={};return this.sharedData.data.forEach((s,i)=>{const[r,t]=y.extractPrefix(i);e[r]=e[r]||{},e[r][t]=s.value}),e}subscribe(e,s,i){let r,t;return typeof e=="string"?(r=e,t=i||"_global"):(r=e.key,t=e.prefix),this.sharedData.addListener(r,t,s),()=>{this.sharedData.removeListener(r,t,s)}}}const H=a=>{const e=U();return{prefix:a??e?.scopeName??"_global"}};function J(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var m,B;function Q(){if(B)return m;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 l,n,c;if(Array.isArray(t)){if(l=t.length,l!=o.length)return!1;for(n=l;n--!==0;)if(!r(t[n],o[n]))return!1;return!0}var f;if(e&&t instanceof Map&&o instanceof Map){if(t.size!==o.size)return!1;for(f=t.entries();!(n=f.next()).done;)if(!o.has(n.value[0]))return!1;for(f=t.entries();!(n=f.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(f=t.entries();!(n=f.next()).done;)if(!o.has(n.value[0]))return!1;return!0}if(i&&ArrayBuffer.isView(t)&&ArrayBuffer.isView(o)){if(l=t.length,l!=o.length)return!1;for(n=l;n--!==0;)if(t[n]!==o[n])return!1;return!0}if(t.constructor===RegExp)return t.source===o.source&&t.flags===o.flags;if(t.valueOf!==Object.prototype.valueOf&&typeof t.valueOf=="function"&&typeof o.valueOf=="function")return t.valueOf()===o.valueOf();if(t.toString!==Object.prototype.toString&&typeof t.toString=="function"&&typeof o.toString=="function")return t.toString()===o.toString();if(c=Object.keys(t),l=c.length,l!==Object.keys(o).length)return!1;for(n=l;n--!==0;)if(!Object.prototype.hasOwnProperty.call(o,c[n]))return!1;if(a&&t instanceof Element)return!1;for(n=l;n--!==0;)if(!((c[n]==="_owner"||c[n]==="__v"||c[n]==="__o")&&t.$$typeof)&&!r(t[c[n]],o[c[n]]))return!1;return!0}return t!==t&&o!==o}return m=function(t,o){try{return r(t,o)}catch(l){if((l.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw l}},m}var X=Q();const Y=J(X),x=new y,g=new G(x),Z=(a,e)=>x.createStatic({initialValue:a},a,e);function b(a,e,s){let i,r=s;if(typeof a!="string"){const{key:f,prefix:h}=a;i=f,r=h}else i=_(a);const{prefix:t}=H(r),o=v.useRef(void 0),l=v.useMemo(()=>f=>(x.init(i,t,void 0),x.addListener(i,t,f),()=>{x.removeListener(i,t,f)}),[i,t]),n=v.useMemo(()=>()=>{const f=x.get(i,t)?.value,h=e(f);return Y(o.current,h)?o.current:(o.current=h,h)},[i,t,e]),c=v.useSyncExternalStore(l,n);return x.useEffect(i,t),c}const S=(a,e)=>Array.isArray(e)&&e.includes(a)||a===e,$=({initialState:a="loading",defaultFallback:e,defaultErrorFallback:s,defaultRetry:i}={})=>{const r=Z({state:a,retry:i}),t=n=>{let c=l.get();return new Promise((f,h)=>{if(S(l.get(),n)){f();return}const p=g.subscribe(r,()=>{S(l.get(),n)?(p(),f()):c!==l.get()&&h()})})},o=function(n,c=()=>[]){return f=>b(r,p=>S(p.state,n))?u.jsx(u.Fragment,{children:w(f.children,...c())}):null},l={use:()=>b(r,n=>n.state),useIsLoading:()=>b(r,n=>n.state)==="loading",useErrors:()=>b(r,n=>n.errors),useContext:()=>b(r,n=>n.context),set:n=>g.update(r,c=>({...c,state:n})),retry:n=>(g.get(r).retry??n)?.(),isLoading:()=>g.get(r).state==="loading",isIdle:()=>g.get(r).state==="idle",isSuccess:()=>g.get(r).state==="success",isError:()=>g.get(r).state==="error",isFinished:()=>g.get(r).state==="success"||g.get(r).state==="error",is:n=>S(l.get(),n),hasErrors:()=>g.get(r).errors!==void 0,get:()=>g.get(r).state,getErrors:()=>g.get(r).errors,getContext:()=>g.get(r).context,reset:()=>g.set(r,{state:a}),addError:n=>{g.update(r,c=>({...c,errors:[...c.errors??[],n]}))},setContext:n=>{g.update(r,c=>({...c,context:n}))},setErrors:n=>{g.update(r,c=>({...c,errors:n}))},clearErrors:()=>{g.update(r,n=>({...n,errors:void 0}))},setRetry:n=>g.update(r,c=>({...c,retry:n})),whenLoaded:()=>t("success").catch(n=>{throw new Error("loading failed",{cause:n})}),whenFinished:()=>t(["success","error"]),wrap(n){const c=()=>new Promise((f,h)=>{l.clearErrors(),l.set("loading"),n().then(p=>{l.set("success"),f(p)}).catch(p=>{l.set("error"),p&&l.setErrors([p]),h(p)})});return l.setRetry(c),c()},wrapWithControl(n){const c={thens:[],catchs:[],finallies:[]},f={then:(...h)=>(c.thens.push(h),f),catch_:(...h)=>(c.catchs.push(h),f),finally_:(...h)=>(c.finallies.push(h),f),start:()=>new Promise((h,p)=>{l.clearErrors(),l.set("loading");let L=n();L.then(()=>{l.set("success")}),L.catch(E=>{l.set("error"),E&&l.setErrors([E])});for(const E of c.thens)L.then(...E);for(const E of c.catchs)L.catch(...E);for(const E of c.finallies)L.finally(...E);L.then(h,p)})};return l.setRetry(f.start),f},ShowWhenLoaded:({children:n,fallback:c,errorFallback:f})=>{const h=b(r,p=>p.state);return h==="idle"?null:(c=F(e,c),f=A(s,f===!0?void 0:f),h==="loading"?u.jsx(u.Fragment,{children:c}):h==="error"?u.jsx(u.Fragment,{children:w(f,l.getErrors()??[],()=>l.retry())}):u.jsx(u.Fragment,{children:n}))},ShowWhileLoading:o("loading"),ShowWhenError:o("error",()=>[l.getErrors()??[],()=>l.retry()]),ShowWhenFinish:o(["error","success"],()=>[l.getErrors(),()=>l.retry()]),ShowWhen:({children:n,state:c})=>{const f=o(c);return u.jsx(f,{children:n})},destroy:()=>{g.clear(r)},__get__id:()=>`loading_id//${r.prefix}/${r.key}`};return l},R=a=>$(typeof a=="string"?{initialState:a}:a),ee=(a={})=>{const e=v.useRef(void 0);return e.current||(e.current=R(a)),v.useEffect(()=>()=>{e.current.destroy(),e.current=void 0},[]),e.current},te=({of:a,fallback:e,errorFallback:s,children:i})=>{const r=a.map(t=>t.use());return e=F(void 0,e),r.some(t=>t==="loading")?e:r.every(t=>t!=="loading")?r.some(t=>t==="error")?(s=A(void 0,s),u.jsx(u.Fragment,{children:w(s,a.reduce((t,o)=>[...t,...o.getErrors()??[]],[]),()=>{a.map(t=>{t.isError()&&t.retry()})})})):u.jsx(u.Fragment,{children:i}):null};d.AllLoaded=te,d.createLoading=$,d.errorFallbacks=k,d.fallbacks=D,d.getDefaultErrorFallback=j,d.getDefaultFallback=V,d.reactNodeOrFunctionValue=w,d.registerDefaultErrorFallback=z,d.registerDefaultFallback=q,d.registerErrorFallback=T,d.registerFallback=W,d.resolveErrorFallback=A,d.resolveLoadingFallback=F,d.useCreateLocalLoading=ee,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.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "keywords": [