react-shared-states 2.1.3-beta → 2.1.5-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.esm.js +157 -159
- package/dist/main.min.js +2 -2
- package/package.json +1 -1
package/dist/main.esm.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-shared-states v2.1.
|
|
2
|
+
* react-shared-states v2.1.5-beta
|
|
3
3
|
* (c) Hichem Taboukouyout
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
* Github: github.com/HichemTab-tech/react-shared-states
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { jsx as
|
|
9
|
-
import { createContext as
|
|
10
|
-
let
|
|
8
|
+
import { jsx as k } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as T, useMemo as b, useContext as W, useEffect as P, useSyncExternalStore as O, useRef as Y } from "react";
|
|
10
|
+
let V = !1;
|
|
11
11
|
const ee = (a) => {
|
|
12
|
-
|
|
13
|
-
},
|
|
14
|
-
|
|
12
|
+
V = a;
|
|
13
|
+
}, R = (...a) => {
|
|
14
|
+
V && console.log(
|
|
15
15
|
"%c[react-shared-states]",
|
|
16
16
|
"color: #007acc; font-weight: bold",
|
|
17
17
|
...a
|
|
@@ -19,13 +19,13 @@ const ee = (a) => {
|
|
|
19
19
|
}, E = (a) => {
|
|
20
20
|
if (!a) throw new Error("Value is empty");
|
|
21
21
|
return a;
|
|
22
|
-
}, q = () => Math.random().toString(36).substring(2, 15),
|
|
22
|
+
}, q = () => Math.random().toString(36).substring(2, 15), $ = T(void 0), te = ({ children: a, scopeName: t }) => {
|
|
23
23
|
if (t && t.includes("//")) throw new Error("scopeName cannot contain '//'");
|
|
24
|
-
return t || (t = b(() => q(), [])), /* @__PURE__ */
|
|
25
|
-
}, H = () =>
|
|
24
|
+
return t || (t = b(() => q(), [])), /* @__PURE__ */ k($.Provider, { value: { scopeName: t }, children: a });
|
|
25
|
+
}, H = () => W($), B = [], D = /* @__PURE__ */ Symbol.for("react-shared-states.manager");
|
|
26
26
|
function J(a, t = () => null) {
|
|
27
|
-
const
|
|
28
|
-
return
|
|
27
|
+
const n = globalThis;
|
|
28
|
+
return n[D] || (n[D] = {}), n[D][a] || (n[D][a] = new l(t)), n[D][a];
|
|
29
29
|
}
|
|
30
30
|
class l {
|
|
31
31
|
constructor(t = () => null) {
|
|
@@ -39,98 +39,96 @@ class l {
|
|
|
39
39
|
__get__uniqueId() {
|
|
40
40
|
return this._uniqueId;
|
|
41
41
|
}
|
|
42
|
-
static getInstance(t,
|
|
43
|
-
return J(t,
|
|
42
|
+
static getInstance(t, n = () => null) {
|
|
43
|
+
return J(t, n);
|
|
44
44
|
}
|
|
45
|
-
addListener(t,
|
|
46
|
-
const i = l.prefix(t,
|
|
45
|
+
addListener(t, n, e) {
|
|
46
|
+
const i = l.prefix(t, n), r = this.data.get(i);
|
|
47
47
|
r && r.listeners.push(e);
|
|
48
48
|
}
|
|
49
|
-
removeListener(t,
|
|
50
|
-
const i = l.prefix(t,
|
|
51
|
-
r && (r.listeners = r.listeners.filter((
|
|
49
|
+
removeListener(t, n, e) {
|
|
50
|
+
const i = l.prefix(t, n), r = this.data.get(i);
|
|
51
|
+
r && (r.listeners = r.listeners.filter((s) => s !== e));
|
|
52
52
|
}
|
|
53
|
-
callListeners(t,
|
|
54
|
-
const e = l.prefix(t,
|
|
53
|
+
callListeners(t, n) {
|
|
54
|
+
const e = l.prefix(t, n), i = this.data.get(e);
|
|
55
55
|
i && i.listeners.forEach((r) => r());
|
|
56
56
|
}
|
|
57
|
-
init(t,
|
|
58
|
-
const r = l.prefix(t,
|
|
57
|
+
init(t, n, e, i = !1) {
|
|
58
|
+
const r = l.prefix(t, n);
|
|
59
59
|
this.data.has(r) || this.data.set(r, {
|
|
60
60
|
value: e,
|
|
61
61
|
isStatic: i || void 0,
|
|
62
62
|
listeners: []
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
|
-
createStatic(t,
|
|
65
|
+
createStatic(t, n, e) {
|
|
66
66
|
const i = e ?? "_global", r = {
|
|
67
67
|
key: q(),
|
|
68
68
|
prefix: i,
|
|
69
69
|
...t
|
|
70
70
|
};
|
|
71
|
-
return B.push(r), this.init(r.key, r.prefix,
|
|
71
|
+
return B.push(r), this.init(r.key, r.prefix, n, !0), this.defaultValue = () => n, r;
|
|
72
72
|
}
|
|
73
73
|
initStatic(t) {
|
|
74
|
-
const { key:
|
|
75
|
-
this.init(
|
|
74
|
+
const { key: n, prefix: e } = t;
|
|
75
|
+
this.init(n, e, this.defaultValue(), !0);
|
|
76
76
|
}
|
|
77
|
-
clearAll(t = !1,
|
|
77
|
+
clearAll(t = !1, n = !1) {
|
|
78
78
|
this.data.forEach((e, i) => {
|
|
79
|
-
const [r,
|
|
80
|
-
this.clear(
|
|
79
|
+
const [r, s] = l.extractPrefix(i);
|
|
80
|
+
this.clear(s, r, t, n);
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
|
-
clear(t,
|
|
84
|
-
const r = l.prefix(t,
|
|
85
|
-
e || this.callListeners(t,
|
|
86
|
-
const
|
|
87
|
-
if (
|
|
88
|
-
const o = B.find((c) => c.key === t && c.prefix ===
|
|
83
|
+
clear(t, n, e = !1, i = !1) {
|
|
84
|
+
const r = l.prefix(t, n);
|
|
85
|
+
e || this.callListeners(t, n);
|
|
86
|
+
const s = this.data.get(r);
|
|
87
|
+
if (s && (this.data.delete(r), s.isStatic && !i)) {
|
|
88
|
+
const o = B.find((c) => c.key === t && c.prefix === n);
|
|
89
89
|
o && this.initStatic(o);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
get(t,
|
|
93
|
-
let e = this.has(t,
|
|
92
|
+
get(t, n) {
|
|
93
|
+
let e = this.has(t, n);
|
|
94
94
|
if (e)
|
|
95
95
|
return this.data.get(e);
|
|
96
96
|
}
|
|
97
|
-
setValue(t,
|
|
98
|
-
const i = l.prefix(t,
|
|
97
|
+
setValue(t, n, e) {
|
|
98
|
+
const i = l.prefix(t, n), r = this.data.get(i);
|
|
99
99
|
r && (r.value = e, this.data.set(i, r));
|
|
100
100
|
}
|
|
101
|
-
has(t,
|
|
102
|
-
return this.data.has(l.prefix(t,
|
|
101
|
+
has(t, n) {
|
|
102
|
+
return this.data.has(l.prefix(t, n)) ? l.prefix(t, n) : this.data.has(l.prefix(t, "_global")) ? l.prefix(t, "_global") : void 0;
|
|
103
103
|
}
|
|
104
|
-
static prefix(t,
|
|
104
|
+
static prefix(t, n) {
|
|
105
105
|
if (t.includes("//")) throw new Error("key cannot contain '//'");
|
|
106
|
-
return `${
|
|
106
|
+
return `${n}//${t}`;
|
|
107
107
|
}
|
|
108
108
|
static extractPrefix(t) {
|
|
109
|
-
const
|
|
110
|
-
return [
|
|
109
|
+
const n = t.split("//");
|
|
110
|
+
return [n[0], n.slice(1).join("//")];
|
|
111
111
|
}
|
|
112
|
-
useEffect(t,
|
|
112
|
+
useEffect(t, n, e = null) {
|
|
113
113
|
P(() => () => {
|
|
114
|
-
e?.()
|
|
115
|
-
|
|
116
|
-
i && i.listeners?.length === 0 && this.clear(t, s);
|
|
117
|
-
}, [t, s]);
|
|
114
|
+
e?.();
|
|
115
|
+
}, [t, n]);
|
|
118
116
|
}
|
|
119
117
|
}
|
|
120
|
-
class
|
|
118
|
+
class M {
|
|
121
119
|
constructor(t) {
|
|
122
120
|
this.sharedData = t, this._uniqueId = q();
|
|
123
121
|
}
|
|
124
122
|
_uniqueId;
|
|
125
|
-
_get(t,
|
|
126
|
-
let e, i =
|
|
123
|
+
_get(t, n) {
|
|
124
|
+
let e, i = n;
|
|
127
125
|
if (typeof t != "string") {
|
|
128
126
|
const { key: o, prefix: c } = t;
|
|
129
127
|
e = o, i = c;
|
|
130
128
|
} else
|
|
131
129
|
e = E(t);
|
|
132
|
-
const r = i || "_global",
|
|
133
|
-
return
|
|
130
|
+
const r = i || "_global", s = this.sharedData.get(e, r);
|
|
131
|
+
return s ? { value: s.value, key: e, prefix: r } : {
|
|
134
132
|
key: e,
|
|
135
133
|
prefix: r,
|
|
136
134
|
value: void 0
|
|
@@ -142,23 +140,23 @@ class $ {
|
|
|
142
140
|
managerId: this.sharedData.__get__uniqueId()
|
|
143
141
|
};
|
|
144
142
|
}
|
|
145
|
-
get(t,
|
|
146
|
-
return this._get(t,
|
|
143
|
+
get(t, n) {
|
|
144
|
+
return this._get(t, n).value;
|
|
147
145
|
}
|
|
148
|
-
set(t,
|
|
146
|
+
set(t, n, e) {
|
|
149
147
|
let i, r = e;
|
|
150
148
|
if (typeof t != "string") {
|
|
151
149
|
const { key: o, prefix: c } = t;
|
|
152
150
|
i = o, r = c;
|
|
153
151
|
} else
|
|
154
152
|
i = E(t);
|
|
155
|
-
const
|
|
156
|
-
this.sharedData.init(i,
|
|
153
|
+
const s = r || "_global";
|
|
154
|
+
this.sharedData.init(i, s, n), this.sharedData.setValue(i, s, n), this.sharedData.callListeners(i, s);
|
|
157
155
|
}
|
|
158
|
-
update(t,
|
|
156
|
+
update(t, n, e) {
|
|
159
157
|
const i = this._get(t, e);
|
|
160
158
|
if (i) {
|
|
161
|
-
const r =
|
|
159
|
+
const r = n(i.value);
|
|
162
160
|
this.set(i.key, r, i.prefix);
|
|
163
161
|
}
|
|
164
162
|
}
|
|
@@ -168,38 +166,38 @@ class $ {
|
|
|
168
166
|
}
|
|
169
167
|
// noinspection JSUnusedGlobalSymbols
|
|
170
168
|
clearScope(t) {
|
|
171
|
-
const
|
|
169
|
+
const n = t || "_global";
|
|
172
170
|
this.sharedData.data.forEach((e, i) => {
|
|
173
|
-
const [r,
|
|
174
|
-
r ===
|
|
171
|
+
const [r, s] = l.extractPrefix(i);
|
|
172
|
+
r === n && (this.sharedData.clear(s, r), this.sharedData.callListeners(s, r));
|
|
175
173
|
});
|
|
176
174
|
}
|
|
177
175
|
// noinspection JSUnusedGlobalSymbols
|
|
178
176
|
resolve(t) {
|
|
179
|
-
const { key:
|
|
180
|
-
return this.get(
|
|
177
|
+
const { key: n, prefix: e } = t;
|
|
178
|
+
return this.get(n, e);
|
|
181
179
|
}
|
|
182
|
-
clear(t,
|
|
180
|
+
clear(t, n) {
|
|
183
181
|
let e, i;
|
|
184
|
-
typeof t == "string" ? (e = t, i =
|
|
182
|
+
typeof t == "string" ? (e = t, i = n || "_global") : (e = t.key, i = t.prefix), this.sharedData.clear(e, i);
|
|
185
183
|
}
|
|
186
184
|
// noinspection JSUnusedGlobalSymbols
|
|
187
|
-
has(t,
|
|
188
|
-
const e =
|
|
185
|
+
has(t, n = "_global") {
|
|
186
|
+
const e = n || "_global";
|
|
189
187
|
return !!this.sharedData.has(t, e);
|
|
190
188
|
}
|
|
191
189
|
// noinspection JSUnusedGlobalSymbols
|
|
192
190
|
getAll() {
|
|
193
191
|
const t = {};
|
|
194
|
-
return this.sharedData.data.forEach((
|
|
192
|
+
return this.sharedData.data.forEach((n, e) => {
|
|
195
193
|
const [i, r] = l.extractPrefix(e);
|
|
196
|
-
t[i] = t[i] || {}, t[i][r] =
|
|
194
|
+
t[i] = t[i] || {}, t[i][r] = n.value;
|
|
197
195
|
}), t;
|
|
198
196
|
}
|
|
199
|
-
subscribe(t,
|
|
197
|
+
subscribe(t, n, e) {
|
|
200
198
|
let i, r;
|
|
201
|
-
return typeof t == "string" ? (i = t, r = e || "_global") : (i = t.key, r = t.prefix), this.sharedData.addListener(i, r,
|
|
202
|
-
this.sharedData.removeListener(i, r,
|
|
199
|
+
return typeof t == "string" ? (i = t, r = e || "_global") : (i = t.key, r = t.prefix), this.sharedData.addListener(i, r, n), () => {
|
|
200
|
+
this.sharedData.removeListener(i, r, n);
|
|
203
201
|
};
|
|
204
202
|
}
|
|
205
203
|
}
|
|
@@ -216,56 +214,56 @@ var F, G;
|
|
|
216
214
|
function U() {
|
|
217
215
|
if (G) return F;
|
|
218
216
|
G = 1;
|
|
219
|
-
var a = typeof Element < "u", t = typeof Map == "function",
|
|
220
|
-
function i(r,
|
|
221
|
-
if (r ===
|
|
222
|
-
if (r &&
|
|
223
|
-
if (r.constructor !==
|
|
217
|
+
var a = typeof Element < "u", t = typeof Map == "function", n = typeof Set == "function", e = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
|
|
218
|
+
function i(r, s) {
|
|
219
|
+
if (r === s) return !0;
|
|
220
|
+
if (r && s && typeof r == "object" && typeof s == "object") {
|
|
221
|
+
if (r.constructor !== s.constructor) return !1;
|
|
224
222
|
var o, c, d;
|
|
225
223
|
if (Array.isArray(r)) {
|
|
226
|
-
if (o = r.length, o !=
|
|
224
|
+
if (o = r.length, o != s.length) return !1;
|
|
227
225
|
for (c = o; c-- !== 0; )
|
|
228
|
-
if (!i(r[c],
|
|
226
|
+
if (!i(r[c], s[c])) return !1;
|
|
229
227
|
return !0;
|
|
230
228
|
}
|
|
231
229
|
var f;
|
|
232
|
-
if (t && r instanceof Map &&
|
|
233
|
-
if (r.size !==
|
|
230
|
+
if (t && r instanceof Map && s instanceof Map) {
|
|
231
|
+
if (r.size !== s.size) return !1;
|
|
234
232
|
for (f = r.entries(); !(c = f.next()).done; )
|
|
235
|
-
if (!
|
|
233
|
+
if (!s.has(c.value[0])) return !1;
|
|
236
234
|
for (f = r.entries(); !(c = f.next()).done; )
|
|
237
|
-
if (!i(c.value[1],
|
|
235
|
+
if (!i(c.value[1], s.get(c.value[0]))) return !1;
|
|
238
236
|
return !0;
|
|
239
237
|
}
|
|
240
|
-
if (
|
|
241
|
-
if (r.size !==
|
|
238
|
+
if (n && r instanceof Set && s instanceof Set) {
|
|
239
|
+
if (r.size !== s.size) return !1;
|
|
242
240
|
for (f = r.entries(); !(c = f.next()).done; )
|
|
243
|
-
if (!
|
|
241
|
+
if (!s.has(c.value[0])) return !1;
|
|
244
242
|
return !0;
|
|
245
243
|
}
|
|
246
|
-
if (e && ArrayBuffer.isView(r) && ArrayBuffer.isView(
|
|
247
|
-
if (o = r.length, o !=
|
|
244
|
+
if (e && ArrayBuffer.isView(r) && ArrayBuffer.isView(s)) {
|
|
245
|
+
if (o = r.length, o != s.length) return !1;
|
|
248
246
|
for (c = o; c-- !== 0; )
|
|
249
|
-
if (r[c] !==
|
|
247
|
+
if (r[c] !== s[c]) return !1;
|
|
250
248
|
return !0;
|
|
251
249
|
}
|
|
252
|
-
if (r.constructor === RegExp) return r.source ===
|
|
253
|
-
if (r.valueOf !== Object.prototype.valueOf && typeof r.valueOf == "function" && typeof
|
|
254
|
-
if (r.toString !== Object.prototype.toString && typeof r.toString == "function" && typeof
|
|
255
|
-
if (d = Object.keys(r), o = d.length, o !== Object.keys(
|
|
250
|
+
if (r.constructor === RegExp) return r.source === s.source && r.flags === s.flags;
|
|
251
|
+
if (r.valueOf !== Object.prototype.valueOf && typeof r.valueOf == "function" && typeof s.valueOf == "function") return r.valueOf() === s.valueOf();
|
|
252
|
+
if (r.toString !== Object.prototype.toString && typeof r.toString == "function" && typeof s.toString == "function") return r.toString() === s.toString();
|
|
253
|
+
if (d = Object.keys(r), o = d.length, o !== Object.keys(s).length) return !1;
|
|
256
254
|
for (c = o; c-- !== 0; )
|
|
257
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
255
|
+
if (!Object.prototype.hasOwnProperty.call(s, d[c])) return !1;
|
|
258
256
|
if (a && r instanceof Element) return !1;
|
|
259
257
|
for (c = o; c-- !== 0; )
|
|
260
|
-
if (!((d[c] === "_owner" || d[c] === "__v" || d[c] === "__o") && r.$$typeof) && !i(r[d[c]],
|
|
258
|
+
if (!((d[c] === "_owner" || d[c] === "__v" || d[c] === "__o") && r.$$typeof) && !i(r[d[c]], s[d[c]]))
|
|
261
259
|
return !1;
|
|
262
260
|
return !0;
|
|
263
261
|
}
|
|
264
|
-
return r !== r &&
|
|
262
|
+
return r !== r && s !== s;
|
|
265
263
|
}
|
|
266
|
-
return F = function(
|
|
264
|
+
return F = function(s, o) {
|
|
267
265
|
try {
|
|
268
|
-
return i(
|
|
266
|
+
return i(s, o);
|
|
269
267
|
} catch (c) {
|
|
270
268
|
if ((c.message || "").match(/stack|recursion/i))
|
|
271
269
|
return console.warn("react-fast-compare cannot handle circular refs"), !1;
|
|
@@ -274,76 +272,76 @@ function U() {
|
|
|
274
272
|
}, F;
|
|
275
273
|
}
|
|
276
274
|
var X = U();
|
|
277
|
-
const
|
|
278
|
-
function ne(a, t,
|
|
279
|
-
let e, i, r =
|
|
275
|
+
const z = /* @__PURE__ */ Q(X), h = l.getInstance("sharedStatesManager"), re = new M(h), se = (a, t) => h.createStatic({ initialValue: a }, a, t);
|
|
276
|
+
function ne(a, t, n) {
|
|
277
|
+
let e, i, r = n;
|
|
280
278
|
if (typeof a != "string") {
|
|
281
|
-
const { key: u, initialValue: v, prefix:
|
|
282
|
-
e = u, i = v, r =
|
|
279
|
+
const { key: u, initialValue: v, prefix: x } = a;
|
|
280
|
+
e = u, i = v, r = x;
|
|
283
281
|
} else
|
|
284
282
|
e = E(a), i = t;
|
|
285
|
-
const { prefix:
|
|
286
|
-
h.init(e,
|
|
287
|
-
const o = b(() => (u) => (h.init(e,
|
|
288
|
-
h.removeListener(e,
|
|
289
|
-
}), [e,
|
|
290
|
-
const v = typeof u == "function" ? u(h.get(e,
|
|
291
|
-
|
|
283
|
+
const { prefix: s } = j(r);
|
|
284
|
+
h.init(e, s, i);
|
|
285
|
+
const o = b(() => (u) => (h.init(e, s, t), h.addListener(e, s, u), () => {
|
|
286
|
+
h.removeListener(e, s, u);
|
|
287
|
+
}), [e, s, t]), c = b(() => () => h.get(e, s)?.value, [e, s]), d = O(o, c), f = (u) => {
|
|
288
|
+
const v = typeof u == "function" ? u(h.get(e, s)?.value) : u;
|
|
289
|
+
z(v, d) || (h.setValue(e, s, v), h.callListeners(e, s));
|
|
292
290
|
};
|
|
293
|
-
return h.useEffect(e,
|
|
291
|
+
return h.useEffect(e, s), [
|
|
294
292
|
d,
|
|
295
293
|
f
|
|
296
294
|
];
|
|
297
295
|
}
|
|
298
|
-
function ie(a, t,
|
|
299
|
-
let e, i =
|
|
296
|
+
function ie(a, t, n) {
|
|
297
|
+
let e, i = n;
|
|
300
298
|
if (typeof a != "string") {
|
|
301
299
|
const { key: f, prefix: u } = a;
|
|
302
300
|
e = f, i = u;
|
|
303
301
|
} else
|
|
304
302
|
e = E(a);
|
|
305
|
-
const { prefix: r } = j(i),
|
|
303
|
+
const { prefix: r } = j(i), s = Y(void 0), o = b(() => (f) => (h.init(e, r, void 0), h.addListener(e, r, f), () => {
|
|
306
304
|
h.removeListener(e, r, f);
|
|
307
305
|
}), [e, r]), c = b(() => () => {
|
|
308
306
|
const f = h.get(e, r)?.value, u = t(f);
|
|
309
|
-
return
|
|
307
|
+
return z(s.current, u) ? s.current : (s.current = u, u);
|
|
310
308
|
}, [e, r, t]), d = O(o, c);
|
|
311
309
|
return h.useEffect(e, r), d;
|
|
312
310
|
}
|
|
313
|
-
const y = l.getInstance("SharedFunctionValue"), I = new
|
|
311
|
+
const y = l.getInstance("SharedFunctionValue"), I = new M(y), A = {
|
|
314
312
|
results: void 0,
|
|
315
313
|
isLoading: !1,
|
|
316
314
|
error: void 0
|
|
317
315
|
}, ae = (a, t) => y.createStatic({ fn: a }, A, t);
|
|
318
|
-
function oe(a, t,
|
|
319
|
-
let e, i, r =
|
|
316
|
+
function oe(a, t, n) {
|
|
317
|
+
let e, i, r = n;
|
|
320
318
|
if (typeof a != "string") {
|
|
321
|
-
const { key: u, fn: v, prefix:
|
|
322
|
-
e = u, i = v, r =
|
|
319
|
+
const { key: u, fn: v, prefix: x } = a;
|
|
320
|
+
e = u, i = v, r = x;
|
|
323
321
|
} else
|
|
324
322
|
e = E(a), i = t;
|
|
325
|
-
const { prefix:
|
|
326
|
-
y.init(e,
|
|
323
|
+
const { prefix: s } = j(r);
|
|
324
|
+
y.init(e, s, A);
|
|
327
325
|
const o = b(
|
|
328
|
-
() => (u) => (y.init(e,
|
|
329
|
-
y.removeListener(e,
|
|
326
|
+
() => (u) => (y.init(e, s, A), y.addListener(e, s, u), () => {
|
|
327
|
+
y.removeListener(e, s, u);
|
|
330
328
|
}),
|
|
331
|
-
[e,
|
|
329
|
+
[e, s]
|
|
332
330
|
), c = b(
|
|
333
|
-
() => () => y.get(e,
|
|
334
|
-
[e,
|
|
331
|
+
() => () => y.get(e, s).value,
|
|
332
|
+
[e, s]
|
|
335
333
|
), d = O(o, c), f = async (u, ...v) => {
|
|
336
|
-
const
|
|
337
|
-
if (!u && (
|
|
338
|
-
I.update(e, (
|
|
334
|
+
const x = y.get(e, s);
|
|
335
|
+
if (!u && (x.value.isLoading || x.value.results !== void 0)) return x.value;
|
|
336
|
+
I.update(e, (S) => ({ ...S, isLoading: !0, error: void 0 }), s);
|
|
339
337
|
try {
|
|
340
|
-
const
|
|
341
|
-
I.set(e, { results:
|
|
342
|
-
} catch (
|
|
343
|
-
I.update(e, (p) => ({ ...p, isLoading: !1, error:
|
|
338
|
+
const S = await i(...v);
|
|
339
|
+
I.set(e, { results: S, isLoading: !1, error: void 0 }, s);
|
|
340
|
+
} catch (S) {
|
|
341
|
+
I.update(e, (p) => ({ ...p, isLoading: !1, error: S }), s);
|
|
344
342
|
}
|
|
345
343
|
};
|
|
346
|
-
return y.useEffect(e,
|
|
344
|
+
return y.useEffect(e, s), {
|
|
347
345
|
state: d,
|
|
348
346
|
trigger: (...u) => {
|
|
349
347
|
f(!1, ...u);
|
|
@@ -352,32 +350,32 @@ function oe(a, t, s) {
|
|
|
352
350
|
f(!0, ...u);
|
|
353
351
|
},
|
|
354
352
|
clear: () => {
|
|
355
|
-
I.set(e, A,
|
|
353
|
+
I.set(e, A, s);
|
|
356
354
|
}
|
|
357
355
|
};
|
|
358
356
|
}
|
|
359
|
-
const g = l.getInstance("SharedSubscription"), L = new
|
|
357
|
+
const g = l.getInstance("SharedSubscription"), L = new M(g), K = {
|
|
360
358
|
data: void 0,
|
|
361
359
|
isLoading: !1,
|
|
362
360
|
error: void 0,
|
|
363
361
|
subscribed: !1,
|
|
364
362
|
unsubscribe: void 0
|
|
365
|
-
}, ce = (a, t,
|
|
363
|
+
}, ce = (a, t, n) => g.createStatic({ subscriber: a }, { ...K, data: t?.initialValue }, n);
|
|
366
364
|
async function C(a, t) {
|
|
367
|
-
const
|
|
368
|
-
|
|
365
|
+
const n = g.get(a, t);
|
|
366
|
+
n?.value.unsubscribe && (n.value.unsubscribe(), L.update(a, (e) => ({ ...e, unsubscribe: void 0, subscribed: !1 }), t));
|
|
369
367
|
}
|
|
370
|
-
function ue(a, t,
|
|
371
|
-
let e, i, r =
|
|
368
|
+
function ue(a, t, n) {
|
|
369
|
+
let e, i, r = n, s = !1;
|
|
372
370
|
if (typeof a != "string") {
|
|
373
371
|
const { key: p, subscriber: m, prefix: _, triggerImmediately: w } = a;
|
|
374
|
-
e = p, i = m, r = _,
|
|
372
|
+
e = p, i = m, r = _, s = w ?? !1;
|
|
375
373
|
} else
|
|
376
374
|
e = E(a), i = t;
|
|
377
375
|
const { prefix: o } = j(r);
|
|
378
|
-
g.init(e, o,
|
|
376
|
+
g.init(e, o, K);
|
|
379
377
|
const c = b(
|
|
380
|
-
() => (p) => (g.init(e, o,
|
|
378
|
+
() => (p) => (g.init(e, o, K), g.addListener(e, o, p), () => {
|
|
381
379
|
g.removeListener(e, o, p);
|
|
382
380
|
}),
|
|
383
381
|
[e, o]
|
|
@@ -388,30 +386,30 @@ function ue(a, t, s) {
|
|
|
388
386
|
L.update(e, (m) => ({ ...m, data: p }), o);
|
|
389
387
|
}, v = (p) => {
|
|
390
388
|
L.update(e, (m) => ({ ...m, isLoading: !1, data: void 0, error: p }), o);
|
|
391
|
-
},
|
|
389
|
+
}, x = () => {
|
|
392
390
|
L.update(e, (p) => ({ ...p, isLoading: !1 }), o);
|
|
393
|
-
},
|
|
391
|
+
}, S = async (p) => {
|
|
394
392
|
const m = g.get(e, o);
|
|
395
393
|
if (p && await C(e, o), m.value.subscribed && !p) return m.value;
|
|
396
|
-
|
|
394
|
+
R("triggered !!"), L.update(e, (_) => ({ ..._, isLoading: !0, error: void 0 }), o);
|
|
397
395
|
try {
|
|
398
|
-
const _ = await i(u, v,
|
|
396
|
+
const _ = await i(u, v, x);
|
|
399
397
|
L.update(e, (w) => ({ ...w, unsubscribe: _, subscribed: !0, isLoading: !1 }), o);
|
|
400
398
|
} catch (_) {
|
|
401
399
|
L.update(e, (w) => ({ ...w, isLoading: !1, error: _ }), o);
|
|
402
400
|
}
|
|
403
401
|
};
|
|
404
402
|
return P(() => () => {
|
|
405
|
-
|
|
403
|
+
R(`[${l.prefix(e, o)}]`, "unmount effect2"), g.get(e, o)?.listeners.length === 0 && C(e, o);
|
|
406
404
|
}, [e, o]), g.useEffect(e, o), P(() => {
|
|
407
|
-
|
|
405
|
+
s && S(!1);
|
|
408
406
|
}, []), {
|
|
409
407
|
state: f,
|
|
410
408
|
trigger: () => {
|
|
411
|
-
|
|
409
|
+
S(!1);
|
|
412
410
|
},
|
|
413
411
|
forceTrigger: () => {
|
|
414
|
-
|
|
412
|
+
S(!0);
|
|
415
413
|
},
|
|
416
414
|
unsubscribe: () => {
|
|
417
415
|
C(e, o);
|
|
@@ -423,7 +421,7 @@ export {
|
|
|
423
421
|
ae as createSharedFunction,
|
|
424
422
|
se as createSharedState,
|
|
425
423
|
ce as createSharedSubscription,
|
|
426
|
-
|
|
424
|
+
V as isDevMode,
|
|
427
425
|
ee as setDevMode,
|
|
428
426
|
I as sharedFunctionsApi,
|
|
429
427
|
re as sharedStatesApi,
|
package/dist/main.min.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-shared-states v2.1.
|
|
2
|
+
* react-shared-states v2.1.5-beta
|
|
3
3
|
* (c) Hichem Taboukouyout
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
* Github: github.com/HichemTab-tech/react-shared-states
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
(function(f,D){typeof exports=="object"&&typeof module<"u"?D(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],D):(f=typeof globalThis<"u"?globalThis:f||self,D(f.ReactSharedStates={},f.jsxRuntime,f.React))})(this,(function(f,D,
|
|
8
|
+
(function(f,D){typeof exports=="object"&&typeof module<"u"?D(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],D):(f=typeof globalThis<"u"?globalThis:f||self,D(f.ReactSharedStates={},f.jsxRuntime,f.React))})(this,(function(f,D,d){"use strict";f.isDevMode=!1;const $=a=>{f.isDevMode=a},K=(...a)=>{f.isDevMode&&console.log("%c[react-shared-states]","color: #007acc; font-weight: bold",...a)},w=a=>{if(!a)throw new Error("Value is empty");return a},q=()=>Math.random().toString(36).substring(2,15),T=d.createContext(void 0),z=({children:a,scopeName:t})=>{if(t&&t.includes("//"))throw new Error("scopeName cannot contain '//'");return t||(t=d.useMemo(()=>q(),[])),D.jsx(T.Provider,{value:{scopeName:t},children:a})},k=()=>d.useContext(T),B=[],A=Symbol.for("react-shared-states.manager");function W(a,t=()=>null){const n=globalThis;return n[A]||(n[A]={}),n[A][a]||(n[A][a]=new h(t)),n[A][a]}class h{constructor(t=()=>null){this.defaultValue=t,this._uniqueId=q()}data=new Map;_uniqueId;__get__uniqueId(){return this._uniqueId}static getInstance(t,n=()=>null){return W(t,n)}addListener(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&r.listeners.push(e)}removeListener(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&(r.listeners=r.listeners.filter(s=>s!==e))}callListeners(t,n){const e=h.prefix(t,n),i=this.data.get(e);i&&i.listeners.forEach(r=>r())}init(t,n,e,i=!1){const r=h.prefix(t,n);this.data.has(r)||this.data.set(r,{value:e,isStatic:i||void 0,listeners:[]})}createStatic(t,n,e){const i=e??"_global",r={key:q(),prefix:i,...t};return B.push(r),this.init(r.key,r.prefix,n,!0),this.defaultValue=()=>n,r}initStatic(t){const{key:n,prefix:e}=t;this.init(n,e,this.defaultValue(),!0)}clearAll(t=!1,n=!1){this.data.forEach((e,i)=>{const[r,s]=h.extractPrefix(i);this.clear(s,r,t,n)})}clear(t,n,e=!1,i=!1){const r=h.prefix(t,n);e||this.callListeners(t,n);const s=this.data.get(r);if(s&&(this.data.delete(r),s.isStatic&&!i)){const o=B.find(u=>u.key===t&&u.prefix===n);o&&this.initStatic(o)}}get(t,n){let e=this.has(t,n);if(e)return this.data.get(e)}setValue(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&(r.value=e,this.data.set(i,r))}has(t,n){return this.data.has(h.prefix(t,n))?h.prefix(t,n):this.data.has(h.prefix(t,"_global"))?h.prefix(t,"_global"):void 0}static prefix(t,n){if(t.includes("//"))throw new Error("key cannot contain '//'");return`${n}//${t}`}static extractPrefix(t){const n=t.split("//");return[n[0],n.slice(1).join("//")]}useEffect(t,n,e=null){d.useEffect(()=>()=>{e?.()},[t,n])}}class O{constructor(t){this.sharedData=t,this._uniqueId=q()}_uniqueId;_get(t,n){let e,i=n;if(typeof t!="string"){const{key:o,prefix:u}=t;e=o,i=u}else e=w(t);const r=i||"_global",s=this.sharedData.get(e,r);return s?{value:s.value,key:e,prefix:r}:{key:e,prefix:r,value:void 0}}__log_instance_id(){return{apiId:this._uniqueId,managerId:this.sharedData.__get__uniqueId()}}get(t,n){return this._get(t,n).value}set(t,n,e){let i,r=e;if(typeof t!="string"){const{key:o,prefix:u}=t;i=o,r=u}else i=w(t);const s=r||"_global";this.sharedData.init(i,s,n),this.sharedData.setValue(i,s,n),this.sharedData.callListeners(i,s)}update(t,n,e){const i=this._get(t,e);if(i){const r=n(i.value);this.set(i.key,r,i.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(t){const n=t||"_global";this.sharedData.data.forEach((e,i)=>{const[r,s]=h.extractPrefix(i);r===n&&(this.sharedData.clear(s,r),this.sharedData.callListeners(s,r))})}resolve(t){const{key:n,prefix:e}=t;return this.get(n,e)}clear(t,n){let e,i;typeof t=="string"?(e=t,i=n||"_global"):(e=t.key,i=t.prefix),this.sharedData.clear(e,i)}has(t,n="_global"){const e=n||"_global";return!!this.sharedData.has(t,e)}getAll(){const t={};return this.sharedData.data.forEach((n,e)=>{const[i,r]=h.extractPrefix(e);t[i]=t[i]||{},t[i][r]=n.value}),t}subscribe(t,n,e){let i,r;return typeof t=="string"?(i=t,r=e||"_global"):(i=t.key,r=t.prefix),this.sharedData.addListener(i,r,n),()=>{this.sharedData.removeListener(i,r,n)}}}const I=a=>{const t=k();return{prefix:a??t?.scopeName??"_global"}};function Y(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var P,G;function H(){if(G)return P;G=1;var a=typeof Element<"u",t=typeof Map=="function",n=typeof Set=="function",e=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function i(r,s){if(r===s)return!0;if(r&&s&&typeof r=="object"&&typeof s=="object"){if(r.constructor!==s.constructor)return!1;var o,u,p;if(Array.isArray(r)){if(o=r.length,o!=s.length)return!1;for(u=o;u--!==0;)if(!i(r[u],s[u]))return!1;return!0}var l;if(t&&r instanceof Map&&s instanceof Map){if(r.size!==s.size)return!1;for(l=r.entries();!(u=l.next()).done;)if(!s.has(u.value[0]))return!1;for(l=r.entries();!(u=l.next()).done;)if(!i(u.value[1],s.get(u.value[0])))return!1;return!0}if(n&&r instanceof Set&&s instanceof Set){if(r.size!==s.size)return!1;for(l=r.entries();!(u=l.next()).done;)if(!s.has(u.value[0]))return!1;return!0}if(e&&ArrayBuffer.isView(r)&&ArrayBuffer.isView(s)){if(o=r.length,o!=s.length)return!1;for(u=o;u--!==0;)if(r[u]!==s[u])return!1;return!0}if(r.constructor===RegExp)return r.source===s.source&&r.flags===s.flags;if(r.valueOf!==Object.prototype.valueOf&&typeof r.valueOf=="function"&&typeof s.valueOf=="function")return r.valueOf()===s.valueOf();if(r.toString!==Object.prototype.toString&&typeof r.toString=="function"&&typeof s.toString=="function")return r.toString()===s.toString();if(p=Object.keys(r),o=p.length,o!==Object.keys(s).length)return!1;for(u=o;u--!==0;)if(!Object.prototype.hasOwnProperty.call(s,p[u]))return!1;if(a&&r instanceof Element)return!1;for(u=o;u--!==0;)if(!((p[u]==="_owner"||p[u]==="__v"||p[u]==="__o")&&r.$$typeof)&&!i(r[p[u]],s[p[u]]))return!1;return!0}return r!==r&&s!==s}return P=function(s,o){try{return i(s,o)}catch(u){if((u.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw u}},P}var J=H();const V=Y(J),g=h.getInstance("sharedStatesManager"),Q=new O(g),U=(a,t)=>g.createStatic({initialValue:a},a,t);function X(a,t,n){let e,i,r=n;if(typeof a!="string"){const{key:c,initialValue:y,prefix:m}=a;e=c,i=y,r=m}else e=w(a),i=t;const{prefix:s}=I(r);g.init(e,s,i);const o=d.useMemo(()=>c=>(g.init(e,s,t),g.addListener(e,s,c),()=>{g.removeListener(e,s,c)}),[e,s,t]),u=d.useMemo(()=>()=>g.get(e,s)?.value,[e,s]),p=d.useSyncExternalStore(o,u),l=c=>{const y=typeof c=="function"?c(g.get(e,s)?.value):c;V(y,p)||(g.setValue(e,s,y),g.callListeners(e,s))};return g.useEffect(e,s),[p,l]}function Z(a,t,n){let e,i=n;if(typeof a!="string"){const{key:l,prefix:c}=a;e=l,i=c}else e=w(a);const{prefix:r}=I(i),s=d.useRef(void 0),o=d.useMemo(()=>l=>(g.init(e,r,void 0),g.addListener(e,r,l),()=>{g.removeListener(e,r,l)}),[e,r]),u=d.useMemo(()=>()=>{const l=g.get(e,r)?.value,c=t(l);return V(s.current,c)?s.current:(s.current=c,c)},[e,r,t]),p=d.useSyncExternalStore(o,u);return g.useEffect(e,r),p}const x=h.getInstance("SharedFunctionValue"),M=new O(x),F={results:void 0,isLoading:!1,error:void 0},N=(a,t)=>x.createStatic({fn:a},F,t);function ee(a,t,n){let e,i,r=n;if(typeof a!="string"){const{key:c,fn:y,prefix:m}=a;e=c,i=y,r=m}else e=w(a),i=t;const{prefix:s}=I(r);x.init(e,s,F);const o=d.useMemo(()=>c=>(x.init(e,s,F),x.addListener(e,s,c),()=>{x.removeListener(e,s,c)}),[e,s]),u=d.useMemo(()=>()=>x.get(e,s).value,[e,s]),p=d.useSyncExternalStore(o,u),l=async(c,...y)=>{const m=x.get(e,s);if(!c&&(m.value.isLoading||m.value.results!==void 0))return m.value;M.update(e,b=>({...b,isLoading:!0,error:void 0}),s);try{const b=await i(...y);M.set(e,{results:b,isLoading:!1,error:void 0},s)}catch(b){M.update(e,S=>({...S,isLoading:!1,error:b}),s)}};return x.useEffect(e,s),{state:p,trigger:(...c)=>{l(!1,...c)},forceTrigger:(...c)=>{l(!0,...c)},clear:()=>{M.set(e,F,s)}}}const v=h.getInstance("SharedSubscription"),_=new O(v),C={data:void 0,isLoading:!1,error:void 0,subscribed:!1,unsubscribe:void 0},te=(a,t,n)=>v.createStatic({subscriber:a},{...C,data:t?.initialValue},n);async function R(a,t){const n=v.get(a,t);n?.value.unsubscribe&&(n.value.unsubscribe(),_.update(a,e=>({...e,unsubscribe:void 0,subscribed:!1}),t))}function re(a,t,n){let e,i,r=n,s=!1;if(typeof a!="string"){const{key:S,subscriber:E,prefix:L,triggerImmediately:j}=a;e=S,i=E,r=L,s=j??!1}else e=w(a),i=t;const{prefix:o}=I(r);v.init(e,o,C);const u=d.useMemo(()=>S=>(v.init(e,o,C),v.addListener(e,o,S),()=>{v.removeListener(e,o,S)}),[e,o]),p=d.useMemo(()=>()=>v.get(e,o).value,[e,o]),l=d.useSyncExternalStore(u,p),c=S=>{_.update(e,E=>({...E,data:S}),o)},y=S=>{_.update(e,E=>({...E,isLoading:!1,data:void 0,error:S}),o)},m=()=>{_.update(e,S=>({...S,isLoading:!1}),o)},b=async S=>{const E=v.get(e,o);if(S&&await R(e,o),E.value.subscribed&&!S)return E.value;K("triggered !!"),_.update(e,L=>({...L,isLoading:!0,error:void 0}),o);try{const L=await i(c,y,m);_.update(e,j=>({...j,unsubscribe:L,subscribed:!0,isLoading:!1}),o)}catch(L){_.update(e,j=>({...j,isLoading:!1,error:L}),o)}};return d.useEffect(()=>()=>{K(`[${h.prefix(e,o)}]`,"unmount effect2"),v.get(e,o)?.listeners.length===0&&R(e,o)},[e,o]),v.useEffect(e,o),d.useEffect(()=>{s&&b(!1)},[]),{state:l,trigger:()=>{b(!1)},forceTrigger:()=>{b(!0)},unsubscribe:()=>{R(e,o)}}}f.SharedStatesProvider=z,f.createSharedFunction=N,f.createSharedState=U,f.createSharedSubscription=te,f.setDevMode=$,f.sharedFunctionsApi=M,f.sharedStatesApi=Q,f.sharedSubscriptionsApi=_,f.useSharedContext=I,f.useSharedFunction=ee,f.useSharedState=X,f.useSharedStateSelector=Z,f.useSharedSubscription=re,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED