react-easy-loading 0.1.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/.editorconfig +17 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +112 -0
- package/LICENSE +21 -0
- package/README.md +299 -0
- package/dist/components/AllLoaded.d.ts +10 -0
- package/dist/components/AllLoaded.d.ts.map +1 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/create-loading.d.ts +68 -0
- package/dist/create-loading.d.ts.map +1 -0
- package/dist/fallbacks.d.ts +30 -0
- package/dist/fallbacks.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/main.esm.js +570 -0
- package/dist/main.min.js +13 -0
- package/dist/utils/lib.d.ts +11 -0
- package/dist/utils/lib.d.ts.map +1 -0
- package/package.json +66 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { ReactNodeOrFunction } from './create-loading';
|
|
3
|
+
export interface FallbackRegistry {
|
|
4
|
+
[k: string]: never;
|
|
5
|
+
}
|
|
6
|
+
export interface ErrorFallbackRegistry {
|
|
7
|
+
[k: string]: never;
|
|
8
|
+
}
|
|
9
|
+
export declare const fallbacks: Record<keyof FallbackRegistry, React.ReactNode>;
|
|
10
|
+
export declare const errorFallbacks: Record<keyof ErrorFallbackRegistry, React.ReactNode>;
|
|
11
|
+
export declare const registerFallback: (key: keyof FallbackRegistry, component: React.ReactNode) => void;
|
|
12
|
+
export declare const registerErrorFallback: (key: keyof ErrorFallbackRegistry, component: React.ReactNode) => void;
|
|
13
|
+
export declare const registerDefaultFallback: (component: React.ReactNode) => void;
|
|
14
|
+
export declare const registerDefaultErrorFallback: (component: React.ReactNode) => void;
|
|
15
|
+
export declare const getDefaultFallback: () => React.ReactNode;
|
|
16
|
+
export declare const getDefaultErrorFallback: () => ReactNodeOrFunction<[errors: string[] | undefined, retry: (() => void) | undefined]>;
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
* @param defaultFallback
|
|
20
|
+
* @param propFallback
|
|
21
|
+
*/
|
|
22
|
+
export declare const resolveLoadingFallback: (defaultFallback?: React.ReactNode, propFallback?: React.ReactNode) => ReactNodeOrFunction<unknown[]>;
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
* @param defaultFallback
|
|
26
|
+
* @param propFallback
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveErrorFallback<Errors>(defaultFallback?: ReactNodeOrFunction<[errors: Errors, retry: () => void]>, propFallback?: ReactNodeOrFunction<[errors: Errors, retry: () => void]>): ReactNodeOrFunction<[errors: Errors, retry: () => void]>;
|
|
29
|
+
export declare function reactNodeOrFunctionValue<Args extends unknown[] = unknown[]>(r: ReactNodeOrFunction<Args>, ...args: Args): React.ReactNode;
|
|
30
|
+
//# sourceMappingURL=fallbacks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fallbacks.d.ts","sourceRoot":"","sources":["../src/fallbacks.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,kBAAkB,CAAC;AAE1D,MAAM,WAAW,gBAAgB;IAC7B,CAAC,CAAC,EAAE,MAAM,GAAG,KAAK,CAAA;CACrB;AAED,MAAM,WAAW,qBAAqB;IAClC,CAAC,CAAC,EAAE,MAAM,GAAG,KAAK,CAAA;CACrB;AAED,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,gBAAgB,EAAE,KAAK,CAAC,SAAS,CAAM,CAAC;AAE7E,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,qBAAqB,EAAE,KAAK,CAAC,SAAS,CAAM,CAAC;AAGvF,eAAO,MAAM,gBAAgB,GAAI,KAAK,MAAM,gBAAgB,EAAE,WAAW,KAAK,CAAC,SAAS,SAEvF,CAAA;AAGD,eAAO,MAAM,qBAAqB,GAAI,KAAK,MAAM,qBAAqB,EAAE,WAAW,KAAK,CAAC,SAAS,SAEjG,CAAA;AAeD,eAAO,MAAM,uBAAuB,GAAI,WAAW,KAAK,CAAC,SAAS,SAEjE,CAAA;AAGD,eAAO,MAAM,4BAA4B,GAAI,WAAW,KAAK,CAAC,SAAS,SAEtE,CAAA;AAED,eAAO,MAAM,kBAAkB,uBAAyB,CAAC;AAEzD,eAAO,MAAM,uBAAuB,yEAtByD,IAAI,eAsB9B,CAAC;AAGpE;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAC/B,kBAAkB,KAAK,CAAC,SAAS,EACjC,eAAe,KAAK,CAAC,SAAS,mCAGjC,CAAA;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAE,MAAM,EACxC,eAAe,CAAC,EAAE,mBAAmB,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,IAAI,CAAC,CAAC,EAC1E,YAAY,CAAC,EAAE,mBAAmB,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,IAAI,CAAC,CAAC,qDAElB,IAAI,GAC5D;AA+BD,wBAAgB,wBAAwB,CAAC,IAAI,SAAS,OAAO,EAAE,GAAG,OAAO,EAAE,EAAE,CAAC,EAAE,mBAAmB,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,IAAI,GAAG,KAAK,CAAC,SAAS,CAKzI"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC"}
|
package/dist/main.esm.js
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* react-easy-loading v0.1.0
|
|
3
|
+
* (c) Hichem Taboukouyout
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
* Github: github.com/HichemTab-tech/react-easy-loading
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { jsx as p, jsxs as W, Fragment as b } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as $, useEffect as j, useRef as z, useMemo as D, useSyncExternalStore as I, useContext as K } from "react";
|
|
10
|
+
const _ = {}, C = {}, fe = (a, e) => {
|
|
11
|
+
_[a] = e;
|
|
12
|
+
}, ce = (a, e) => {
|
|
13
|
+
C[a] = e;
|
|
14
|
+
};
|
|
15
|
+
let P = /* @__PURE__ */ p("div", { children: "Loading..." }), R = (a, e) => /* @__PURE__ */ W("div", { children: [
|
|
16
|
+
/* @__PURE__ */ p("h3", { children: "Errors:" }),
|
|
17
|
+
/* @__PURE__ */ p("ul", { children: a?.map((t, r) => /* @__PURE__ */ p("li", { children: t }, r)) }),
|
|
18
|
+
/* @__PURE__ */ p("button", { onClick: e, disabled: !e, children: "Retry" })
|
|
19
|
+
] });
|
|
20
|
+
const he = (a) => {
|
|
21
|
+
P = a;
|
|
22
|
+
}, ge = (a) => {
|
|
23
|
+
R = a;
|
|
24
|
+
}, T = () => P, U = () => R, k = (a, e) => F(T, _, a, e);
|
|
25
|
+
function B(a, e) {
|
|
26
|
+
return F(U, C, a, e);
|
|
27
|
+
}
|
|
28
|
+
function F(a, e, t, r) {
|
|
29
|
+
return r ? typeof r == "string" ? e[r] ?? r : r : r === null ? null : t ? typeof t == "string" ? e[t] ?? t : t : a();
|
|
30
|
+
}
|
|
31
|
+
function m(a, ...e) {
|
|
32
|
+
return typeof a == "function" ? a(...e) : a;
|
|
33
|
+
}
|
|
34
|
+
/*!
|
|
35
|
+
* react-shared-states v1.0.22
|
|
36
|
+
* (c) Hichem Taboukouyout
|
|
37
|
+
* Released under the MIT License.
|
|
38
|
+
* Github: github.com/HichemTab-tech/react-shared-states
|
|
39
|
+
*/
|
|
40
|
+
const M = (...a) => {
|
|
41
|
+
}, L = (a) => {
|
|
42
|
+
if (!a) throw new Error("Value is empty");
|
|
43
|
+
return a;
|
|
44
|
+
}, N = () => Math.random().toString(36).substring(2, 15), q = $(void 0), G = () => K(q), A = [];
|
|
45
|
+
class c {
|
|
46
|
+
data = /* @__PURE__ */ new Map();
|
|
47
|
+
defaultValue() {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
addListener(e, t, r) {
|
|
51
|
+
this.data.has(c.prefix(e, t)) || this.data.set(c.prefix(e, t), {
|
|
52
|
+
...this.defaultValue(),
|
|
53
|
+
listeners: []
|
|
54
|
+
}), this.data.get(c.prefix(e, t)).listeners.push(r);
|
|
55
|
+
}
|
|
56
|
+
removeListener(e, t, r) {
|
|
57
|
+
this.data.has(c.prefix(e, t)) && (this.data.get(c.prefix(e, t)).listeners = this.data.get(c.prefix(e, t)).listeners.filter((s) => s !== r));
|
|
58
|
+
}
|
|
59
|
+
callListeners(e, t) {
|
|
60
|
+
this.data.has(c.prefix(e, t)) && this.data.get(c.prefix(e, t)).listeners.forEach((r) => r());
|
|
61
|
+
}
|
|
62
|
+
init(e, t, r, s = !1) {
|
|
63
|
+
this.data.has(c.prefix(e, t)) || this.data.set(c.prefix(e, t), {
|
|
64
|
+
...r,
|
|
65
|
+
isStatic: s,
|
|
66
|
+
listeners: []
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
createStatic(e, t) {
|
|
70
|
+
const r = t ?? t ?? "_global", s = {
|
|
71
|
+
key: N(),
|
|
72
|
+
prefix: r,
|
|
73
|
+
...e
|
|
74
|
+
};
|
|
75
|
+
return A.push(s), this.initStatic(s), s;
|
|
76
|
+
}
|
|
77
|
+
initStatic(e) {
|
|
78
|
+
const { key: t, prefix: r } = e;
|
|
79
|
+
this.init(t, r, this.defaultValue(), !0);
|
|
80
|
+
}
|
|
81
|
+
clearAll(e = !1, t = !1) {
|
|
82
|
+
this.data.forEach((r, s) => {
|
|
83
|
+
const [i, o] = c.extractPrefix(s);
|
|
84
|
+
this.clear(o, i, e, t);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
clear(e, t, r = !1, s = !1) {
|
|
88
|
+
r || this.callListeners(e, t);
|
|
89
|
+
const i = this.data.get(c.prefix(e, t));
|
|
90
|
+
if (!i) return;
|
|
91
|
+
const o = { ...i };
|
|
92
|
+
if (this.data.delete(c.prefix(e, t)), o.isStatic && !s) {
|
|
93
|
+
const u = A.find((n) => n.key === e && n.prefix === t);
|
|
94
|
+
u && this.initStatic(u);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
get(e, t) {
|
|
98
|
+
let r = this.has(e, t);
|
|
99
|
+
if (r)
|
|
100
|
+
return this.data.get(r);
|
|
101
|
+
}
|
|
102
|
+
setValue(e, t, r) {
|
|
103
|
+
this.data.has(c.prefix(e, t)) && this.data.set(c.prefix(e, t), {
|
|
104
|
+
...this.data.get(c.prefix(e, t)),
|
|
105
|
+
...r
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
has(e, t) {
|
|
109
|
+
return this.data.has(c.prefix(e, t)) ? c.prefix(e, t) : this.data.has(c.prefix(e, "_global")) ? c.prefix(e, "_global") : void 0;
|
|
110
|
+
}
|
|
111
|
+
static prefix(e, t) {
|
|
112
|
+
if (e.includes("//")) throw new Error("key cannot contain '//'");
|
|
113
|
+
return `${t}//${e}`;
|
|
114
|
+
}
|
|
115
|
+
static extractPrefix(e) {
|
|
116
|
+
return e.split("//");
|
|
117
|
+
}
|
|
118
|
+
useEffect(e, t, r = null) {
|
|
119
|
+
j(() => () => {
|
|
120
|
+
r?.(), M(`[${c.prefix(e, t)}]`, "unmount effect"), this.data.get(c.prefix(e, t)).listeners?.length === 0 && this.clear(e, t);
|
|
121
|
+
}, []);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
class V {
|
|
125
|
+
constructor(e) {
|
|
126
|
+
this.sharedData = e;
|
|
127
|
+
}
|
|
128
|
+
get(e, t) {
|
|
129
|
+
let r, s = t;
|
|
130
|
+
if (typeof e != "string") {
|
|
131
|
+
const { key: o, prefix: u } = e;
|
|
132
|
+
r = o, s = u;
|
|
133
|
+
} else
|
|
134
|
+
r = L(e);
|
|
135
|
+
const i = s || "_global";
|
|
136
|
+
return this.sharedData.get(r, i);
|
|
137
|
+
}
|
|
138
|
+
set(e, t, r) {
|
|
139
|
+
let s, i = r;
|
|
140
|
+
if (typeof e != "string") {
|
|
141
|
+
const { key: u, prefix: n } = e;
|
|
142
|
+
s = u, i = n;
|
|
143
|
+
} else
|
|
144
|
+
s = L(e);
|
|
145
|
+
const o = i || "_global";
|
|
146
|
+
this.sharedData.init(s, o, t), this.sharedData.setValue(s, o, t), this.sharedData.callListeners(s, o);
|
|
147
|
+
}
|
|
148
|
+
update(e, t, r) {
|
|
149
|
+
let s;
|
|
150
|
+
typeof e == "string" ? s = this.get(e, r) : s = this.get(e);
|
|
151
|
+
const i = t(s);
|
|
152
|
+
typeof e == "string" ? this.set(e, i, r) : this.set(e, i);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* clear all values from the shared data
|
|
156
|
+
*/
|
|
157
|
+
clearAll() {
|
|
158
|
+
this.sharedData.clearAll();
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* clear all values from the shared data in a scope
|
|
162
|
+
* @param scopeName
|
|
163
|
+
*/
|
|
164
|
+
clearScope(e) {
|
|
165
|
+
const t = e || "_global";
|
|
166
|
+
this.sharedData.data.forEach((r, s) => {
|
|
167
|
+
const [i, o] = c.extractPrefix(s);
|
|
168
|
+
if (i === t) {
|
|
169
|
+
this.sharedData.clear(o, i), this.sharedData.callListeners(o, i);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* resolve a shared created object to a value
|
|
176
|
+
* @param sharedCreated
|
|
177
|
+
*/
|
|
178
|
+
resolve(e) {
|
|
179
|
+
const { key: t, prefix: r } = e;
|
|
180
|
+
return this.get(t, r);
|
|
181
|
+
}
|
|
182
|
+
clear(e, t) {
|
|
183
|
+
let r, s;
|
|
184
|
+
typeof e == "string" ? (r = e, s = t || "_global") : (r = e.key, s = e.prefix), this.sharedData.clear(r, s);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* check if a value exists in the shared data
|
|
188
|
+
* @param key
|
|
189
|
+
* @param scopeName
|
|
190
|
+
*/
|
|
191
|
+
has(e, t = "_global") {
|
|
192
|
+
const r = t || "_global";
|
|
193
|
+
return !!this.sharedData.has(e, r);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* get all values from the shared data
|
|
197
|
+
*/
|
|
198
|
+
getAll() {
|
|
199
|
+
const e = {};
|
|
200
|
+
return this.sharedData.data.forEach((t, r) => {
|
|
201
|
+
const [s, i] = c.extractPrefix(r);
|
|
202
|
+
e[s] = e[s] || {}, e[s][i] = t;
|
|
203
|
+
}), e;
|
|
204
|
+
}
|
|
205
|
+
subscribe(e, t, r) {
|
|
206
|
+
let s, i;
|
|
207
|
+
return typeof e == "string" ? (s = e, i = r || "_global") : (s = e.key, i = e.prefix), this.sharedData.addListener(s, i, t), () => {
|
|
208
|
+
this.sharedData.removeListener(s, i, t);
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const H = (a) => {
|
|
213
|
+
const e = G();
|
|
214
|
+
return {
|
|
215
|
+
prefix: a ?? e?.scopeName ?? "_global"
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
function J(a) {
|
|
219
|
+
return a && a.__esModule && Object.prototype.hasOwnProperty.call(a, "default") ? a.default : a;
|
|
220
|
+
}
|
|
221
|
+
var w, O;
|
|
222
|
+
function Q() {
|
|
223
|
+
if (O) return w;
|
|
224
|
+
O = 1;
|
|
225
|
+
var a = typeof Element < "u", e = typeof Map == "function", t = typeof Set == "function", r = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
|
|
226
|
+
function s(i, o) {
|
|
227
|
+
if (i === o) return !0;
|
|
228
|
+
if (i && o && typeof i == "object" && typeof o == "object") {
|
|
229
|
+
if (i.constructor !== o.constructor) return !1;
|
|
230
|
+
var u, n, l;
|
|
231
|
+
if (Array.isArray(i)) {
|
|
232
|
+
if (u = i.length, u != o.length) return !1;
|
|
233
|
+
for (n = u; n-- !== 0; )
|
|
234
|
+
if (!s(i[n], o[n])) return !1;
|
|
235
|
+
return !0;
|
|
236
|
+
}
|
|
237
|
+
var f;
|
|
238
|
+
if (e && i instanceof Map && o instanceof Map) {
|
|
239
|
+
if (i.size !== o.size) return !1;
|
|
240
|
+
for (f = i.entries(); !(n = f.next()).done; )
|
|
241
|
+
if (!o.has(n.value[0])) return !1;
|
|
242
|
+
for (f = i.entries(); !(n = f.next()).done; )
|
|
243
|
+
if (!s(n.value[1], o.get(n.value[0]))) return !1;
|
|
244
|
+
return !0;
|
|
245
|
+
}
|
|
246
|
+
if (t && i instanceof Set && o instanceof Set) {
|
|
247
|
+
if (i.size !== o.size) return !1;
|
|
248
|
+
for (f = i.entries(); !(n = f.next()).done; )
|
|
249
|
+
if (!o.has(n.value[0])) return !1;
|
|
250
|
+
return !0;
|
|
251
|
+
}
|
|
252
|
+
if (r && ArrayBuffer.isView(i) && ArrayBuffer.isView(o)) {
|
|
253
|
+
if (u = i.length, u != o.length) return !1;
|
|
254
|
+
for (n = u; n-- !== 0; )
|
|
255
|
+
if (i[n] !== o[n]) return !1;
|
|
256
|
+
return !0;
|
|
257
|
+
}
|
|
258
|
+
if (i.constructor === RegExp) return i.source === o.source && i.flags === o.flags;
|
|
259
|
+
if (i.valueOf !== Object.prototype.valueOf && typeof i.valueOf == "function" && typeof o.valueOf == "function") return i.valueOf() === o.valueOf();
|
|
260
|
+
if (i.toString !== Object.prototype.toString && typeof i.toString == "function" && typeof o.toString == "function") return i.toString() === o.toString();
|
|
261
|
+
if (l = Object.keys(i), u = l.length, u !== Object.keys(o).length) return !1;
|
|
262
|
+
for (n = u; n-- !== 0; )
|
|
263
|
+
if (!Object.prototype.hasOwnProperty.call(o, l[n])) return !1;
|
|
264
|
+
if (a && i instanceof Element) return !1;
|
|
265
|
+
for (n = u; n-- !== 0; )
|
|
266
|
+
if (!((l[n] === "_owner" || l[n] === "__v" || l[n] === "__o") && i.$$typeof) && !s(i[l[n]], o[l[n]]))
|
|
267
|
+
return !1;
|
|
268
|
+
return !0;
|
|
269
|
+
}
|
|
270
|
+
return i !== i && o !== o;
|
|
271
|
+
}
|
|
272
|
+
return w = function(i, o) {
|
|
273
|
+
try {
|
|
274
|
+
return s(i, o);
|
|
275
|
+
} catch (u) {
|
|
276
|
+
if ((u.message || "").match(/stack|recursion/i))
|
|
277
|
+
return console.warn("react-fast-compare cannot handle circular refs"), !1;
|
|
278
|
+
throw u;
|
|
279
|
+
}
|
|
280
|
+
}, w;
|
|
281
|
+
}
|
|
282
|
+
var X = Q();
|
|
283
|
+
const Y = /* @__PURE__ */ J(X);
|
|
284
|
+
class Z extends c {
|
|
285
|
+
defaultValue() {
|
|
286
|
+
return { value: void 0 };
|
|
287
|
+
}
|
|
288
|
+
initValue(e, t, r, s = !1) {
|
|
289
|
+
super.init(e, t, { value: r }, s);
|
|
290
|
+
}
|
|
291
|
+
initStatic(e) {
|
|
292
|
+
const { key: t, prefix: r, initialValue: s } = e;
|
|
293
|
+
this.initValue(t, r, s, !0);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
class ee extends V {
|
|
297
|
+
constructor(e) {
|
|
298
|
+
super(e);
|
|
299
|
+
}
|
|
300
|
+
get(e, t = "_global") {
|
|
301
|
+
return typeof e != "string" ? super.get(e)?.value : super.get(e, t)?.value;
|
|
302
|
+
}
|
|
303
|
+
set(e, t, r = "_global") {
|
|
304
|
+
if (typeof e != "string") {
|
|
305
|
+
super.set(e, { value: t });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
super.set(e, { value: t }, r);
|
|
309
|
+
}
|
|
310
|
+
update(e, t, r = "_global") {
|
|
311
|
+
let s;
|
|
312
|
+
typeof e == "string" ? s = this.get(e, r) : s = this.get(e);
|
|
313
|
+
const i = t(s);
|
|
314
|
+
typeof e == "string" ? this.set(e, i, r) : this.set(e, i);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const E = new Z(), g = new ee(E), te = (a, e) => E.createStatic({ initialValue: a }, e);
|
|
318
|
+
function v(a, e, t) {
|
|
319
|
+
let r, s = t;
|
|
320
|
+
if (typeof a != "string") {
|
|
321
|
+
const { key: f, prefix: h } = a;
|
|
322
|
+
r = f, s = h;
|
|
323
|
+
} else
|
|
324
|
+
r = L(a);
|
|
325
|
+
const { prefix: i } = H(s), o = z(void 0), u = D(() => (f) => (E.addListener(r, i, f), () => {
|
|
326
|
+
E.removeListener(r, i, f);
|
|
327
|
+
}), []), n = D(() => () => {
|
|
328
|
+
const f = E.get(r, i)?.value, h = e(f);
|
|
329
|
+
return Y(o.current, h) ? o.current : h;
|
|
330
|
+
}, []), l = I(u, n);
|
|
331
|
+
return E.useEffect(r, i), l;
|
|
332
|
+
}
|
|
333
|
+
class re extends c {
|
|
334
|
+
defaultValue() {
|
|
335
|
+
return {
|
|
336
|
+
fnState: {
|
|
337
|
+
results: void 0,
|
|
338
|
+
isLoading: !1,
|
|
339
|
+
error: void 0
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
initValue(e, t, r = !1) {
|
|
344
|
+
super.init(e, t, this.defaultValue(), r);
|
|
345
|
+
}
|
|
346
|
+
setValue(e, t, r) {
|
|
347
|
+
super.setValue(e, t, r);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
class se extends V {
|
|
351
|
+
constructor(e) {
|
|
352
|
+
super(e);
|
|
353
|
+
}
|
|
354
|
+
get(e, t = "_global") {
|
|
355
|
+
return typeof e != "string" ? super.get(e)?.fnState : super.get(e, t)?.fnState;
|
|
356
|
+
}
|
|
357
|
+
set(e, t, r = "_global") {
|
|
358
|
+
if (typeof e != "string") {
|
|
359
|
+
super.set(e, t);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
super.set(e, t, r);
|
|
363
|
+
}
|
|
364
|
+
update(e, t, r = "_global") {
|
|
365
|
+
let s;
|
|
366
|
+
typeof e == "string" ? s = this.get(e, r) : s = this.get(e);
|
|
367
|
+
const i = t(s);
|
|
368
|
+
typeof e == "string" ? this.set(e, i, r) : this.set(e, i);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const ie = new re();
|
|
372
|
+
new se(ie);
|
|
373
|
+
class ne extends c {
|
|
374
|
+
defaultValue() {
|
|
375
|
+
return {
|
|
376
|
+
fnState: {
|
|
377
|
+
data: void 0,
|
|
378
|
+
isLoading: !1,
|
|
379
|
+
error: void 0,
|
|
380
|
+
subscribed: !1
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
initValue(e, t, r = !1) {
|
|
385
|
+
super.init(e, t, this.defaultValue(), r);
|
|
386
|
+
}
|
|
387
|
+
setValue(e, t, r) {
|
|
388
|
+
super.setValue(e, t, r);
|
|
389
|
+
}
|
|
390
|
+
useEffect(e, t) {
|
|
391
|
+
j(() => () => {
|
|
392
|
+
M(`[${c.prefix(e, t)}]`, "unmount effect2"), this.get(e, t)?.listeners.length === 0 && this.unsubscribe(e, t);
|
|
393
|
+
}, []), super.useEffect(e, t);
|
|
394
|
+
}
|
|
395
|
+
async unsubscribe(e, t) {
|
|
396
|
+
const r = this.get(e, t);
|
|
397
|
+
r && (r.unsubscribe && (r.unsubscribe(), r.unsubscribe = void 0), r.fnState = { ...r.fnState, subscribed: !1 }, this.callListeners(e, t));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
class ae extends V {
|
|
401
|
+
constructor(e) {
|
|
402
|
+
super(e);
|
|
403
|
+
}
|
|
404
|
+
get(e, t = "_global") {
|
|
405
|
+
return typeof e != "string" ? super.get(e)?.fnState : super.get(e, t)?.fnState;
|
|
406
|
+
}
|
|
407
|
+
set(e, t, r = "_global") {
|
|
408
|
+
if (typeof e != "string") {
|
|
409
|
+
super.set(e, t);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
super.set(e, t, r);
|
|
413
|
+
}
|
|
414
|
+
update(e, t, r = "_global") {
|
|
415
|
+
let s;
|
|
416
|
+
typeof e == "string" ? s = this.get(e, r) : s = this.get(e);
|
|
417
|
+
const i = t(s);
|
|
418
|
+
typeof e == "string" ? this.set(e, i, r) : this.set(e, i);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const oe = new ne();
|
|
422
|
+
new ae(oe);
|
|
423
|
+
const S = (a, e) => Array.isArray(e) && e.includes(a) || a === e, de = ({
|
|
424
|
+
initialState: a = "loading",
|
|
425
|
+
defaultFallback: e,
|
|
426
|
+
defaultErrorFallback: t,
|
|
427
|
+
defaultRetry: r
|
|
428
|
+
} = {}) => {
|
|
429
|
+
const s = te({
|
|
430
|
+
state: a,
|
|
431
|
+
retry: r
|
|
432
|
+
}), i = (n) => {
|
|
433
|
+
let l = u.get();
|
|
434
|
+
return new Promise((f, h) => {
|
|
435
|
+
if (S(u.get(), n)) {
|
|
436
|
+
f();
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const d = g.subscribe(s, () => {
|
|
440
|
+
S(u.get(), n) ? (d(), f()) : l !== u.get() && h();
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
}, o = function(n, l = () => []) {
|
|
444
|
+
return (f) => v(s, (d) => S(d.state, n)) ? /* @__PURE__ */ p(b, { children: m(f.children, ...l()) }) : null;
|
|
445
|
+
}, u = {
|
|
446
|
+
//hooks
|
|
447
|
+
use: () => v(s, (n) => n.state),
|
|
448
|
+
useIsLoading: () => v(s, (n) => n.state) === "loading",
|
|
449
|
+
useErrors: () => v(s, (n) => n.errors),
|
|
450
|
+
useContext: () => v(s, (n) => n.context),
|
|
451
|
+
set: (n) => g.update(s, (l) => ({ ...l, state: n })),
|
|
452
|
+
retry: () => g.get(s).retry?.(),
|
|
453
|
+
// getters
|
|
454
|
+
isLoading: () => g.get(s).state === "loading",
|
|
455
|
+
isIdle: () => g.get(s).state === "idle",
|
|
456
|
+
isSuccess: () => g.get(s).state === "success",
|
|
457
|
+
isError: () => g.get(s).state === "error",
|
|
458
|
+
isFinished: () => g.get(s).state === "success" || g.get(s).state === "error",
|
|
459
|
+
is: (n) => S(u.get(), n),
|
|
460
|
+
hasErrors: () => g.get(s).errors !== void 0,
|
|
461
|
+
get: () => g.get(s).state,
|
|
462
|
+
getErrors: () => g.get(s).errors,
|
|
463
|
+
getContext: () => g.get(s).context,
|
|
464
|
+
// setters
|
|
465
|
+
reset: () => g.set(s, {
|
|
466
|
+
state: a
|
|
467
|
+
}),
|
|
468
|
+
addError: (n) => {
|
|
469
|
+
g.update(s, (l) => ({
|
|
470
|
+
...l,
|
|
471
|
+
errors: [...l.errors ?? [], n]
|
|
472
|
+
}));
|
|
473
|
+
},
|
|
474
|
+
setContext: (n) => {
|
|
475
|
+
g.update(s, (l) => ({ ...l, context: n }));
|
|
476
|
+
},
|
|
477
|
+
setErrors: (n) => {
|
|
478
|
+
g.update(s, (l) => ({ ...l, errors: n }));
|
|
479
|
+
},
|
|
480
|
+
clearErrors: () => {
|
|
481
|
+
g.update(s, (n) => ({ ...n, errors: void 0 }));
|
|
482
|
+
},
|
|
483
|
+
// set a retry function
|
|
484
|
+
setRetry: (n) => g.update(s, (l) => ({ ...l, retry: n })),
|
|
485
|
+
// promises
|
|
486
|
+
whenLoaded: () => i("success").catch((n) => {
|
|
487
|
+
throw new Error("loading failed", { cause: n });
|
|
488
|
+
}),
|
|
489
|
+
whenFinished: () => i(["success", "error"]),
|
|
490
|
+
// wrap a promise
|
|
491
|
+
wrap(n) {
|
|
492
|
+
const l = () => new Promise((f, h) => {
|
|
493
|
+
u.clearErrors(), u.set("loading"), n().then((d) => {
|
|
494
|
+
u.set("success"), f(d);
|
|
495
|
+
}).catch((d) => {
|
|
496
|
+
u.set("error"), d && u.setErrors([d]), h(d);
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
return u.setRetry(l), l();
|
|
500
|
+
},
|
|
501
|
+
wrapWithControl(n) {
|
|
502
|
+
const l = {
|
|
503
|
+
thens: [],
|
|
504
|
+
catchs: [],
|
|
505
|
+
finallies: []
|
|
506
|
+
}, f = {
|
|
507
|
+
then: (...h) => (l.thens.push(h), f),
|
|
508
|
+
catch_: (...h) => (l.catchs.push(h), f),
|
|
509
|
+
finally_: (...h) => (l.finallies.push(h), f),
|
|
510
|
+
start: () => new Promise((h, d) => {
|
|
511
|
+
u.clearErrors(), u.set("loading");
|
|
512
|
+
let x = n();
|
|
513
|
+
x.then(() => {
|
|
514
|
+
u.set("success");
|
|
515
|
+
}), x.catch((y) => {
|
|
516
|
+
u.set("error"), y && u.setErrors([y]);
|
|
517
|
+
});
|
|
518
|
+
for (const y of l.thens)
|
|
519
|
+
x.then(...y);
|
|
520
|
+
for (const y of l.catchs)
|
|
521
|
+
x.catch(...y);
|
|
522
|
+
for (const y of l.finallies)
|
|
523
|
+
x.finally(...y);
|
|
524
|
+
x.then(h, d);
|
|
525
|
+
})
|
|
526
|
+
};
|
|
527
|
+
return u.setRetry(f.start), f;
|
|
528
|
+
},
|
|
529
|
+
// components
|
|
530
|
+
// - show when
|
|
531
|
+
ShowWhenLoaded: ({ children: n, fallback: l, errorFallback: f }) => {
|
|
532
|
+
const h = v(s, (d) => d.state);
|
|
533
|
+
return h === "idle" ? null : (l = k(e, l), f = B(t, f === !0 ? void 0 : f), h === "loading" ? /* @__PURE__ */ p(b, { children: l }) : h === "error" ? /* @__PURE__ */ p(b, { children: m(f, u.getErrors() ?? [], () => u.retry()) }) : /* @__PURE__ */ p(b, { children: n }));
|
|
534
|
+
},
|
|
535
|
+
ShowWhileLoading: o("loading"),
|
|
536
|
+
ShowWhenError: o("error", () => [u.getErrors() ?? [], () => u.retry()]),
|
|
537
|
+
ShowWhenFinish: o(["error", "success"], () => [u.getErrors(), () => u.retry()]),
|
|
538
|
+
ShowWhen: ({ children: n, state: l }) => {
|
|
539
|
+
const f = o(l);
|
|
540
|
+
return /* @__PURE__ */ p(f, { children: n });
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
return u;
|
|
544
|
+
}, pe = ({ of: a, fallback: e, errorFallback: t, children: r }) => {
|
|
545
|
+
const s = a.map((i) => i.use());
|
|
546
|
+
return e = k(void 0, e), s.some((i) => i === "loading") ? e : s.every((i) => i !== "loading") ? s.some((i) => i === "error") ? (t = B(void 0, t), /* @__PURE__ */ p(b, { children: m(
|
|
547
|
+
t,
|
|
548
|
+
a.reduce((i, o) => [...i, ...o.getErrors() ?? []], []),
|
|
549
|
+
() => {
|
|
550
|
+
a.map((i) => {
|
|
551
|
+
i.isError() && i.retry();
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
) })) : /* @__PURE__ */ p(b, { children: r }) : null;
|
|
555
|
+
};
|
|
556
|
+
export {
|
|
557
|
+
pe as AllLoaded,
|
|
558
|
+
de as createLoading,
|
|
559
|
+
C as errorFallbacks,
|
|
560
|
+
_ as fallbacks,
|
|
561
|
+
U as getDefaultErrorFallback,
|
|
562
|
+
T as getDefaultFallback,
|
|
563
|
+
m as reactNodeOrFunctionValue,
|
|
564
|
+
ge as registerDefaultErrorFallback,
|
|
565
|
+
he as registerDefaultFallback,
|
|
566
|
+
ce as registerErrorFallback,
|
|
567
|
+
fe as registerFallback,
|
|
568
|
+
B as resolveErrorFallback,
|
|
569
|
+
k as resolveLoadingFallback
|
|
570
|
+
};
|
package/dist/main.min.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* react-easy-loading v0.1.0
|
|
3
|
+
* (c) Hichem Taboukouyout
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
* Github: github.com/HichemTab-tech/react-easy-loading
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
(function(d,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],h):(d=typeof globalThis<"u"?globalThis:d||self,h(d.ReactEasyLoading={},d.jsxRuntime,d.React))})(this,(function(d,h,b){"use strict";const V={},D={},$=(a,e)=>{V[a]=e},z=(a,e)=>{D[a]=e};let _=h.jsx("div",{children:"Loading..."}),C=(a,e)=>h.jsxs("div",{children:[h.jsx("h3",{children:"Errors:"}),h.jsx("ul",{children:a?.map((t,r)=>h.jsx("li",{children:t},r))}),h.jsx("button",{onClick:e,disabled:!e,children:"Retry"})]});const q=a=>{_=a},I=a=>{C=a},P=()=>_,j=()=>C,F=(a,e)=>M(P,V,a,e);function A(a,e){return M(j,D,a,e)}function M(a,e,t,r){return r?typeof r=="string"?e[r]??r:r:r===null?null:t?typeof t=="string"?e[t]??t:t:a()}function w(a,...e){return typeof a=="function"?a(...e):a}/*!
|
|
9
|
+
* react-shared-states v1.0.22
|
|
10
|
+
* (c) Hichem Taboukouyout
|
|
11
|
+
* Released under the MIT License.
|
|
12
|
+
* Github: github.com/HichemTab-tech/react-shared-states
|
|
13
|
+
*/const B=(...a)=>{},O=a=>{if(!a)throw new Error("Value is empty");return a},K=()=>Math.random().toString(36).substring(2,15),N=b.createContext(void 0),U=()=>b.useContext(N),W=[];class c{data=new Map;defaultValue(){return{}}addListener(e,t,r){this.data.has(c.prefix(e,t))||this.data.set(c.prefix(e,t),{...this.defaultValue(),listeners:[]}),this.data.get(c.prefix(e,t)).listeners.push(r)}removeListener(e,t,r){this.data.has(c.prefix(e,t))&&(this.data.get(c.prefix(e,t)).listeners=this.data.get(c.prefix(e,t)).listeners.filter(s=>s!==r))}callListeners(e,t){this.data.has(c.prefix(e,t))&&this.data.get(c.prefix(e,t)).listeners.forEach(r=>r())}init(e,t,r,s=!1){this.data.has(c.prefix(e,t))||this.data.set(c.prefix(e,t),{...r,isStatic:s,listeners:[]})}createStatic(e,t){const r=t??t??"_global",s={key:K(),prefix:r,...e};return W.push(s),this.initStatic(s),s}initStatic(e){const{key:t,prefix:r}=e;this.init(t,r,this.defaultValue(),!0)}clearAll(e=!1,t=!1){this.data.forEach((r,s)=>{const[i,o]=c.extractPrefix(s);this.clear(o,i,e,t)})}clear(e,t,r=!1,s=!1){r||this.callListeners(e,t);const i=this.data.get(c.prefix(e,t));if(!i)return;const o={...i};if(this.data.delete(c.prefix(e,t)),o.isStatic&&!s){const l=W.find(n=>n.key===e&&n.prefix===t);l&&this.initStatic(l)}}get(e,t){let r=this.has(e,t);if(r)return this.data.get(r)}setValue(e,t,r){this.data.has(c.prefix(e,t))&&this.data.set(c.prefix(e,t),{...this.data.get(c.prefix(e,t)),...r})}has(e,t){return this.data.has(c.prefix(e,t))?c.prefix(e,t):this.data.has(c.prefix(e,"_global"))?c.prefix(e,"_global"):void 0}static prefix(e,t){if(e.includes("//"))throw new Error("key cannot contain '//'");return`${t}//${e}`}static extractPrefix(e){return e.split("//")}useEffect(e,t,r=null){b.useEffect(()=>()=>{r?.(),B(`[${c.prefix(e,t)}]`,"unmount effect"),this.data.get(c.prefix(e,t)).listeners?.length===0&&this.clear(e,t)},[])}}class k{constructor(e){this.sharedData=e}get(e,t){let r,s=t;if(typeof e!="string"){const{key:o,prefix:l}=e;r=o,s=l}else r=O(e);const i=s||"_global";return this.sharedData.get(r,i)}set(e,t,r){let s,i=r;if(typeof e!="string"){const{key:l,prefix:n}=e;s=l,i=n}else s=O(e);const o=i||"_global";this.sharedData.init(s,o,t),this.sharedData.setValue(s,o,t),this.sharedData.callListeners(s,o)}update(e,t,r){let s;typeof e=="string"?s=this.get(e,r):s=this.get(e);const i=t(s);typeof e=="string"?this.set(e,i,r):this.set(e,i)}clearAll(){this.sharedData.clearAll()}clearScope(e){const t=e||"_global";this.sharedData.data.forEach((r,s)=>{const[i,o]=c.extractPrefix(s);if(i===t){this.sharedData.clear(o,i),this.sharedData.callListeners(o,i);return}})}resolve(e){const{key:t,prefix:r}=e;return this.get(t,r)}clear(e,t){let r,s;typeof e=="string"?(r=e,s=t||"_global"):(r=e.key,s=e.prefix),this.sharedData.clear(r,s)}has(e,t="_global"){const r=t||"_global";return!!this.sharedData.has(e,r)}getAll(){const e={};return this.sharedData.data.forEach((t,r)=>{const[s,i]=c.extractPrefix(r);e[s]=e[s]||{},e[s][i]=t}),e}subscribe(e,t,r){let s,i;return typeof e=="string"?(s=e,i=r||"_global"):(s=e.key,i=e.prefix),this.sharedData.addListener(s,i,t),()=>{this.sharedData.removeListener(s,i,t)}}}const G=a=>{const e=U();return{prefix:a??e?.scopeName??"_global"}};function H(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var m,T;function J(){if(T)return m;T=1;var a=typeof Element<"u",e=typeof Map=="function",t=typeof Set=="function",r=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function s(i,o){if(i===o)return!0;if(i&&o&&typeof i=="object"&&typeof o=="object"){if(i.constructor!==o.constructor)return!1;var l,n,u;if(Array.isArray(i)){if(l=i.length,l!=o.length)return!1;for(n=l;n--!==0;)if(!s(i[n],o[n]))return!1;return!0}var f;if(e&&i instanceof Map&&o instanceof Map){if(i.size!==o.size)return!1;for(f=i.entries();!(n=f.next()).done;)if(!o.has(n.value[0]))return!1;for(f=i.entries();!(n=f.next()).done;)if(!s(n.value[1],o.get(n.value[0])))return!1;return!0}if(t&&i instanceof Set&&o instanceof Set){if(i.size!==o.size)return!1;for(f=i.entries();!(n=f.next()).done;)if(!o.has(n.value[0]))return!1;return!0}if(r&&ArrayBuffer.isView(i)&&ArrayBuffer.isView(o)){if(l=i.length,l!=o.length)return!1;for(n=l;n--!==0;)if(i[n]!==o[n])return!1;return!0}if(i.constructor===RegExp)return i.source===o.source&&i.flags===o.flags;if(i.valueOf!==Object.prototype.valueOf&&typeof i.valueOf=="function"&&typeof o.valueOf=="function")return i.valueOf()===o.valueOf();if(i.toString!==Object.prototype.toString&&typeof i.toString=="function"&&typeof o.toString=="function")return i.toString()===o.toString();if(u=Object.keys(i),l=u.length,l!==Object.keys(o).length)return!1;for(n=l;n--!==0;)if(!Object.prototype.hasOwnProperty.call(o,u[n]))return!1;if(a&&i instanceof Element)return!1;for(n=l;n--!==0;)if(!((u[n]==="_owner"||u[n]==="__v"||u[n]==="__o")&&i.$$typeof)&&!s(i[u[n]],o[u[n]]))return!1;return!0}return i!==i&&o!==o}return m=function(i,o){try{return s(i,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 Q=J();const X=H(Q);class Y extends c{defaultValue(){return{value:void 0}}initValue(e,t,r,s=!1){super.init(e,t,{value:r},s)}initStatic(e){const{key:t,prefix:r,initialValue:s}=e;this.initValue(t,r,s,!0)}}class Z extends k{constructor(e){super(e)}get(e,t="_global"){return typeof e!="string"?super.get(e)?.value:super.get(e,t)?.value}set(e,t,r="_global"){if(typeof e!="string"){super.set(e,{value:t});return}super.set(e,{value:t},r)}update(e,t,r="_global"){let s;typeof e=="string"?s=this.get(e,r):s=this.get(e);const i=t(s);typeof e=="string"?this.set(e,i,r):this.set(e,i)}}const x=new Y,p=new Z(x),R=(a,e)=>x.createStatic({initialValue:a},e);function E(a,e,t){let r,s=t;if(typeof a!="string"){const{key:f,prefix:g}=a;r=f,s=g}else r=O(a);const{prefix:i}=G(s),o=b.useRef(void 0),l=b.useMemo(()=>f=>(x.addListener(r,i,f),()=>{x.removeListener(r,i,f)}),[]),n=b.useMemo(()=>()=>{const f=x.get(r,i)?.value,g=e(f);return X(o.current,g)?o.current:g},[]),u=b.useSyncExternalStore(l,n);return x.useEffect(r,i),u}class ee extends c{defaultValue(){return{fnState:{results:void 0,isLoading:!1,error:void 0}}}initValue(e,t,r=!1){super.init(e,t,this.defaultValue(),r)}setValue(e,t,r){super.setValue(e,t,r)}}class te extends k{constructor(e){super(e)}get(e,t="_global"){return typeof e!="string"?super.get(e)?.fnState:super.get(e,t)?.fnState}set(e,t,r="_global"){if(typeof e!="string"){super.set(e,t);return}super.set(e,t,r)}update(e,t,r="_global"){let s;typeof e=="string"?s=this.get(e,r):s=this.get(e);const i=t(s);typeof e=="string"?this.set(e,i,r):this.set(e,i)}}const re=new ee;new te(re);class se extends c{defaultValue(){return{fnState:{data:void 0,isLoading:!1,error:void 0,subscribed:!1}}}initValue(e,t,r=!1){super.init(e,t,this.defaultValue(),r)}setValue(e,t,r){super.setValue(e,t,r)}useEffect(e,t){b.useEffect(()=>()=>{B(`[${c.prefix(e,t)}]`,"unmount effect2"),this.get(e,t)?.listeners.length===0&&this.unsubscribe(e,t)},[]),super.useEffect(e,t)}async unsubscribe(e,t){const r=this.get(e,t);r&&(r.unsubscribe&&(r.unsubscribe(),r.unsubscribe=void 0),r.fnState={...r.fnState,subscribed:!1},this.callListeners(e,t))}}class ie extends k{constructor(e){super(e)}get(e,t="_global"){return typeof e!="string"?super.get(e)?.fnState:super.get(e,t)?.fnState}set(e,t,r="_global"){if(typeof e!="string"){super.set(e,t);return}super.set(e,t,r)}update(e,t,r="_global"){let s;typeof e=="string"?s=this.get(e,r):s=this.get(e);const i=t(s);typeof e=="string"?this.set(e,i,r):this.set(e,i)}}const ne=new se;new ie(ne);const L=(a,e)=>Array.isArray(e)&&e.includes(a)||a===e,ae=({initialState:a="loading",defaultFallback:e,defaultErrorFallback:t,defaultRetry:r}={})=>{const s=R({state:a,retry:r}),i=n=>{let u=l.get();return new Promise((f,g)=>{if(L(l.get(),n)){f();return}const y=p.subscribe(s,()=>{L(l.get(),n)?(y(),f()):u!==l.get()&&g()})})},o=function(n,u=()=>[]){return f=>E(s,y=>L(y.state,n))?h.jsx(h.Fragment,{children:w(f.children,...u())}):null},l={use:()=>E(s,n=>n.state),useIsLoading:()=>E(s,n=>n.state)==="loading",useErrors:()=>E(s,n=>n.errors),useContext:()=>E(s,n=>n.context),set:n=>p.update(s,u=>({...u,state:n})),retry:()=>p.get(s).retry?.(),isLoading:()=>p.get(s).state==="loading",isIdle:()=>p.get(s).state==="idle",isSuccess:()=>p.get(s).state==="success",isError:()=>p.get(s).state==="error",isFinished:()=>p.get(s).state==="success"||p.get(s).state==="error",is:n=>L(l.get(),n),hasErrors:()=>p.get(s).errors!==void 0,get:()=>p.get(s).state,getErrors:()=>p.get(s).errors,getContext:()=>p.get(s).context,reset:()=>p.set(s,{state:a}),addError:n=>{p.update(s,u=>({...u,errors:[...u.errors??[],n]}))},setContext:n=>{p.update(s,u=>({...u,context:n}))},setErrors:n=>{p.update(s,u=>({...u,errors:n}))},clearErrors:()=>{p.update(s,n=>({...n,errors:void 0}))},setRetry:n=>p.update(s,u=>({...u,retry:n})),whenLoaded:()=>i("success").catch(n=>{throw new Error("loading failed",{cause:n})}),whenFinished:()=>i(["success","error"]),wrap(n){const u=()=>new Promise((f,g)=>{l.clearErrors(),l.set("loading"),n().then(y=>{l.set("success"),f(y)}).catch(y=>{l.set("error"),y&&l.setErrors([y]),g(y)})});return l.setRetry(u),u()},wrapWithControl(n){const u={thens:[],catchs:[],finallies:[]},f={then:(...g)=>(u.thens.push(g),f),catch_:(...g)=>(u.catchs.push(g),f),finally_:(...g)=>(u.finallies.push(g),f),start:()=>new Promise((g,y)=>{l.clearErrors(),l.set("loading");let S=n();S.then(()=>{l.set("success")}),S.catch(v=>{l.set("error"),v&&l.setErrors([v])});for(const v of u.thens)S.then(...v);for(const v of u.catchs)S.catch(...v);for(const v of u.finallies)S.finally(...v);S.then(g,y)})};return l.setRetry(f.start),f},ShowWhenLoaded:({children:n,fallback:u,errorFallback:f})=>{const g=E(s,y=>y.state);return g==="idle"?null:(u=F(e,u),f=A(t,f===!0?void 0:f),g==="loading"?h.jsx(h.Fragment,{children:u}):g==="error"?h.jsx(h.Fragment,{children:w(f,l.getErrors()??[],()=>l.retry())}):h.jsx(h.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:u})=>{const f=o(u);return h.jsx(f,{children:n})}};return l},oe=({of:a,fallback:e,errorFallback:t,children:r})=>{const s=a.map(i=>i.use());return e=F(void 0,e),s.some(i=>i==="loading")?e:s.every(i=>i!=="loading")?s.some(i=>i==="error")?(t=A(void 0,t),h.jsx(h.Fragment,{children:w(t,a.reduce((i,o)=>[...i,...o.getErrors()??[]],[]),()=>{a.map(i=>{i.isError()&&i.retry()})})})):h.jsx(h.Fragment,{children:r}):null};d.AllLoaded=oe,d.createLoading=ae,d.errorFallbacks=D,d.fallbacks=V,d.getDefaultErrorFallback=j,d.getDefaultFallback=P,d.reactNodeOrFunctionValue=w,d.registerDefaultErrorFallback=I,d.registerDefaultFallback=q,d.registerErrorFallback=z,d.registerFallback=$,d.resolveErrorFallback=A,d.resolveLoadingFallback=F,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a type that can be either a single instance of type `T` or an array of instances of type `T`.
|
|
3
|
+
*
|
|
4
|
+
* This type is useful in scenarios where a value may come as a single item or as a collection of items,
|
|
5
|
+
* providing flexibility in handling both cases seamlessly.
|
|
6
|
+
*
|
|
7
|
+
* @template T The type of the element(s).
|
|
8
|
+
*/
|
|
9
|
+
export type AOS<T = any> = T | T[];
|
|
10
|
+
export declare const aosEquals: <T>(needle: T, aos: AOS<T>) => boolean;
|
|
11
|
+
//# sourceMappingURL=lib.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../../src/utils/lib.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;AAEnC,eAAO,MAAM,SAAS,GAAI,CAAC,EAAE,QAAQ,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,YAElD,CAAA"}
|