react-shared-states 2.1.2 → 2.1.4-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,8 +3,12 @@ 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
- static INSTANCES: Map<string, SharedValuesManager<any>>;
6
+ private readonly _uniqueId;
7
7
  constructor(defaultValue?: () => T);
8
+ /**
9
+ * @internal
10
+ */
11
+ __get__uniqueId(): string;
8
12
  static getInstance<T>(instanceKey: string, defaultValue?: () => T): SharedValuesManager<T>;
9
13
  addListener(key: string, prefix: Prefix, listener: AFunction): void;
10
14
  removeListener(key: string, prefix: Prefix, listener: AFunction): void;
@@ -23,8 +27,13 @@ export declare class SharedValuesManager<T> {
23
27
  }
24
28
  export declare class SharedValuesApi<T> {
25
29
  protected sharedData: SharedValuesManager<T>;
30
+ private readonly _uniqueId;
26
31
  constructor(sharedData: SharedValuesManager<T>);
27
32
  private _get;
33
+ __log_instance_id(): {
34
+ apiId: string;
35
+ managerId: string;
36
+ };
28
37
  get(key: string, scopeName?: Prefix): T;
29
38
  get(sharedCreated: SharedCreated): T;
30
39
  set(key: string, value: T, scopeName?: Prefix): void;
package/dist/main.esm.js CHANGED
@@ -1,38 +1,44 @@
1
1
  /*!
2
- * react-shared-states v2.1.2
2
+ * react-shared-states v2.1.4-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 V } from "react/jsx-runtime";
9
- import { createContext as W, useMemo as b, useContext as Y, useEffect as I, useSyncExternalStore as j, useRef as k } from "react";
10
- let B = !1;
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
11
  const ee = (a) => {
12
- B = a;
12
+ M = a;
13
13
  }, K = (...a) => {
14
- B && console.log(
14
+ M && console.log(
15
15
  "%c[react-shared-states]",
16
16
  "color: #007acc; font-weight: bold",
17
17
  ...a
18
18
  );
19
- }, w = (a) => {
19
+ }, E = (a) => {
20
20
  if (!a) throw new Error("Value is empty");
21
21
  return a;
22
- }, G = () => Math.random().toString(36).substring(2, 15), z = W(void 0), te = ({ children: a, scopeName: t }) => {
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__ */ V(z.Provider, { value: { scopeName: t }, children: a });
25
- }, H = () => Y(z), $ = [], D = /* @__PURE__ */ Symbol.for("react-shared-states.manager");
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
26
  function J(a, t = () => null) {
27
27
  const s = globalThis;
28
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) {
32
- this.defaultValue = t;
32
+ this.defaultValue = t, this._uniqueId = q();
33
33
  }
34
34
  data = /* @__PURE__ */ new Map();
35
- static INSTANCES = /* @__PURE__ */ new Map();
35
+ _uniqueId;
36
+ /**
37
+ * @internal
38
+ */
39
+ __get__uniqueId() {
40
+ return this._uniqueId;
41
+ }
36
42
  static getInstance(t, s = () => null) {
37
43
  return J(t, s);
38
44
  }
@@ -58,11 +64,11 @@ class l {
58
64
  }
59
65
  createStatic(t, s, e) {
60
66
  const i = e ?? "_global", r = {
61
- key: G(),
67
+ key: q(),
62
68
  prefix: i,
63
69
  ...t
64
70
  };
65
- return $.push(r), this.init(r.key, r.prefix, s, !0), this.defaultValue = () => s, r;
71
+ return B.push(r), this.init(r.key, r.prefix, s, !0), this.defaultValue = () => s, r;
66
72
  }
67
73
  initStatic(t) {
68
74
  const { key: s, prefix: e } = t;
@@ -79,7 +85,7 @@ class l {
79
85
  e || this.callListeners(t, s);
80
86
  const n = this.data.get(r);
81
87
  if (n && (this.data.delete(r), n.isStatic && !i)) {
82
- const o = $.find((c) => c.key === t && c.prefix === s);
88
+ const o = B.find((c) => c.key === t && c.prefix === s);
83
89
  o && this.initStatic(o);
84
90
  }
85
91
  }
@@ -104,24 +110,25 @@ class l {
104
110
  return [s[0], s.slice(1).join("//")];
105
111
  }
106
112
  useEffect(t, s, e = null) {
107
- I(() => () => {
113
+ P(() => () => {
108
114
  e?.(), K(`[${l.prefix(t, s)}]`, "unmount effect");
109
115
  const i = this.get(t, s);
110
116
  i && i.listeners?.length === 0 && this.clear(t, s);
111
117
  }, [t, s]);
112
118
  }
113
119
  }
114
- class R {
120
+ class $ {
115
121
  constructor(t) {
116
- this.sharedData = t;
122
+ this.sharedData = t, this._uniqueId = q();
117
123
  }
124
+ _uniqueId;
118
125
  _get(t, s) {
119
126
  let e, i = s;
120
127
  if (typeof t != "string") {
121
128
  const { key: o, prefix: c } = t;
122
129
  e = o, i = c;
123
130
  } else
124
- e = w(t);
131
+ e = E(t);
125
132
  const r = i || "_global", n = this.sharedData.get(e, r);
126
133
  return n ? { value: n.value, key: e, prefix: r } : {
127
134
  key: e,
@@ -129,6 +136,12 @@ class R {
129
136
  value: void 0
130
137
  };
131
138
  }
139
+ __log_instance_id() {
140
+ return {
141
+ apiId: this._uniqueId,
142
+ managerId: this.sharedData.__get__uniqueId()
143
+ };
144
+ }
132
145
  get(t, s) {
133
146
  return this._get(t, s).value;
134
147
  }
@@ -138,7 +151,7 @@ class R {
138
151
  const { key: o, prefix: c } = t;
139
152
  i = o, r = c;
140
153
  } else
141
- i = w(t);
154
+ i = E(t);
142
155
  const n = r || "_global";
143
156
  this.sharedData.init(i, n, s), this.sharedData.setValue(i, n, s), this.sharedData.callListeners(i, n);
144
157
  }
@@ -190,19 +203,19 @@ class R {
190
203
  };
191
204
  }
192
205
  }
193
- const C = (a) => {
206
+ const j = (a) => {
194
207
  const t = H();
195
208
  return {
196
209
  prefix: a ?? t?.scopeName ?? "_global"
197
210
  };
198
211
  };
199
- function N(a) {
212
+ function Q(a) {
200
213
  return a && a.__esModule && Object.prototype.hasOwnProperty.call(a, "default") ? a.default : a;
201
214
  }
202
- var F, q;
203
- function Q() {
204
- if (q) return F;
205
- q = 1;
215
+ var F, G;
216
+ function U() {
217
+ if (G) return F;
218
+ G = 1;
206
219
  var a = typeof Element < "u", t = typeof Map == "function", s = typeof Set == "function", e = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
207
220
  function i(r, n) {
208
221
  if (r === n) return !0;
@@ -215,18 +228,18 @@ function Q() {
215
228
  if (!i(r[c], n[c])) return !1;
216
229
  return !0;
217
230
  }
218
- var u;
231
+ var f;
219
232
  if (t && r instanceof Map && n instanceof Map) {
220
233
  if (r.size !== n.size) return !1;
221
- for (u = r.entries(); !(c = u.next()).done; )
234
+ for (f = r.entries(); !(c = f.next()).done; )
222
235
  if (!n.has(c.value[0])) return !1;
223
- for (u = r.entries(); !(c = u.next()).done; )
236
+ for (f = r.entries(); !(c = f.next()).done; )
224
237
  if (!i(c.value[1], n.get(c.value[0]))) return !1;
225
238
  return !0;
226
239
  }
227
240
  if (s && r instanceof Set && n instanceof Set) {
228
241
  if (r.size !== n.size) return !1;
229
- for (u = r.entries(); !(c = u.next()).done; )
242
+ for (f = r.entries(); !(c = f.next()).done; )
230
243
  if (!n.has(c.value[0])) return !1;
231
244
  return !0;
232
245
  }
@@ -260,140 +273,140 @@ function Q() {
260
273
  }
261
274
  }, F;
262
275
  }
263
- var U = Q();
264
- const T = /* @__PURE__ */ N(U), h = l.getInstance("sharedStatesManager"), re = new R(h), se = (a, t) => h.createStatic({ initialValue: a }, a, t);
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);
265
278
  function ne(a, t, s) {
266
279
  let e, i, r = s;
267
280
  if (typeof a != "string") {
268
- const { key: f, initialValue: v, prefix: S } = a;
269
- e = f, i = v, r = S;
281
+ const { key: u, initialValue: v, prefix: S } = a;
282
+ e = u, i = v, r = S;
270
283
  } else
271
- e = w(a), i = t;
272
- const { prefix: n } = C(r);
284
+ e = E(a), i = t;
285
+ const { prefix: n } = j(r);
273
286
  h.init(e, n, i);
274
- const o = b(() => (f) => (h.init(e, n, t), h.addListener(e, n, f), () => {
275
- h.removeListener(e, n, f);
276
- }), [e, n, t]), c = b(() => () => h.get(e, n)?.value, [e, n]), d = j(o, c), u = (f) => {
277
- const v = typeof f == "function" ? f(h.get(e, n)?.value) : f;
278
- T(v, d) || (h.setValue(e, n, v), h.callListeners(e, n));
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));
279
292
  };
280
293
  return h.useEffect(e, n), [
281
294
  d,
282
- u
295
+ f
283
296
  ];
284
297
  }
285
298
  function ie(a, t, s) {
286
299
  let e, i = s;
287
300
  if (typeof a != "string") {
288
- const { key: u, prefix: f } = a;
289
- e = u, i = f;
301
+ const { key: f, prefix: u } = a;
302
+ e = f, i = u;
290
303
  } else
291
- e = w(a);
292
- const { prefix: r } = C(i), n = k(void 0), o = b(() => (u) => (h.init(e, r, void 0), h.addListener(e, r, u), () => {
293
- 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);
294
307
  }), [e, r]), c = b(() => () => {
295
- const u = h.get(e, r)?.value, f = t(u);
296
- return T(n.current, f) ? n.current : (n.current = f, f);
297
- }, [e, r, t]), d = j(o, 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);
298
311
  return h.useEffect(e, r), d;
299
312
  }
300
- const y = l.getInstance("SharedFunctionValue"), _ = new R(y), O = {
313
+ const y = l.getInstance("SharedFunctionValue"), I = new $(y), A = {
301
314
  results: void 0,
302
315
  isLoading: !1,
303
316
  error: void 0
304
- }, ae = (a, t) => y.createStatic({ fn: a }, O, t);
317
+ }, ae = (a, t) => y.createStatic({ fn: a }, A, t);
305
318
  function oe(a, t, s) {
306
319
  let e, i, r = s;
307
320
  if (typeof a != "string") {
308
- const { key: f, fn: v, prefix: S } = a;
309
- e = f, i = v, r = S;
321
+ const { key: u, fn: v, prefix: S } = a;
322
+ e = u, i = v, r = S;
310
323
  } else
311
- e = w(a), i = t;
312
- const { prefix: n } = C(r);
313
- y.init(e, n, O);
324
+ e = E(a), i = t;
325
+ const { prefix: n } = j(r);
326
+ y.init(e, n, A);
314
327
  const o = b(
315
- () => (f) => (y.init(e, n, O), y.addListener(e, n, f), () => {
316
- y.removeListener(e, n, f);
328
+ () => (u) => (y.init(e, n, A), y.addListener(e, n, u), () => {
329
+ y.removeListener(e, n, u);
317
330
  }),
318
331
  [e, n]
319
332
  ), c = b(
320
333
  () => () => y.get(e, n).value,
321
334
  [e, n]
322
- ), d = j(o, c), u = async (f, ...v) => {
335
+ ), d = O(o, c), f = async (u, ...v) => {
323
336
  const S = y.get(e, n);
324
- if (!f && (S.value.isLoading || S.value.results !== void 0)) return S.value;
325
- _.update(e, (x) => ({ ...x, isLoading: !0, error: void 0 }), 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);
326
339
  try {
327
340
  const x = await i(...v);
328
- _.set(e, { results: x, isLoading: !1, error: void 0 }, n);
341
+ I.set(e, { results: x, isLoading: !1, error: void 0 }, n);
329
342
  } catch (x) {
330
- _.update(e, (p) => ({ ...p, isLoading: !1, error: x }), n);
343
+ I.update(e, (p) => ({ ...p, isLoading: !1, error: x }), n);
331
344
  }
332
345
  };
333
346
  return y.useEffect(e, n), {
334
347
  state: d,
335
- trigger: (...f) => {
336
- u(!1, ...f);
348
+ trigger: (...u) => {
349
+ f(!1, ...u);
337
350
  },
338
- forceTrigger: (...f) => {
339
- u(!0, ...f);
351
+ forceTrigger: (...u) => {
352
+ f(!0, ...u);
340
353
  },
341
354
  clear: () => {
342
- _.set(e, O, n);
355
+ I.set(e, A, n);
343
356
  }
344
357
  };
345
358
  }
346
- const g = l.getInstance("SharedSubscription"), E = new R(g), M = {
359
+ const g = l.getInstance("SharedSubscription"), L = new $(g), R = {
347
360
  data: void 0,
348
361
  isLoading: !1,
349
362
  error: void 0,
350
363
  subscribed: !1,
351
364
  unsubscribe: void 0
352
- }, ce = (a, t, s) => g.createStatic({ subscriber: a }, { ...M, data: t?.initialValue }, s);
353
- async function P(a, t) {
365
+ }, ce = (a, t, s) => g.createStatic({ subscriber: a }, { ...R, data: t?.initialValue }, s);
366
+ async function C(a, t) {
354
367
  const s = g.get(a, t);
355
- s?.value.unsubscribe && (s.value.unsubscribe(), E.update(a, (e) => ({ ...e, unsubscribe: void 0, subscribed: !1 }), t));
368
+ s?.value.unsubscribe && (s.value.unsubscribe(), L.update(a, (e) => ({ ...e, unsubscribe: void 0, subscribed: !1 }), t));
356
369
  }
357
- function fe(a, t, s) {
370
+ function ue(a, t, s) {
358
371
  let e, i, r = s, n = !1;
359
372
  if (typeof a != "string") {
360
- const { key: p, subscriber: m, prefix: L, triggerImmediately: A } = a;
361
- e = p, i = m, r = L, n = A ?? !1;
373
+ const { key: p, subscriber: m, prefix: _, triggerImmediately: w } = a;
374
+ e = p, i = m, r = _, n = w ?? !1;
362
375
  } else
363
- e = w(a), i = t;
364
- const { prefix: o } = C(r);
365
- g.init(e, o, M);
376
+ e = E(a), i = t;
377
+ const { prefix: o } = j(r);
378
+ g.init(e, o, R);
366
379
  const c = b(
367
- () => (p) => (g.init(e, o, M), g.addListener(e, o, p), () => {
380
+ () => (p) => (g.init(e, o, R), g.addListener(e, o, p), () => {
368
381
  g.removeListener(e, o, p);
369
382
  }),
370
383
  [e, o]
371
384
  ), d = b(
372
385
  () => () => g.get(e, o).value,
373
386
  [e, o]
374
- ), u = j(c, d), f = (p) => {
375
- E.update(e, (m) => ({ ...m, data: p }), o);
387
+ ), f = O(c, d), u = (p) => {
388
+ L.update(e, (m) => ({ ...m, data: p }), o);
376
389
  }, v = (p) => {
377
- E.update(e, (m) => ({ ...m, isLoading: !1, data: void 0, error: p }), o);
390
+ L.update(e, (m) => ({ ...m, isLoading: !1, data: void 0, error: p }), o);
378
391
  }, S = () => {
379
- E.update(e, (p) => ({ ...p, isLoading: !1 }), o);
392
+ L.update(e, (p) => ({ ...p, isLoading: !1 }), o);
380
393
  }, x = async (p) => {
381
394
  const m = g.get(e, o);
382
- if (p && await P(e, o), m.value.subscribed && !p) return m.value;
383
- K("triggered !!"), E.update(e, (L) => ({ ...L, isLoading: !0, error: void 0 }), 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);
384
397
  try {
385
- const L = await i(f, v, S);
386
- E.update(e, (A) => ({ ...A, unsubscribe: L, subscribed: !0, isLoading: !1 }), o);
387
- } catch (L) {
388
- E.update(e, (A) => ({ ...A, isLoading: !1, error: L }), o);
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);
389
402
  }
390
403
  };
391
- return I(() => () => {
392
- K(`[${l.prefix(e, o)}]`, "unmount effect2"), g.get(e, o)?.listeners.length === 0 && P(e, o);
393
- }, [e, o]), g.useEffect(e, o), I(() => {
404
+ return P(() => () => {
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(() => {
394
407
  n && x(!1);
395
408
  }, []), {
396
- state: u,
409
+ state: f,
397
410
  trigger: () => {
398
411
  x(!1);
399
412
  },
@@ -401,7 +414,7 @@ function fe(a, t, s) {
401
414
  x(!0);
402
415
  },
403
416
  unsubscribe: () => {
404
- P(e, o);
417
+ C(e, o);
405
418
  }
406
419
  };
407
420
  }
@@ -410,14 +423,14 @@ export {
410
423
  ae as createSharedFunction,
411
424
  se as createSharedState,
412
425
  ce as createSharedSubscription,
413
- B as isDevMode,
426
+ M as isDevMode,
414
427
  ee as setDevMode,
415
- _ as sharedFunctionsApi,
428
+ I as sharedFunctionsApi,
416
429
  re as sharedStatesApi,
417
- E as sharedSubscriptionsApi,
418
- C as useSharedContext,
430
+ L as sharedSubscriptionsApi,
431
+ j as useSharedContext,
419
432
  oe as useSharedFunction,
420
433
  ne as useSharedState,
421
434
  ie as useSharedStateSelector,
422
- fe as useSharedSubscription
435
+ ue as useSharedSubscription
423
436
  };
package/dist/main.min.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /*!
2
- * react-shared-states v2.1.2
2
+ * react-shared-states v2.1.4-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,h){"use strict";f.isDevMode=!1;const z=a=>{f.isDevMode=a},P=(...a)=>{f.isDevMode&&console.log("%c[react-shared-states]","color: #007acc; font-weight: bold",...a)},A=a=>{if(!a)throw new Error("Value is empty");return a},T=()=>Math.random().toString(36).substring(2,15),K=h.createContext(void 0),V=({children:a,scopeName:t})=>{if(t&&t.includes("//"))throw new Error("scopeName cannot contain '//'");return t||(t=h.useMemo(()=>T(),[])),D.jsx(K.Provider,{value:{scopeName:t},children:a})},W=()=>h.useContext(K),$=[],M=Symbol.for("react-shared-states.manager");function Y(a,t=()=>null){const s=globalThis;return s[M]||(s[M]={}),s[M][a]||(s[M][a]=new l(t)),s[M][a]}class l{constructor(t=()=>null){this.defaultValue=t}data=new Map;static INSTANCES=new Map;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:T(),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?.(),P(`[${l.prefix(t,s)}]`,"unmount effect");const i=this.get(t,s);i&&i.listeners?.length===0&&this.clear(t,s)},[t,s])}}class C{constructor(t){this.sharedData=t}_get(t,s){let e,i=s;if(typeof t!="string"){const{key:o,prefix:u}=t;e=o,i=u}else e=A(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}}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=A(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 j=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 I,B;function H(){if(B)return I;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 I=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}},I}var J=H();const G=k(J),g=l.getInstance("sharedStatesManager"),N=new C(g),Q=(a,t)=>g.createStatic({initialValue:a},a,t);function U(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=A(a),i=t;const{prefix:n}=j(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 X(a,t,s){let e,i=s;if(typeof a!="string"){const{key:d,prefix:c}=a;e=d,i=c}else e=A(a);const{prefix:r}=j(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"),F=new C(x),_={results:void 0,isLoading:!1,error:void 0},Z=(a,t)=>x.createStatic({fn:a},_,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=A(a),i=t;const{prefix:n}=j(r);x.init(e,n,_);const o=h.useMemo(()=>c=>(x.init(e,n,_),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;F.update(e,b=>({...b,isLoading:!0,error:void 0}),n);try{const b=await i(...y);F.set(e,{results:b,isLoading:!1,error:void 0},n)}catch(b){F.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:()=>{F.set(e,_,n)}}}const v=l.getInstance("SharedSubscription"),E=new C(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 q(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:L,prefix:w,triggerImmediately:O}=a;e=S,i=L,r=w,n=O??!1}else e=A(a),i=t;const{prefix:o}=j(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,L=>({...L,data:S}),o)},y=S=>{E.update(e,L=>({...L,isLoading:!1,data:void 0,error:S}),o)},m=()=>{E.update(e,S=>({...S,isLoading:!1}),o)},b=async S=>{const L=v.get(e,o);if(S&&await q(e,o),L.value.subscribed&&!S)return L.value;P("triggered !!"),E.update(e,w=>({...w,isLoading:!0,error:void 0}),o);try{const w=await i(c,y,m);E.update(e,O=>({...O,unsubscribe:w,subscribed:!0,isLoading:!1}),o)}catch(w){E.update(e,O=>({...O,isLoading:!1,error:w}),o)}};return h.useEffect(()=>()=>{P(`[${l.prefix(e,o)}]`,"unmount effect2"),v.get(e,o)?.listeners.length===0&&q(e,o)},[e,o]),v.useEffect(e,o),h.useEffect(()=>{n&&b(!1)},[]),{state:d,trigger:()=>{b(!1)},forceTrigger:()=>{b(!0)},unsubscribe:()=>{q(e,o)}}}f.SharedStatesProvider=V,f.createSharedFunction=Z,f.createSharedState=Q,f.createSharedSubscription=te,f.setDevMode=z,f.sharedFunctionsApi=F,f.sharedStatesApi=N,f.sharedSubscriptionsApi=E,f.useSharedContext=j,f.useSharedFunction=ee,f.useSharedState=U,f.useSharedStateSelector=X,f.useSharedSubscription=re,Object.defineProperty(f,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.2",
3
+ "version": "2.1.4-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": [