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