react-shared-states 2.1.5-beta → 2.1.6-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 +147 -145
- 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.6-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 T } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as W, useMemo as b, useContext as k, useEffect as P, useSyncExternalStore as O, useRef as Y } from "react";
|
|
10
|
+
let M = !1;
|
|
11
11
|
const ee = (a) => {
|
|
12
|
-
|
|
13
|
-
},
|
|
14
|
-
|
|
12
|
+
M = a;
|
|
13
|
+
}, K = (...a) => {
|
|
14
|
+
M && 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), z = W(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__ */ T(z.Provider, { value: { scopeName: t }, children: a });
|
|
25
|
+
}, H = () => k(z), B = [], D = /* @__PURE__ */ Symbol.for("react-shared-states.manager");
|
|
26
26
|
function J(a, t = () => null) {
|
|
27
|
-
const
|
|
28
|
-
return
|
|
27
|
+
const s = globalThis;
|
|
28
|
+
return s[D] || (s[D] = {}), s[D][a] || (s[D][a] = new l(t)), s[D][a];
|
|
29
29
|
}
|
|
30
30
|
class l {
|
|
31
31
|
constructor(t = () => null) {
|
|
@@ -39,96 +39,98 @@ 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, s = () => null) {
|
|
43
|
+
return J(t, s);
|
|
44
44
|
}
|
|
45
|
-
addListener(t,
|
|
46
|
-
const i = l.prefix(t,
|
|
45
|
+
addListener(t, s, e) {
|
|
46
|
+
const i = l.prefix(t, s), 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, s, e) {
|
|
50
|
+
const i = l.prefix(t, s), r = this.data.get(i);
|
|
51
|
+
r && (r.listeners = r.listeners.filter((n) => n !== e));
|
|
52
52
|
}
|
|
53
|
-
callListeners(t,
|
|
54
|
-
const e = l.prefix(t,
|
|
53
|
+
callListeners(t, s) {
|
|
54
|
+
const e = l.prefix(t, s), 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, s, e, i = !1) {
|
|
58
|
+
const r = l.prefix(t, s);
|
|
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, s, 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, s, !0), this.defaultValue = () => s, r;
|
|
72
72
|
}
|
|
73
73
|
initStatic(t) {
|
|
74
|
-
const { key:
|
|
75
|
-
this.init(
|
|
74
|
+
const { key: s, prefix: e } = t;
|
|
75
|
+
this.init(s, e, this.defaultValue(), !0);
|
|
76
76
|
}
|
|
77
|
-
clearAll(t = !1,
|
|
77
|
+
clearAll(t = !1, s = !1) {
|
|
78
78
|
this.data.forEach((e, i) => {
|
|
79
|
-
const [r,
|
|
80
|
-
this.clear(
|
|
79
|
+
const [r, n] = l.extractPrefix(i);
|
|
80
|
+
this.clear(n, r, t, s);
|
|
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, s, e = !1, i = !1) {
|
|
84
|
+
const r = l.prefix(t, s);
|
|
85
|
+
e || this.callListeners(t, s);
|
|
86
|
+
const n = this.data.get(r);
|
|
87
|
+
if (n && (this.data.delete(r), n.isStatic && !i)) {
|
|
88
|
+
const o = B.find((c) => c.key === t && c.prefix === s);
|
|
89
89
|
o && this.initStatic(o);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
get(t,
|
|
93
|
-
let e = this.has(t,
|
|
92
|
+
get(t, s) {
|
|
93
|
+
let e = this.has(t, s);
|
|
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, s, e) {
|
|
98
|
+
const i = l.prefix(t, s), 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, s) {
|
|
102
|
+
return this.data.has(l.prefix(t, s)) ? l.prefix(t, s) : this.data.has(l.prefix(t, "_global")) ? l.prefix(t, "_global") : void 0;
|
|
103
103
|
}
|
|
104
|
-
static prefix(t,
|
|
104
|
+
static prefix(t, s) {
|
|
105
105
|
if (t.includes("//")) throw new Error("key cannot contain '//'");
|
|
106
|
-
return `${
|
|
106
|
+
return `${s}//${t}`;
|
|
107
107
|
}
|
|
108
108
|
static extractPrefix(t) {
|
|
109
|
-
const
|
|
110
|
-
return [
|
|
109
|
+
const s = t.split("//");
|
|
110
|
+
return [s[0], s.slice(1).join("//")];
|
|
111
111
|
}
|
|
112
|
-
useEffect(t,
|
|
112
|
+
useEffect(t, s, e = null) {
|
|
113
113
|
P(() => () => {
|
|
114
|
-
e?.();
|
|
115
|
-
|
|
114
|
+
e?.(), K(`[${l.prefix(t, s)}]`, "unmount effect");
|
|
115
|
+
const i = this.get(t, s);
|
|
116
|
+
i && i.listeners?.length === 0 && this.clear(t, s);
|
|
117
|
+
}, []);
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
|
-
class
|
|
120
|
+
class $ {
|
|
119
121
|
constructor(t) {
|
|
120
122
|
this.sharedData = t, this._uniqueId = q();
|
|
121
123
|
}
|
|
122
124
|
_uniqueId;
|
|
123
|
-
_get(t,
|
|
124
|
-
let e, i =
|
|
125
|
+
_get(t, s) {
|
|
126
|
+
let e, i = s;
|
|
125
127
|
if (typeof t != "string") {
|
|
126
128
|
const { key: o, prefix: c } = t;
|
|
127
129
|
e = o, i = c;
|
|
128
130
|
} else
|
|
129
131
|
e = E(t);
|
|
130
|
-
const r = i || "_global",
|
|
131
|
-
return
|
|
132
|
+
const r = i || "_global", n = this.sharedData.get(e, r);
|
|
133
|
+
return n ? { value: n.value, key: e, prefix: r } : {
|
|
132
134
|
key: e,
|
|
133
135
|
prefix: r,
|
|
134
136
|
value: void 0
|
|
@@ -140,23 +142,23 @@ class M {
|
|
|
140
142
|
managerId: this.sharedData.__get__uniqueId()
|
|
141
143
|
};
|
|
142
144
|
}
|
|
143
|
-
get(t,
|
|
144
|
-
return this._get(t,
|
|
145
|
+
get(t, s) {
|
|
146
|
+
return this._get(t, s).value;
|
|
145
147
|
}
|
|
146
|
-
set(t,
|
|
148
|
+
set(t, s, e) {
|
|
147
149
|
let i, r = e;
|
|
148
150
|
if (typeof t != "string") {
|
|
149
151
|
const { key: o, prefix: c } = t;
|
|
150
152
|
i = o, r = c;
|
|
151
153
|
} else
|
|
152
154
|
i = E(t);
|
|
153
|
-
const
|
|
154
|
-
this.sharedData.init(i,
|
|
155
|
+
const n = r || "_global";
|
|
156
|
+
this.sharedData.init(i, n, s), this.sharedData.setValue(i, n, s), this.sharedData.callListeners(i, n);
|
|
155
157
|
}
|
|
156
|
-
update(t,
|
|
158
|
+
update(t, s, e) {
|
|
157
159
|
const i = this._get(t, e);
|
|
158
160
|
if (i) {
|
|
159
|
-
const r =
|
|
161
|
+
const r = s(i.value);
|
|
160
162
|
this.set(i.key, r, i.prefix);
|
|
161
163
|
}
|
|
162
164
|
}
|
|
@@ -166,38 +168,38 @@ class M {
|
|
|
166
168
|
}
|
|
167
169
|
// noinspection JSUnusedGlobalSymbols
|
|
168
170
|
clearScope(t) {
|
|
169
|
-
const
|
|
171
|
+
const s = t || "_global";
|
|
170
172
|
this.sharedData.data.forEach((e, i) => {
|
|
171
|
-
const [r,
|
|
172
|
-
r ===
|
|
173
|
+
const [r, n] = l.extractPrefix(i);
|
|
174
|
+
r === s && (this.sharedData.clear(n, r), this.sharedData.callListeners(n, r));
|
|
173
175
|
});
|
|
174
176
|
}
|
|
175
177
|
// noinspection JSUnusedGlobalSymbols
|
|
176
178
|
resolve(t) {
|
|
177
|
-
const { key:
|
|
178
|
-
return this.get(
|
|
179
|
+
const { key: s, prefix: e } = t;
|
|
180
|
+
return this.get(s, e);
|
|
179
181
|
}
|
|
180
|
-
clear(t,
|
|
182
|
+
clear(t, s) {
|
|
181
183
|
let e, i;
|
|
182
|
-
typeof t == "string" ? (e = t, i =
|
|
184
|
+
typeof t == "string" ? (e = t, i = s || "_global") : (e = t.key, i = t.prefix), this.sharedData.clear(e, i);
|
|
183
185
|
}
|
|
184
186
|
// noinspection JSUnusedGlobalSymbols
|
|
185
|
-
has(t,
|
|
186
|
-
const e =
|
|
187
|
+
has(t, s = "_global") {
|
|
188
|
+
const e = s || "_global";
|
|
187
189
|
return !!this.sharedData.has(t, e);
|
|
188
190
|
}
|
|
189
191
|
// noinspection JSUnusedGlobalSymbols
|
|
190
192
|
getAll() {
|
|
191
193
|
const t = {};
|
|
192
|
-
return this.sharedData.data.forEach((
|
|
194
|
+
return this.sharedData.data.forEach((s, e) => {
|
|
193
195
|
const [i, r] = l.extractPrefix(e);
|
|
194
|
-
t[i] = t[i] || {}, t[i][r] =
|
|
196
|
+
t[i] = t[i] || {}, t[i][r] = s.value;
|
|
195
197
|
}), t;
|
|
196
198
|
}
|
|
197
|
-
subscribe(t,
|
|
199
|
+
subscribe(t, s, e) {
|
|
198
200
|
let i, r;
|
|
199
|
-
return typeof t == "string" ? (i = t, r = e || "_global") : (i = t.key, r = t.prefix), this.sharedData.addListener(i, r,
|
|
200
|
-
this.sharedData.removeListener(i, r,
|
|
201
|
+
return typeof t == "string" ? (i = t, r = e || "_global") : (i = t.key, r = t.prefix), this.sharedData.addListener(i, r, s), () => {
|
|
202
|
+
this.sharedData.removeListener(i, r, s);
|
|
201
203
|
};
|
|
202
204
|
}
|
|
203
205
|
}
|
|
@@ -214,56 +216,56 @@ var F, G;
|
|
|
214
216
|
function U() {
|
|
215
217
|
if (G) return F;
|
|
216
218
|
G = 1;
|
|
217
|
-
var a = typeof Element < "u", t = typeof Map == "function",
|
|
218
|
-
function i(r,
|
|
219
|
-
if (r ===
|
|
220
|
-
if (r &&
|
|
221
|
-
if (r.constructor !==
|
|
219
|
+
var a = typeof Element < "u", t = typeof Map == "function", s = typeof Set == "function", e = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
|
|
220
|
+
function i(r, n) {
|
|
221
|
+
if (r === n) return !0;
|
|
222
|
+
if (r && n && typeof r == "object" && typeof n == "object") {
|
|
223
|
+
if (r.constructor !== n.constructor) return !1;
|
|
222
224
|
var o, c, d;
|
|
223
225
|
if (Array.isArray(r)) {
|
|
224
|
-
if (o = r.length, o !=
|
|
226
|
+
if (o = r.length, o != n.length) return !1;
|
|
225
227
|
for (c = o; c-- !== 0; )
|
|
226
|
-
if (!i(r[c],
|
|
228
|
+
if (!i(r[c], n[c])) return !1;
|
|
227
229
|
return !0;
|
|
228
230
|
}
|
|
229
231
|
var f;
|
|
230
|
-
if (t && r instanceof Map &&
|
|
231
|
-
if (r.size !==
|
|
232
|
+
if (t && r instanceof Map && n instanceof Map) {
|
|
233
|
+
if (r.size !== n.size) return !1;
|
|
232
234
|
for (f = r.entries(); !(c = f.next()).done; )
|
|
233
|
-
if (!
|
|
235
|
+
if (!n.has(c.value[0])) return !1;
|
|
234
236
|
for (f = r.entries(); !(c = f.next()).done; )
|
|
235
|
-
if (!i(c.value[1],
|
|
237
|
+
if (!i(c.value[1], n.get(c.value[0]))) return !1;
|
|
236
238
|
return !0;
|
|
237
239
|
}
|
|
238
|
-
if (
|
|
239
|
-
if (r.size !==
|
|
240
|
+
if (s && r instanceof Set && n instanceof Set) {
|
|
241
|
+
if (r.size !== n.size) return !1;
|
|
240
242
|
for (f = r.entries(); !(c = f.next()).done; )
|
|
241
|
-
if (!
|
|
243
|
+
if (!n.has(c.value[0])) return !1;
|
|
242
244
|
return !0;
|
|
243
245
|
}
|
|
244
|
-
if (e && ArrayBuffer.isView(r) && ArrayBuffer.isView(
|
|
245
|
-
if (o = r.length, o !=
|
|
246
|
+
if (e && ArrayBuffer.isView(r) && ArrayBuffer.isView(n)) {
|
|
247
|
+
if (o = r.length, o != n.length) return !1;
|
|
246
248
|
for (c = o; c-- !== 0; )
|
|
247
|
-
if (r[c] !==
|
|
249
|
+
if (r[c] !== n[c]) return !1;
|
|
248
250
|
return !0;
|
|
249
251
|
}
|
|
250
|
-
if (r.constructor === RegExp) return r.source ===
|
|
251
|
-
if (r.valueOf !== Object.prototype.valueOf && typeof r.valueOf == "function" && typeof
|
|
252
|
-
if (r.toString !== Object.prototype.toString && typeof r.toString == "function" && typeof
|
|
253
|
-
if (d = Object.keys(r), o = d.length, o !== Object.keys(
|
|
252
|
+
if (r.constructor === RegExp) return r.source === n.source && r.flags === n.flags;
|
|
253
|
+
if (r.valueOf !== Object.prototype.valueOf && typeof r.valueOf == "function" && typeof n.valueOf == "function") return r.valueOf() === n.valueOf();
|
|
254
|
+
if (r.toString !== Object.prototype.toString && typeof r.toString == "function" && typeof n.toString == "function") return r.toString() === n.toString();
|
|
255
|
+
if (d = Object.keys(r), o = d.length, o !== Object.keys(n).length) return !1;
|
|
254
256
|
for (c = o; c-- !== 0; )
|
|
255
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
257
|
+
if (!Object.prototype.hasOwnProperty.call(n, d[c])) return !1;
|
|
256
258
|
if (a && r instanceof Element) return !1;
|
|
257
259
|
for (c = o; c-- !== 0; )
|
|
258
|
-
if (!((d[c] === "_owner" || d[c] === "__v" || d[c] === "__o") && r.$$typeof) && !i(r[d[c]],
|
|
260
|
+
if (!((d[c] === "_owner" || d[c] === "__v" || d[c] === "__o") && r.$$typeof) && !i(r[d[c]], n[d[c]]))
|
|
259
261
|
return !1;
|
|
260
262
|
return !0;
|
|
261
263
|
}
|
|
262
|
-
return r !== r &&
|
|
264
|
+
return r !== r && n !== n;
|
|
263
265
|
}
|
|
264
|
-
return F = function(
|
|
266
|
+
return F = function(n, o) {
|
|
265
267
|
try {
|
|
266
|
-
return i(
|
|
268
|
+
return i(n, o);
|
|
267
269
|
} catch (c) {
|
|
268
270
|
if ((c.message || "").match(/stack|recursion/i))
|
|
269
271
|
return console.warn("react-fast-compare cannot handle circular refs"), !1;
|
|
@@ -272,76 +274,76 @@ function U() {
|
|
|
272
274
|
}, F;
|
|
273
275
|
}
|
|
274
276
|
var X = U();
|
|
275
|
-
const
|
|
276
|
-
function ne(a, t,
|
|
277
|
-
let e, i, r =
|
|
277
|
+
const V = /* @__PURE__ */ Q(X), h = l.getInstance("sharedStatesManager"), re = new $(h), se = (a, t) => h.createStatic({ initialValue: a }, a, t);
|
|
278
|
+
function ne(a, t, s) {
|
|
279
|
+
let e, i, r = s;
|
|
278
280
|
if (typeof a != "string") {
|
|
279
281
|
const { key: u, initialValue: v, prefix: x } = a;
|
|
280
282
|
e = u, i = v, r = x;
|
|
281
283
|
} else
|
|
282
284
|
e = E(a), i = t;
|
|
283
|
-
const { prefix:
|
|
284
|
-
h.init(e,
|
|
285
|
-
const o = b(() => (u) => (h.init(e,
|
|
286
|
-
h.removeListener(e,
|
|
287
|
-
}), [e,
|
|
288
|
-
const v = typeof u == "function" ? u(h.get(e,
|
|
289
|
-
|
|
285
|
+
const { prefix: n } = j(r);
|
|
286
|
+
h.init(e, n, i);
|
|
287
|
+
const o = b(() => (u) => (h.init(e, n, t), h.addListener(e, n, u), () => {
|
|
288
|
+
h.removeListener(e, n, u);
|
|
289
|
+
}), [e, n, t]), c = b(() => () => h.get(e, n)?.value, [e, n]), d = O(o, c), f = (u) => {
|
|
290
|
+
const v = typeof u == "function" ? u(h.get(e, n)?.value) : u;
|
|
291
|
+
V(v, d) || (h.setValue(e, n, v), h.callListeners(e, n));
|
|
290
292
|
};
|
|
291
|
-
return h.useEffect(e,
|
|
293
|
+
return h.useEffect(e, n), [
|
|
292
294
|
d,
|
|
293
295
|
f
|
|
294
296
|
];
|
|
295
297
|
}
|
|
296
|
-
function ie(a, t,
|
|
297
|
-
let e, i =
|
|
298
|
+
function ie(a, t, s) {
|
|
299
|
+
let e, i = s;
|
|
298
300
|
if (typeof a != "string") {
|
|
299
301
|
const { key: f, prefix: u } = a;
|
|
300
302
|
e = f, i = u;
|
|
301
303
|
} else
|
|
302
304
|
e = E(a);
|
|
303
|
-
const { prefix: r } = j(i),
|
|
305
|
+
const { prefix: r } = j(i), n = Y(void 0), o = b(() => (f) => (h.init(e, r, void 0), h.addListener(e, r, f), () => {
|
|
304
306
|
h.removeListener(e, r, f);
|
|
305
307
|
}), [e, r]), c = b(() => () => {
|
|
306
308
|
const f = h.get(e, r)?.value, u = t(f);
|
|
307
|
-
return
|
|
309
|
+
return V(n.current, u) ? n.current : (n.current = u, u);
|
|
308
310
|
}, [e, r, t]), d = O(o, c);
|
|
309
311
|
return h.useEffect(e, r), d;
|
|
310
312
|
}
|
|
311
|
-
const y = l.getInstance("SharedFunctionValue"), I = new
|
|
313
|
+
const y = l.getInstance("SharedFunctionValue"), I = new $(y), A = {
|
|
312
314
|
results: void 0,
|
|
313
315
|
isLoading: !1,
|
|
314
316
|
error: void 0
|
|
315
317
|
}, ae = (a, t) => y.createStatic({ fn: a }, A, t);
|
|
316
|
-
function oe(a, t,
|
|
317
|
-
let e, i, r =
|
|
318
|
+
function oe(a, t, s) {
|
|
319
|
+
let e, i, r = s;
|
|
318
320
|
if (typeof a != "string") {
|
|
319
321
|
const { key: u, fn: v, prefix: x } = a;
|
|
320
322
|
e = u, i = v, r = x;
|
|
321
323
|
} else
|
|
322
324
|
e = E(a), i = t;
|
|
323
|
-
const { prefix:
|
|
324
|
-
y.init(e,
|
|
325
|
+
const { prefix: n } = j(r);
|
|
326
|
+
y.init(e, n, A);
|
|
325
327
|
const o = b(
|
|
326
|
-
() => (u) => (y.init(e,
|
|
327
|
-
y.removeListener(e,
|
|
328
|
+
() => (u) => (y.init(e, n, A), y.addListener(e, n, u), () => {
|
|
329
|
+
y.removeListener(e, n, u);
|
|
328
330
|
}),
|
|
329
|
-
[e,
|
|
331
|
+
[e, n]
|
|
330
332
|
), c = b(
|
|
331
|
-
() => () => y.get(e,
|
|
332
|
-
[e,
|
|
333
|
+
() => () => y.get(e, n).value,
|
|
334
|
+
[e, n]
|
|
333
335
|
), d = O(o, c), f = async (u, ...v) => {
|
|
334
|
-
const x = y.get(e,
|
|
336
|
+
const x = y.get(e, n);
|
|
335
337
|
if (!u && (x.value.isLoading || x.value.results !== void 0)) return x.value;
|
|
336
|
-
I.update(e, (S) => ({ ...S, isLoading: !0, error: void 0 }),
|
|
338
|
+
I.update(e, (S) => ({ ...S, isLoading: !0, error: void 0 }), n);
|
|
337
339
|
try {
|
|
338
340
|
const S = await i(...v);
|
|
339
|
-
I.set(e, { results: S, isLoading: !1, error: void 0 },
|
|
341
|
+
I.set(e, { results: S, isLoading: !1, error: void 0 }, n);
|
|
340
342
|
} catch (S) {
|
|
341
|
-
I.update(e, (p) => ({ ...p, isLoading: !1, error: S }),
|
|
343
|
+
I.update(e, (p) => ({ ...p, isLoading: !1, error: S }), n);
|
|
342
344
|
}
|
|
343
345
|
};
|
|
344
|
-
return y.useEffect(e,
|
|
346
|
+
return y.useEffect(e, n), {
|
|
345
347
|
state: d,
|
|
346
348
|
trigger: (...u) => {
|
|
347
349
|
f(!1, ...u);
|
|
@@ -350,32 +352,32 @@ function oe(a, t, n) {
|
|
|
350
352
|
f(!0, ...u);
|
|
351
353
|
},
|
|
352
354
|
clear: () => {
|
|
353
|
-
I.set(e, A,
|
|
355
|
+
I.set(e, A, n);
|
|
354
356
|
}
|
|
355
357
|
};
|
|
356
358
|
}
|
|
357
|
-
const g = l.getInstance("SharedSubscription"), L = new
|
|
359
|
+
const g = l.getInstance("SharedSubscription"), L = new $(g), R = {
|
|
358
360
|
data: void 0,
|
|
359
361
|
isLoading: !1,
|
|
360
362
|
error: void 0,
|
|
361
363
|
subscribed: !1,
|
|
362
364
|
unsubscribe: void 0
|
|
363
|
-
}, ce = (a, t,
|
|
365
|
+
}, ce = (a, t, s) => g.createStatic({ subscriber: a }, { ...R, data: t?.initialValue }, s);
|
|
364
366
|
async function C(a, t) {
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
+
const s = g.get(a, t);
|
|
368
|
+
s?.value.unsubscribe && (s.value.unsubscribe(), L.update(a, (e) => ({ ...e, unsubscribe: void 0, subscribed: !1 }), t));
|
|
367
369
|
}
|
|
368
|
-
function ue(a, t,
|
|
369
|
-
let e, i, r =
|
|
370
|
+
function ue(a, t, s) {
|
|
371
|
+
let e, i, r = s, n = !1;
|
|
370
372
|
if (typeof a != "string") {
|
|
371
373
|
const { key: p, subscriber: m, prefix: _, triggerImmediately: w } = a;
|
|
372
|
-
e = p, i = m, r = _,
|
|
374
|
+
e = p, i = m, r = _, n = w ?? !1;
|
|
373
375
|
} else
|
|
374
376
|
e = E(a), i = t;
|
|
375
377
|
const { prefix: o } = j(r);
|
|
376
|
-
g.init(e, o,
|
|
378
|
+
g.init(e, o, R);
|
|
377
379
|
const c = b(
|
|
378
|
-
() => (p) => (g.init(e, o,
|
|
380
|
+
() => (p) => (g.init(e, o, R), g.addListener(e, o, p), () => {
|
|
379
381
|
g.removeListener(e, o, p);
|
|
380
382
|
}),
|
|
381
383
|
[e, o]
|
|
@@ -391,7 +393,7 @@ function ue(a, t, n) {
|
|
|
391
393
|
}, S = async (p) => {
|
|
392
394
|
const m = g.get(e, o);
|
|
393
395
|
if (p && await C(e, o), m.value.subscribed && !p) return m.value;
|
|
394
|
-
|
|
396
|
+
K("triggered !!"), L.update(e, (_) => ({ ..._, isLoading: !0, error: void 0 }), o);
|
|
395
397
|
try {
|
|
396
398
|
const _ = await i(u, v, x);
|
|
397
399
|
L.update(e, (w) => ({ ...w, unsubscribe: _, subscribed: !0, isLoading: !1 }), o);
|
|
@@ -400,9 +402,9 @@ function ue(a, t, n) {
|
|
|
400
402
|
}
|
|
401
403
|
};
|
|
402
404
|
return P(() => () => {
|
|
403
|
-
|
|
405
|
+
K(`[${l.prefix(e, o)}]`, "unmount effect2"), g.get(e, o)?.listeners.length === 0 && C(e, o);
|
|
404
406
|
}, [e, o]), g.useEffect(e, o), P(() => {
|
|
405
|
-
|
|
407
|
+
n && S(!1);
|
|
406
408
|
}, []), {
|
|
407
409
|
state: f,
|
|
408
410
|
trigger: () => {
|
|
@@ -421,7 +423,7 @@ export {
|
|
|
421
423
|
ae as createSharedFunction,
|
|
422
424
|
se as createSharedState,
|
|
423
425
|
ce as createSharedSubscription,
|
|
424
|
-
|
|
426
|
+
M as isDevMode,
|
|
425
427
|
ee as setDevMode,
|
|
426
428
|
I as sharedFunctionsApi,
|
|
427
429
|
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.6-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,h){"use strict";f.isDevMode=!1;const z=a=>{f.isDevMode=a},O=(...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=h.createContext(void 0),V=({children:a,scopeName:t})=>{if(t&&t.includes("//"))throw new Error("scopeName cannot contain '//'");return t||(t=h.useMemo(()=>q(),[])),D.jsx(T.Provider,{value:{scopeName:t},children:a})},W=()=>h.useContext(T),$=[],A=Symbol.for("react-shared-states.manager");function k(a,t=()=>null){const s=globalThis;return s[A]||(s[A]={}),s[A][a]||(s[A][a]=new l(t)),s[A][a]}class l{constructor(t=()=>null){this.defaultValue=t,this._uniqueId=q()}data=new Map;_uniqueId;__get__uniqueId(){return this._uniqueId}static getInstance(t,s=()=>null){return k(t,s)}addListener(t,s,e){const i=l.prefix(t,s),r=this.data.get(i);r&&r.listeners.push(e)}removeListener(t,s,e){const i=l.prefix(t,s),r=this.data.get(i);r&&(r.listeners=r.listeners.filter(n=>n!==e))}callListeners(t,s){const e=l.prefix(t,s),i=this.data.get(e);i&&i.listeners.forEach(r=>r())}init(t,s,e,i=!1){const r=l.prefix(t,s);this.data.has(r)||this.data.set(r,{value:e,isStatic:i||void 0,listeners:[]})}createStatic(t,s,e){const i=e??"_global",r={key:q(),prefix:i,...t};return $.push(r),this.init(r.key,r.prefix,s,!0),this.defaultValue=()=>s,r}initStatic(t){const{key:s,prefix:e}=t;this.init(s,e,this.defaultValue(),!0)}clearAll(t=!1,s=!1){this.data.forEach((e,i)=>{const[r,n]=l.extractPrefix(i);this.clear(n,r,t,s)})}clear(t,s,e=!1,i=!1){const r=l.prefix(t,s);e||this.callListeners(t,s);const n=this.data.get(r);if(n&&(this.data.delete(r),n.isStatic&&!i)){const o=$.find(u=>u.key===t&&u.prefix===s);o&&this.initStatic(o)}}get(t,s){let e=this.has(t,s);if(e)return this.data.get(e)}setValue(t,s,e){const i=l.prefix(t,s),r=this.data.get(i);r&&(r.value=e,this.data.set(i,r))}has(t,s){return this.data.has(l.prefix(t,s))?l.prefix(t,s):this.data.has(l.prefix(t,"_global"))?l.prefix(t,"_global"):void 0}static prefix(t,s){if(t.includes("//"))throw new Error("key cannot contain '//'");return`${s}//${t}`}static extractPrefix(t){const s=t.split("//");return[s[0],s.slice(1).join("//")]}useEffect(t,s,e=null){h.useEffect(()=>()=>{e?.(),O(`[${l.prefix(t,s)}]`,"unmount effect");const i=this.get(t,s);i&&i.listeners?.length===0&&this.clear(t,s)},[])}}class P{constructor(t){this.sharedData=t,this._uniqueId=q()}_uniqueId;_get(t,s){let e,i=s;if(typeof t!="string"){const{key:o,prefix:u}=t;e=o,i=u}else e=w(t);const r=i||"_global",n=this.sharedData.get(e,r);return n?{value:n.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,s){return this._get(t,s).value}set(t,s,e){let i,r=e;if(typeof t!="string"){const{key:o,prefix:u}=t;i=o,r=u}else i=w(t);const n=r||"_global";this.sharedData.init(i,n,s),this.sharedData.setValue(i,n,s),this.sharedData.callListeners(i,n)}update(t,s,e){const i=this._get(t,e);if(i){const r=s(i.value);this.set(i.key,r,i.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(t){const s=t||"_global";this.sharedData.data.forEach((e,i)=>{const[r,n]=l.extractPrefix(i);r===s&&(this.sharedData.clear(n,r),this.sharedData.callListeners(n,r))})}resolve(t){const{key:s,prefix:e}=t;return this.get(s,e)}clear(t,s){let e,i;typeof t=="string"?(e=t,i=s||"_global"):(e=t.key,i=t.prefix),this.sharedData.clear(e,i)}has(t,s="_global"){const e=s||"_global";return!!this.sharedData.has(t,e)}getAll(){const t={};return this.sharedData.data.forEach((s,e)=>{const[i,r]=l.extractPrefix(e);t[i]=t[i]||{},t[i][r]=s.value}),t}subscribe(t,s,e){let i,r;return typeof t=="string"?(i=t,r=e||"_global"):(i=t.key,r=t.prefix),this.sharedData.addListener(i,r,s),()=>{this.sharedData.removeListener(i,r,s)}}}const I=a=>{const t=W();return{prefix:a??t?.scopeName??"_global"}};function Y(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var C,B;function H(){if(B)return C;B=1;var a=typeof Element<"u",t=typeof Map=="function",s=typeof Set=="function",e=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function i(r,n){if(r===n)return!0;if(r&&n&&typeof r=="object"&&typeof n=="object"){if(r.constructor!==n.constructor)return!1;var o,u,p;if(Array.isArray(r)){if(o=r.length,o!=n.length)return!1;for(u=o;u--!==0;)if(!i(r[u],n[u]))return!1;return!0}var d;if(t&&r instanceof Map&&n instanceof Map){if(r.size!==n.size)return!1;for(d=r.entries();!(u=d.next()).done;)if(!n.has(u.value[0]))return!1;for(d=r.entries();!(u=d.next()).done;)if(!i(u.value[1],n.get(u.value[0])))return!1;return!0}if(s&&r instanceof Set&&n instanceof Set){if(r.size!==n.size)return!1;for(d=r.entries();!(u=d.next()).done;)if(!n.has(u.value[0]))return!1;return!0}if(e&&ArrayBuffer.isView(r)&&ArrayBuffer.isView(n)){if(o=r.length,o!=n.length)return!1;for(u=o;u--!==0;)if(r[u]!==n[u])return!1;return!0}if(r.constructor===RegExp)return r.source===n.source&&r.flags===n.flags;if(r.valueOf!==Object.prototype.valueOf&&typeof r.valueOf=="function"&&typeof n.valueOf=="function")return r.valueOf()===n.valueOf();if(r.toString!==Object.prototype.toString&&typeof r.toString=="function"&&typeof n.toString=="function")return r.toString()===n.toString();if(p=Object.keys(r),o=p.length,o!==Object.keys(n).length)return!1;for(u=o;u--!==0;)if(!Object.prototype.hasOwnProperty.call(n,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]],n[p[u]]))return!1;return!0}return r!==r&&n!==n}return C=function(n,o){try{return i(n,o)}catch(u){if((u.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw u}},C}var J=H();const G=Y(J),g=l.getInstance("sharedStatesManager"),Q=new P(g),U=(a,t)=>g.createStatic({initialValue:a},a,t);function X(a,t,s){let e,i,r=s;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:n}=I(r);g.init(e,n,i);const o=h.useMemo(()=>c=>(g.init(e,n,t),g.addListener(e,n,c),()=>{g.removeListener(e,n,c)}),[e,n,t]),u=h.useMemo(()=>()=>g.get(e,n)?.value,[e,n]),p=h.useSyncExternalStore(o,u),d=c=>{const y=typeof c=="function"?c(g.get(e,n)?.value):c;G(y,p)||(g.setValue(e,n,y),g.callListeners(e,n))};return g.useEffect(e,n),[p,d]}function Z(a,t,s){let e,i=s;if(typeof a!="string"){const{key:d,prefix:c}=a;e=d,i=c}else e=w(a);const{prefix:r}=I(i),n=h.useRef(void 0),o=h.useMemo(()=>d=>(g.init(e,r,void 0),g.addListener(e,r,d),()=>{g.removeListener(e,r,d)}),[e,r]),u=h.useMemo(()=>()=>{const d=g.get(e,r)?.value,c=t(d);return G(n.current,c)?n.current:(n.current=c,c)},[e,r,t]),p=h.useSyncExternalStore(o,u);return g.useEffect(e,r),p}const x=l.getInstance("SharedFunctionValue"),M=new P(x),F={results:void 0,isLoading:!1,error:void 0},N=(a,t)=>x.createStatic({fn:a},F,t);function ee(a,t,s){let e,i,r=s;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:n}=I(r);x.init(e,n,F);const o=h.useMemo(()=>c=>(x.init(e,n,F),x.addListener(e,n,c),()=>{x.removeListener(e,n,c)}),[e,n]),u=h.useMemo(()=>()=>x.get(e,n).value,[e,n]),p=h.useSyncExternalStore(o,u),d=async(c,...y)=>{const m=x.get(e,n);if(!c&&(m.value.isLoading||m.value.results!==void 0))return m.value;M.update(e,b=>({...b,isLoading:!0,error:void 0}),n);try{const b=await i(...y);M.set(e,{results:b,isLoading:!1,error:void 0},n)}catch(b){M.update(e,S=>({...S,isLoading:!1,error:b}),n)}};return x.useEffect(e,n),{state:p,trigger:(...c)=>{d(!1,...c)},forceTrigger:(...c)=>{d(!0,...c)},clear:()=>{M.set(e,F,n)}}}const v=l.getInstance("SharedSubscription"),E=new P(v),R={data:void 0,isLoading:!1,error:void 0,subscribed:!1,unsubscribe:void 0},te=(a,t,s)=>v.createStatic({subscriber:a},{...R,data:t?.initialValue},s);async function K(a,t){const s=v.get(a,t);s?.value.unsubscribe&&(s.value.unsubscribe(),E.update(a,e=>({...e,unsubscribe:void 0,subscribed:!1}),t))}function re(a,t,s){let e,i,r=s,n=!1;if(typeof a!="string"){const{key:S,subscriber:_,prefix:L,triggerImmediately:j}=a;e=S,i=_,r=L,n=j??!1}else e=w(a),i=t;const{prefix:o}=I(r);v.init(e,o,R);const u=h.useMemo(()=>S=>(v.init(e,o,R),v.addListener(e,o,S),()=>{v.removeListener(e,o,S)}),[e,o]),p=h.useMemo(()=>()=>v.get(e,o).value,[e,o]),d=h.useSyncExternalStore(u,p),c=S=>{E.update(e,_=>({..._,data:S}),o)},y=S=>{E.update(e,_=>({..._,isLoading:!1,data:void 0,error:S}),o)},m=()=>{E.update(e,S=>({...S,isLoading:!1}),o)},b=async S=>{const _=v.get(e,o);if(S&&await K(e,o),_.value.subscribed&&!S)return _.value;O("triggered !!"),E.update(e,L=>({...L,isLoading:!0,error:void 0}),o);try{const L=await i(c,y,m);E.update(e,j=>({...j,unsubscribe:L,subscribed:!0,isLoading:!1}),o)}catch(L){E.update(e,j=>({...j,isLoading:!1,error:L}),o)}};return h.useEffect(()=>()=>{O(`[${l.prefix(e,o)}]`,"unmount effect2"),v.get(e,o)?.listeners.length===0&&K(e,o)},[e,o]),v.useEffect(e,o),h.useEffect(()=>{n&&b(!1)},[]),{state:d,trigger:()=>{b(!1)},forceTrigger:()=>{b(!0)},unsubscribe:()=>{K(e,o)}}}f.SharedStatesProvider=V,f.createSharedFunction=N,f.createSharedState=U,f.createSharedSubscription=te,f.setDevMode=z,f.sharedFunctionsApi=M,f.sharedStatesApi=Q,f.sharedSubscriptionsApi=E,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