react-easy-loading 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -117,6 +117,8 @@ import { useCreateLocalLoading } from 'react-easy-loading';
117
117
 
118
118
  function MyComponent() {
119
119
  const loading = useCreateLocalLoading();
120
+ // You can pass options or just the initial state (by default it's loading)
121
+ //const loading = useCreateLocalLoading('idle');
120
122
 
121
123
  // Use it just like a regular loading instance
122
124
  const isLoading = loading.useIsLoading();
@@ -1,3 +1,3 @@
1
1
  import { CreateLoadingOptions, Loading } from './create-loading';
2
- export declare const useCreateLocalLoading: <Errors extends unknown[] = string[], Context extends Record<string, unknown> = any>(createLoadingOptions?: CreateLoadingOptions) => Loading<Errors, Context>;
2
+ export declare const useCreateLocalLoading: <Errors extends unknown[] = string[], Context extends Record<string, unknown> = any>(createLoadingOptions?: CreateLoadingOptions | CreateLoadingOptions["initialState"]) => Loading<Errors, Context>;
3
3
  //# sourceMappingURL=create-local-loading.d.ts.map
@@ -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,oBAAyB,KAAG,OAAO,CAAC,MAAM,EAAE,OAAO,CAUlM,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;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"}
package/dist/main.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * react-easy-loading v0.2.0
2
+ * react-easy-loading v0.3.0
3
3
  * (c) Hichem Taboukouyout
4
4
  * Released under the MIT License.
5
5
  * Github: github.com/HichemTab-tech/react-easy-loading
@@ -28,7 +28,7 @@ function B(o, e) {
28
28
  function M(o, e, s, i) {
29
29
  return i ? typeof i == "string" ? e[i] ?? i : i : i === null ? null : s ? typeof s == "string" ? e[s] ?? s : s : o();
30
30
  }
31
- function S(o, ...e) {
31
+ function L(o, ...e) {
32
32
  return typeof o == "function" ? o(...e) : o;
33
33
  }
34
34
  const U = (...o) => {
@@ -199,34 +199,34 @@ const H = (o) => {
199
199
  function J(o) {
200
200
  return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
201
201
  }
202
- var L, _;
202
+ var S, _;
203
203
  function Q() {
204
- if (_) return L;
204
+ if (_) return S;
205
205
  _ = 1;
206
206
  var o = typeof Element < "u", e = typeof Map == "function", s = typeof Set == "function", i = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
207
207
  function r(t, a) {
208
208
  if (t === a) return !0;
209
209
  if (t && a && typeof t == "object" && typeof a == "object") {
210
210
  if (t.constructor !== a.constructor) return !1;
211
- var u, n, c;
211
+ var u, n, f;
212
212
  if (Array.isArray(t)) {
213
213
  if (u = t.length, u != a.length) return !1;
214
214
  for (n = u; n-- !== 0; )
215
215
  if (!r(t[n], a[n])) return !1;
216
216
  return !0;
217
217
  }
218
- var f;
218
+ var c;
219
219
  if (e && t instanceof Map && a instanceof Map) {
220
220
  if (t.size !== a.size) return !1;
221
- for (f = t.entries(); !(n = f.next()).done; )
221
+ for (c = t.entries(); !(n = c.next()).done; )
222
222
  if (!a.has(n.value[0])) return !1;
223
- for (f = t.entries(); !(n = f.next()).done; )
223
+ for (c = t.entries(); !(n = c.next()).done; )
224
224
  if (!r(n.value[1], a.get(n.value[0]))) return !1;
225
225
  return !0;
226
226
  }
227
227
  if (s && t instanceof Set && a instanceof Set) {
228
228
  if (t.size !== a.size) return !1;
229
- for (f = t.entries(); !(n = f.next()).done; )
229
+ for (c = t.entries(); !(n = c.next()).done; )
230
230
  if (!a.has(n.value[0])) return !1;
231
231
  return !0;
232
232
  }
@@ -239,18 +239,18 @@ function Q() {
239
239
  if (t.constructor === RegExp) return t.source === a.source && t.flags === a.flags;
240
240
  if (t.valueOf !== Object.prototype.valueOf && typeof t.valueOf == "function" && typeof a.valueOf == "function") return t.valueOf() === a.valueOf();
241
241
  if (t.toString !== Object.prototype.toString && typeof t.toString == "function" && typeof a.toString == "function") return t.toString() === a.toString();
242
- if (c = Object.keys(t), u = c.length, u !== Object.keys(a).length) return !1;
242
+ if (f = Object.keys(t), u = f.length, u !== Object.keys(a).length) return !1;
243
243
  for (n = u; n-- !== 0; )
244
- if (!Object.prototype.hasOwnProperty.call(a, c[n])) return !1;
244
+ if (!Object.prototype.hasOwnProperty.call(a, f[n])) return !1;
245
245
  if (o && t instanceof Element) return !1;
246
246
  for (n = u; n-- !== 0; )
247
- if (!((c[n] === "_owner" || c[n] === "__v" || c[n] === "__o") && t.$$typeof) && !r(t[c[n]], a[c[n]]))
247
+ if (!((f[n] === "_owner" || f[n] === "__v" || f[n] === "__o") && t.$$typeof) && !r(t[f[n]], a[f[n]]))
248
248
  return !1;
249
249
  return !0;
250
250
  }
251
251
  return t !== t && a !== a;
252
252
  }
253
- return L = function(t, a) {
253
+ return S = function(t, a) {
254
254
  try {
255
255
  return r(t, a);
256
256
  } catch (u) {
@@ -258,24 +258,24 @@ function Q() {
258
258
  return console.warn("react-fast-compare cannot handle circular refs"), !1;
259
259
  throw u;
260
260
  }
261
- }, L;
261
+ }, S;
262
262
  }
263
263
  var X = Q();
264
264
  const Y = /* @__PURE__ */ J(X), x = new d(), h = new G(x), Z = (o, e) => x.createStatic({ initialValue: o }, o, e);
265
265
  function E(o, e, s) {
266
266
  let i, r = s;
267
267
  if (typeof o != "string") {
268
- const { key: f, prefix: l } = o;
269
- i = f, r = l;
268
+ const { key: c, prefix: l } = o;
269
+ i = c, r = l;
270
270
  } else
271
271
  i = b(o);
272
- const { prefix: t } = H(r), a = j(void 0), u = D(() => (f) => (x.init(i, t, void 0), x.addListener(i, t, f), () => {
273
- x.removeListener(i, t, f);
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);
274
274
  }), [i, t]), n = D(() => () => {
275
- const f = x.get(i, t)?.value, l = e(f);
275
+ const c = x.get(i, t)?.value, l = e(c);
276
276
  return Y(a.current, l) ? a.current : (a.current = l, l);
277
- }, [i, t, e]), c = W(u, n);
278
- return x.useEffect(i, t), c;
277
+ }, [i, t, e]), f = W(u, n);
278
+ return x.useEffect(i, t), f;
279
279
  }
280
280
  const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
281
281
  initialState: o = "loading",
@@ -287,25 +287,25 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
287
287
  state: o,
288
288
  retry: i
289
289
  }), t = (n) => {
290
- let c = u.get();
291
- return new Promise((f, l) => {
290
+ let f = u.get();
291
+ return new Promise((c, l) => {
292
292
  if (m(u.get(), n)) {
293
- f();
293
+ c();
294
294
  return;
295
295
  }
296
296
  const g = h.subscribe(r, () => {
297
- m(u.get(), n) ? (g(), f()) : c !== u.get() && l();
297
+ m(u.get(), n) ? (g(), c()) : f !== u.get() && l();
298
298
  });
299
299
  });
300
- }, a = function(n, c = () => []) {
301
- return (f) => E(r, (g) => m(g.state, n)) ? /* @__PURE__ */ p(w, { children: S(f.children, ...c()) }) : null;
300
+ }, a = function(n, f = () => []) {
301
+ return (c) => E(r, (g) => m(g.state, n)) ? /* @__PURE__ */ p(w, { children: L(c.children, ...f()) }) : 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, (c) => ({ ...c, state: n })),
308
+ set: (n) => h.update(r, (f) => ({ ...f, state: n })),
309
309
  retry: (n) => (h.get(r).retry ?? n)?.(),
310
310
  // getters
311
311
  isLoading: () => h.get(r).state === "loading",
@@ -323,22 +323,22 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
323
323
  state: o
324
324
  }),
325
325
  addError: (n) => {
326
- h.update(r, (c) => ({
327
- ...c,
328
- errors: [...c.errors ?? [], n]
326
+ h.update(r, (f) => ({
327
+ ...f,
328
+ errors: [...f.errors ?? [], n]
329
329
  }));
330
330
  },
331
331
  setContext: (n) => {
332
- h.update(r, (c) => ({ ...c, context: n }));
332
+ h.update(r, (f) => ({ ...f, context: n }));
333
333
  },
334
334
  setErrors: (n) => {
335
- h.update(r, (c) => ({ ...c, errors: n }));
335
+ h.update(r, (f) => ({ ...f, 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, (c) => ({ ...c, retry: n })),
341
+ setRetry: (n) => h.update(r, (f) => ({ ...f, 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 c = () => new Promise((f, l) => {
349
+ const f = () => new Promise((c, l) => {
350
350
  u.clearErrors(), u.set("loading"), n().then((g) => {
351
- u.set("success"), f(g);
351
+ u.set("success"), c(g);
352
352
  }).catch((g) => {
353
353
  u.set("error"), g && u.setErrors([g]), l(g);
354
354
  });
355
355
  });
356
- return u.setRetry(c), c();
356
+ return u.setRetry(f), f();
357
357
  },
358
358
  wrapWithControl(n) {
359
- const c = {
359
+ const f = {
360
360
  thens: [],
361
361
  catchs: [],
362
362
  finallies: []
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),
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),
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 c.thens)
375
+ for (const y of f.thens)
376
376
  v.then(...y);
377
- for (const y of c.catchs)
377
+ for (const y of f.catchs)
378
378
  v.catch(...y);
379
- for (const y of c.finallies)
379
+ for (const y of f.finallies)
380
380
  v.finally(...y);
381
381
  v.then(l, g);
382
382
  })
383
383
  };
384
- return u.setRetry(f.start), f;
384
+ return u.setRetry(c.start), c;
385
385
  },
386
386
  // components
387
387
  // - show when
388
- ShowWhenLoaded: ({ children: n, fallback: c, errorFallback: f }) => {
388
+ ShowWhenLoaded: ({ children: n, fallback: f, errorFallback: c }) => {
389
389
  const l = E(r, (g) => g.state);
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: S(f, u.getErrors() ?? [], () => u.retry()) }) : /* @__PURE__ */ p(w, { children: n }));
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 }));
391
391
  },
392
392
  ShowWhileLoading: a("loading"),
393
393
  ShowWhenError: a("error", () => [u.getErrors() ?? [], () => u.retry()]),
394
394
  ShowWhenFinish: a(["error", "success"], () => [u.getErrors(), () => u.retry()]),
395
- ShowWhen: ({ children: n, state: c }) => {
396
- const f = a(c);
397
- return /* @__PURE__ */ p(f, { children: n });
395
+ ShowWhen: ({ children: n, state: f }) => {
396
+ const c = a(f);
397
+ return /* @__PURE__ */ p(c, { children: n });
398
398
  },
399
399
  destroy: () => {
400
400
  h.clear(r);
@@ -409,13 +409,15 @@ const m = (o, e) => Array.isArray(e) && e.includes(o) || o === e, ee = ({
409
409
  };
410
410
  return u;
411
411
  }, oe = (o = {}) => {
412
- const e = j(ee(o));
412
+ const e = j(ee(typeof o == "string" ? {
413
+ initialState: o
414
+ } : o));
413
415
  return O(() => () => {
414
416
  e.current.destroy();
415
417
  }, []), e.current;
416
418
  }, ue = ({ of: o, fallback: e, errorFallback: s, children: i }) => {
417
419
  const r = o.map((t) => t.use());
418
- 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: S(
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(
419
421
  s,
420
422
  o.reduce((t, a) => [...t, ...a.getErrors() ?? []], []),
421
423
  () => {
@@ -432,7 +434,7 @@ export {
432
434
  k as fallbacks,
433
435
  K as getDefaultErrorFallback,
434
436
  I as getDefaultFallback,
435
- S as reactNodeOrFunctionValue,
437
+ L as reactNodeOrFunctionValue,
436
438
  ae as registerDefaultErrorFallback,
437
439
  ie as registerDefaultFallback,
438
440
  ne as registerErrorFallback,
package/dist/main.min.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /*!
2
- * react-easy-loading v0.2.0
2
+ * react-easy-loading v0.3.0
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 m=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=>{m=o},z=o=>{C=o},V=()=>m,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 O,B;function Q(){if(B)return O;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,c;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 f;if(e&&t instanceof Map&&a instanceof Map){if(t.size!==a.size)return!1;for(f=t.entries();!(n=f.next()).done;)if(!a.has(n.value[0]))return!1;for(f=t.entries();!(n=f.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(f=t.entries();!(n=f.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(c=Object.keys(t),l=c.length,l!==Object.keys(a).length)return!1;for(n=l;n--!==0;)if(!Object.prototype.hasOwnProperty.call(a,c[n]))return!1;if(o&&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]],a[c[n]]))return!1;return!0}return t!==t&&a!==a}return O=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}},O}var X=Q();const Y=J(X),x=new y,g=new G(x),Z=(o,e)=>x.createStatic({initialValue:o},o,e);function L(o,e,s){let i,r=s;if(typeof o!="string"){const{key:f,prefix:h}=o;i=f,r=h}else i=_(o);const{prefix:t}=H(r),a=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(a.current,h)?a.current:(a.current=h,h)},[i,t,e]),c=v.useSyncExternalStore(l,n);return x.useEffect(i,t),c}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 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()})})},a=function(n,c=()=>[]){return f=>L(r,p=>S(p.state,n))?u.jsx(u.Fragment,{children:w(f.children,...c())}):null},l={use:()=>L(r,n=>n.state),useIsLoading:()=>L(r,n=>n.state)==="loading",useErrors:()=>L(r,n=>n.errors),useContext:()=>L(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:o}),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 b=n();b.then(()=>{l.set("success")}),b.catch(E=>{l.set("error"),E&&l.setErrors([E])});for(const E of c.thens)b.then(...E);for(const E of c.catchs)b.catch(...E);for(const E of c.finallies)b.finally(...E);b.then(h,p)})};return l.setRetry(f.start),f},ShowWhenLoaded:({children:n,fallback:c,errorFallback:f})=>{const h=L(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:a("loading"),ShowWhenError:a("error",()=>[l.getErrors()??[],()=>l.retry()]),ShowWhenFinish:a(["error","success"],()=>[l.getErrors(),()=>l.retry()]),ShowWhen:({children:n,state:c})=>{const f=a(c);return u.jsx(f,{children:n})},destroy:()=>{g.clear(r)},__get__id:()=>`loading_id//${r.prefix}/${r.key}`};return l},R=(o={})=>{const e=v.useRef($(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=(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"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-easy-loading",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "keywords": [