react-shared-states 2.0.0 → 2.1.0

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