react-shared-states 1.0.23 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -598,10 +598,10 @@ Returns `{ state, trigger, forceTrigger, clear }`.
598
598
  Returns `{ state, trigger, forceTrigger, clear }`.
599
599
 
600
600
  ### `useSharedSubscription(key, subscriber, scopeName?)`
601
- Returns `{ state, trigger, unsubscribe }`.
601
+ Returns `{ state, trigger, forceTrigger, unsubscribe }`.
602
602
 
603
603
  ### `useSharedSubscription(sharedSubscriptionCreated)`
604
- Returns `{ state, trigger, unsubscribe }`.
604
+ Returns `{ state, trigger, forceTrigger, unsubscribe }`.
605
605
 
606
606
  ### `<SharedStatesProvider scopeName?>`
607
607
  Wrap children; optional `scopeName` (string). If omitted a random unique one is generated.
@@ -1,82 +1,41 @@
1
1
  import { AFunction, Prefix, SharedCreated, SharedValue } from './types';
2
2
  export declare const staticStores: SharedCreated[];
3
- export declare abstract class SharedValuesManager<T extends SharedValue, V> {
4
- data: Map<string, T>;
5
- defaultValue(): V;
3
+ export declare class SharedValuesManager<T> {
4
+ protected defaultValue: () => T;
5
+ data: Map<string, SharedValue<T>>;
6
+ constructor(defaultValue?: () => T);
6
7
  addListener(key: string, prefix: Prefix, listener: AFunction): void;
7
8
  removeListener(key: string, prefix: Prefix, listener: AFunction): void;
8
9
  callListeners(key: string, prefix: Prefix): void;
9
- init(key: string, prefix: Prefix, data: V, isStatic?: boolean): void;
10
- createStatic<X extends SharedCreated>(rest: Omit<X, 'key' | 'prefix'>, scopeName?: Prefix): {
11
- key: string;
12
- prefix: Prefix;
13
- } & Omit<X, "key" | "prefix">;
10
+ init(key: string, prefix: Prefix, initialValue: T, isStatic?: boolean): void;
11
+ createStatic<X extends SharedCreated>(rest: Omit<X, 'key' | 'prefix'>, initialValue: T, scopeName?: Prefix): X;
14
12
  initStatic(sharedCreated: SharedCreated): void;
15
13
  clearAll(withoutListeners?: boolean, withStatic?: boolean): void;
16
14
  clear(key: string, prefix: Prefix, withoutListeners?: boolean, withStatic?: boolean): void;
17
- get(key: string, prefix: Prefix): T | undefined;
18
- setValue(key: string, prefix: Prefix, data: V): void;
15
+ get(key: string, prefix: Prefix): SharedValue<T> | undefined;
16
+ setValue(key: string, prefix: Prefix, value: T): void;
19
17
  has(key: string, prefix: Prefix): string | undefined;
20
18
  static prefix(key: string, prefix: Prefix): string;
21
- static extractPrefix(mapKey: string): string[];
19
+ static extractPrefix(mapKey: string): [Prefix, string];
22
20
  useEffect(key: string, prefix: Prefix, unsub?: (() => void) | null): void;
23
21
  }
24
- export declare class SharedValuesApi<T extends SharedValue, V, R = T> {
25
- protected sharedData: SharedValuesManager<T, V>;
26
- constructor(sharedData: SharedValuesManager<T, V>);
27
- /**
28
- * get a value from the shared data
29
- * @param key
30
- * @param scopeName
31
- */
32
- get<S extends string = string>(key: S, scopeName: Prefix): R;
33
- get<S extends string = string>(sharedCreated: SharedCreated): R;
34
- /**
35
- * set a value in the shared data
36
- * @param key
37
- * @param value
38
- * @param scopeName
39
- */
40
- set<S extends string = string>(key: S, value: V, scopeName: Prefix): void;
41
- set<S extends string = string>(sharedCreated: SharedCreated, value: V): void;
42
- /**
43
- * update a value in the shared data
44
- * @param key
45
- * @param updater
46
- * @param scopeName
47
- */
48
- update<S extends string = string>(key: S, updater: (prev: R) => V, scopeName: Prefix): void;
49
- update<S extends string = string>(sharedCreated: SharedCreated, updater: (prev: R) => V): void;
50
- /**
51
- * clear all values from the shared data
52
- */
22
+ export declare class SharedValuesApi<T> {
23
+ protected sharedData: SharedValuesManager<T>;
24
+ constructor(sharedData: SharedValuesManager<T>);
25
+ private _get;
26
+ get(key: string, scopeName?: Prefix): T;
27
+ get(sharedCreated: SharedCreated): T;
28
+ set(key: string, value: T, scopeName?: Prefix): void;
29
+ set(sharedCreated: SharedCreated, value: T): void;
30
+ update(key: string, updater: (prev: T) => T, scopeName?: Prefix): void;
31
+ update(sharedCreated: SharedCreated, updater: (prev: T) => T): void;
53
32
  clearAll(): void;
54
- /**
55
- * clear all values from the shared data in a scope
56
- * @param scopeName
57
- */
58
33
  clearScope(scopeName?: Prefix): void;
59
- /**
60
- * resolve a shared created object to a value
61
- * @param sharedCreated
62
- */
63
- resolve(sharedCreated: SharedCreated): R;
64
- /**
65
- * clear a value from the shared data
66
- * @param key
67
- * @param scopeName
68
- */
34
+ resolve(sharedCreated: SharedCreated): T | undefined;
69
35
  clear(key: string, scopeName: Prefix): void;
70
36
  clear(sharedCreated: SharedCreated): void;
71
- /**
72
- * check if a value exists in the shared data
73
- * @param key
74
- * @param scopeName
75
- */
76
37
  has(key: string, scopeName?: Prefix): boolean;
77
- /**
78
- * get all values from the shared data
79
- */
80
- getAll(): Record<string, Record<string, any>>;
81
- subscribe<S extends string = string>(sharedCreated: SharedCreated, listener: AFunction): () => void;
38
+ getAll(): Record<string, Record<string, T>>;
39
+ subscribe(key: string, listener: AFunction, scopeName?: Prefix): () => void;
40
+ subscribe(sharedCreated: SharedCreated, listener: AFunction): () => void;
82
41
  }
@@ -1,7 +1,7 @@
1
- export { useSharedState, sharedStatesApi, createSharedState, SharedStatesApi, useSharedStateSelector } from './use-shared-state';
1
+ export { useSharedState, sharedStatesApi, createSharedState, useSharedStateSelector } from './use-shared-state';
2
2
  export type { SharedStateCreated, SharedStateSelector } from './use-shared-state';
3
- export { useSharedFunction, sharedFunctionsApi, createSharedFunction, SharedFunctionsApi } from './use-shared-function';
3
+ export { useSharedFunction, sharedFunctionsApi, createSharedFunction, } from './use-shared-function';
4
4
  export type { SharedFunctionStateReturn } from './use-shared-function';
5
- export { useSharedSubscription, sharedSubscriptionsApi, createSharedSubscription, SharedSubscriptionsApi } from './use-shared-subscription';
5
+ export { useSharedSubscription, sharedSubscriptionsApi, createSharedSubscription, } from './use-shared-subscription';
6
6
  export type { SharedSubscriptionStateReturn } from './use-shared-subscription';
7
7
  export { default as useSharedContext } from './use-shared';
@@ -1,48 +1,11 @@
1
- import { AFunction, Prefix, SharedCreated, SharedValue } from '../types';
2
- import { SharedValuesApi, SharedValuesManager } from '../SharedValuesManager';
3
- type SharedFunctionValue<T> = {
1
+ import { AFunction, Prefix, SharedCreated } from '../types';
2
+ import { SharedValuesApi } from '../SharedValuesManager';
3
+ export type SharedFunctionValue<T> = {
4
4
  results?: T;
5
5
  isLoading: boolean;
6
6
  error?: unknown;
7
7
  };
8
- interface SharedFunction<T> extends SharedValue {
9
- fnState: SharedFunctionValue<T>;
10
- }
11
- declare class SharedFunctionsManager extends SharedValuesManager<SharedFunction<unknown>, {
12
- fnState: SharedFunctionValue<unknown>;
13
- }> {
14
- defaultValue(): {
15
- fnState: {
16
- results: undefined;
17
- isLoading: boolean;
18
- error: undefined;
19
- };
20
- };
21
- initValue(key: string, prefix: Prefix, isStatic?: boolean): void;
22
- setValue<T>(key: string, prefix: Prefix, data: {
23
- fnState: SharedFunctionValue<T>;
24
- }): void;
25
- }
26
- export declare class SharedFunctionsApi extends SharedValuesApi<SharedFunction<unknown>, {
27
- fnState: SharedFunctionValue<unknown>;
28
- }, SharedFunctionValue<unknown>> {
29
- constructor(sharedFunctionManager: SharedFunctionsManager);
30
- get<T, S extends string = string>(key: S, scopeName?: Prefix): SharedFunctionValue<T>;
31
- get<T, Args extends unknown[]>(sharedFunctionCreated: SharedFunctionCreated<T, Args>): SharedFunctionValue<T>;
32
- set<T, S extends string = string>(key: S, value: {
33
- fnState: SharedFunctionValue<T>;
34
- }, scopeName?: Prefix): void;
35
- set<T, Args extends unknown[]>(sharedFunctionCreated: SharedFunctionCreated<T, Args>, value: {
36
- fnState: SharedFunctionValue<T>;
37
- }): void;
38
- update<T, Args extends unknown[], S extends string = string>(key: S, updater: (prev: SharedFunctionValue<T>) => {
39
- fnState: SharedFunctionValue<T>;
40
- }, scopeName?: Prefix): void;
41
- update<T, Args extends unknown[]>(sharedFunctionCreated: SharedFunctionCreated<T, Args>, updater: (prev: SharedFunctionValue<T>) => {
42
- fnState: SharedFunctionValue<T>;
43
- }): void;
44
- }
45
- export declare const sharedFunctionsApi: SharedFunctionsApi;
8
+ export declare const sharedFunctionsApi: SharedValuesApi<SharedFunctionValue<any>>;
46
9
  interface SharedFunctionCreated<T, Args extends unknown[]> extends SharedCreated {
47
10
  fn: AFunction<T, Args>;
48
11
  }
@@ -1,29 +1,6 @@
1
- import { Prefix, SharedCreated, SharedValue } from '../types';
2
- import { SharedValuesApi, SharedValuesManager } from '../SharedValuesManager';
3
- interface SharedState<T> extends SharedValue {
4
- value: T;
5
- }
6
- declare class SharedStatesManager extends SharedValuesManager<SharedState<unknown>, {
7
- value: unknown;
8
- }> {
9
- defaultValue(): {
10
- value: undefined;
11
- };
12
- initValue(key: string, prefix: Prefix, value: unknown, isStatic?: boolean): void;
13
- initStatic(sharedStateCreated: SharedStateCreated<any>): void;
14
- }
15
- export declare class SharedStatesApi extends SharedValuesApi<SharedState<unknown>, {
16
- value: unknown;
17
- }, unknown> {
18
- constructor(sharedStateManager: SharedStatesManager);
19
- get<T, S extends string = string>(key: S, scopeName?: Prefix): T;
20
- get<T>(sharedStateCreated: SharedStateCreated<T>): T;
21
- set<T, S extends string = string>(key: S, value: T, scopeName?: Prefix): void;
22
- set<T>(sharedStateCreated: SharedStateCreated<T>, value: T): void;
23
- update<T, S extends string = string>(key: S, updater: (prev: T) => T, scopeName?: Prefix): void;
24
- update<T>(sharedStateCreated: SharedStateCreated<T>, updater: (prev: T) => T): void;
25
- }
26
- export declare const sharedStatesApi: SharedStatesApi;
1
+ import { Prefix, SharedCreated } from '../types';
2
+ import { SharedValuesApi } from '../SharedValuesManager';
3
+ export declare const sharedStatesApi: SharedValuesApi<any>;
27
4
  export interface SharedStateCreated<T> extends SharedCreated {
28
5
  initialValue: T;
29
6
  }
@@ -33,4 +10,3 @@ export declare function useSharedState<T>(sharedStateCreated: SharedStateCreated
33
10
  export type SharedStateSelector<S, T = S> = (original: S) => T;
34
11
  export declare function useSharedStateSelector<T, S extends string, R>(key: S, selector: SharedStateSelector<T, R>, scopeName?: Prefix): Readonly<R>;
35
12
  export declare function useSharedStateSelector<T, R>(sharedStateCreated: SharedStateCreated<T>, selector: SharedStateSelector<T, R>): Readonly<R>;
36
- export {};
@@ -1,5 +1,5 @@
1
- import { PotentialPromise, Prefix, SharedCreated, SharedValue } from '../types';
2
- import { SharedValuesApi, SharedValuesManager } from '../SharedValuesManager';
1
+ import { PotentialPromise, Prefix, SharedCreated } from '../types';
2
+ import { SharedValuesApi } from '../SharedValuesManager';
3
3
  export type Unsubscribe = () => void;
4
4
  export declare namespace SubscriberEvents {
5
5
  type OnError = (error: unknown) => void;
@@ -7,58 +7,24 @@ export declare namespace SubscriberEvents {
7
7
  type Set<T> = (value: T) => void;
8
8
  }
9
9
  export type Subscriber<T> = (set: SubscriberEvents.Set<T>, onError: SubscriberEvents.OnError, onCompletion: SubscriberEvents.OnCompletion) => PotentialPromise<Unsubscribe | void | undefined>;
10
- type SharedSubscriptionValue<T> = {
11
- data?: T;
10
+ export type SharedSubscriptionValue<T> = {
11
+ data: T;
12
12
  isLoading: boolean;
13
13
  error?: unknown;
14
14
  subscribed?: boolean;
15
15
  };
16
- interface SharedSubscription<T> extends SharedValue {
17
- fnState: SharedSubscriptionValue<T>;
16
+ interface SharedSubscription<T> extends SharedSubscriptionValue<T> {
18
17
  unsubscribe?: Unsubscribe | void;
19
18
  }
20
- declare class SharedSubscriptionsManager extends SharedValuesManager<SharedSubscription<unknown>, {
21
- fnState: SharedSubscriptionValue<unknown>;
22
- }> {
23
- defaultValue(): {
24
- fnState: {
25
- data: undefined;
26
- isLoading: boolean;
27
- error: undefined;
28
- subscribed: boolean;
29
- };
30
- };
31
- initValue(key: string, prefix: Prefix, isStatic?: boolean): void;
32
- setValue<T>(key: string, prefix: Prefix, data: {
33
- fnState: SharedSubscriptionValue<T>;
34
- }): void;
35
- useEffect(key: string, prefix: Prefix): void;
36
- unsubscribe(key: string, prefix: Prefix): Promise<void>;
37
- }
38
- export declare class SharedSubscriptionsApi extends SharedValuesApi<SharedSubscription<unknown>, {
39
- fnState: SharedSubscriptionValue<unknown>;
40
- }, SharedSubscriptionValue<unknown>> {
41
- constructor(sharedSubscriptionsManager: SharedSubscriptionsManager);
42
- get<T, S extends string = string>(key: S, scopeName?: Prefix): SharedSubscriptionValue<T>;
43
- get<T>(sharedSubscriptionCreated: SharedSubscriptionCreated<T>): SharedSubscriptionValue<T>;
44
- set<T, S extends string = string>(key: S, value: {
45
- fnState: SharedSubscriptionValue<T>;
46
- }, scopeName?: Prefix): void;
47
- set<T>(sharedSubscriptionCreated: SharedSubscriptionCreated<T>, value: {
48
- fnState: SharedSubscriptionValue<T>;
49
- }): void;
50
- update<T, S extends string = string>(key: S, updater: (prev: SharedSubscriptionValue<T>) => {
51
- fnState: SharedSubscriptionValue<T>;
52
- }, scopeName?: Prefix): void;
53
- update<T>(sharedSubscriptionCreated: SharedSubscriptionCreated<T>, updater: (prev: SharedSubscriptionValue<T>) => {
54
- fnState: SharedSubscriptionValue<T>;
55
- }): void;
56
- }
57
- export declare const sharedSubscriptionsApi: SharedSubscriptionsApi;
19
+ export declare const sharedSubscriptionsApi: SharedValuesApi<SharedSubscription<any>>;
58
20
  interface SharedSubscriptionCreated<T> extends SharedCreated {
59
21
  subscriber: Subscriber<T>;
22
+ triggerImmediately?: boolean;
60
23
  }
61
- 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>;
62
28
  export type SharedSubscriptionStateReturn<T> = {
63
29
  readonly state: NonNullable<SharedSubscriptionValue<T>>;
64
30
  readonly trigger: () => void;
package/dist/main.esm.js CHANGED
@@ -1,535 +1,410 @@
1
1
  /*!
2
- * react-shared-states v1.0.23
2
+ * react-shared-states v2.0.1
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 $ } from "react/jsx-runtime";
9
- import { createContext as q, useMemo as L, useContext as R, useEffect as C, useSyncExternalStore as m, useRef as z } from "react";
10
- let P = !1;
11
- const N = (a) => {
12
- P = a;
13
- }, V = (...a) => {
14
- P && 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
- ...a
17
+ ...o
18
18
  );
19
- }, w = (a) => {
20
- if (!a) throw new Error("Value is empty");
21
- return a;
22
- }, _ = () => Math.random().toString(36).substring(2, 15), B = q(void 0), k = ({ children: a, scopeName: t }) => {
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 }) => {
23
23
  if (t && t.includes("//")) throw new Error("scopeName cannot contain '//'");
24
- return t || (t = L(() => _(), [])), /* @__PURE__ */ $(B.Provider, { value: { scopeName: t }, children: a });
25
- }, G = () => R(B), F = [];
26
- class c {
27
- data = /* @__PURE__ */ new Map();
28
- defaultValue() {
29
- return {};
30
- }
31
- addListener(t, r, e) {
32
- this.data.has(c.prefix(t, r)) || this.data.set(c.prefix(t, r), {
33
- ...this.defaultValue(),
34
- listeners: []
35
- }), this.data.get(c.prefix(t, r)).listeners.push(e);
36
- }
37
- removeListener(t, r, e) {
38
- this.data.has(c.prefix(t, r)) && (this.data.get(c.prefix(t, r)).listeners = this.data.get(c.prefix(t, r)).listeners.filter((i) => i !== e));
24
+ return t || (t = b(() => G(), [])), /* @__PURE__ */ M(I.Provider, { value: { scopeName: t }, children: o });
25
+ }, H = () => W(I), B = [];
26
+ class l {
27
+ constructor(t = () => null) {
28
+ this.defaultValue = t;
39
29
  }
40
- callListeners(t, r) {
41
- this.data.has(c.prefix(t, r)) && this.data.get(c.prefix(t, r)).listeners.forEach((e) => e());
42
- }
43
- init(t, r, e, i = !1) {
44
- this.data.has(c.prefix(t, r)) || this.data.set(c.prefix(t, r), {
45
- ...e,
46
- isStatic: i,
30
+ data = /* @__PURE__ */ new Map();
31
+ addListener(t, n, e) {
32
+ const i = l.prefix(t, n), r = this.data.get(i);
33
+ r && r.listeners.push(e);
34
+ }
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));
38
+ }
39
+ callListeners(t, n) {
40
+ const e = l.prefix(t, n), i = this.data.get(e);
41
+ i && i.listeners.forEach((r) => r());
42
+ }
43
+ init(t, n, e, i = !1) {
44
+ const r = l.prefix(t, n);
45
+ this.data.has(r) || this.data.set(r, {
46
+ value: e,
47
+ isStatic: i || void 0,
47
48
  listeners: []
48
49
  });
49
50
  }
50
- createStatic(t, r) {
51
- const e = r ?? r ?? "_global", i = {
52
- key: _(),
53
- prefix: e,
51
+ createStatic(t, n, e) {
52
+ const i = e ?? "_global", r = {
53
+ key: G(),
54
+ prefix: i,
54
55
  ...t
55
56
  };
56
- return F.push(i), this.initStatic(i), i;
57
+ return B.push(r), this.init(r.key, r.prefix, n, !0), this.defaultValue = () => n, r;
57
58
  }
58
59
  initStatic(t) {
59
- const { key: r, prefix: e } = t;
60
- this.init(r, e, this.defaultValue(), !0);
60
+ const { key: n, prefix: e } = t;
61
+ this.init(n, e, this.defaultValue(), !0);
61
62
  }
62
- clearAll(t = !1, r = !1) {
63
+ clearAll(t = !1, n = !1) {
63
64
  this.data.forEach((e, i) => {
64
- const [n, s] = c.extractPrefix(i);
65
- this.clear(s, n, t, r);
65
+ const [r, s] = l.extractPrefix(i);
66
+ this.clear(s, r, t, n);
66
67
  });
67
68
  }
68
- clear(t, r, e = !1, i = !1) {
69
- e || this.callListeners(t, r);
70
- const n = this.data.get(c.prefix(t, r));
71
- if (!n) return;
72
- const s = { ...n };
73
- if (this.data.delete(c.prefix(t, r)), s.isStatic && !i) {
74
- const u = F.find((o) => o.key === t && o.prefix === r);
75
- u && this.initStatic(u);
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);
76
76
  }
77
77
  }
78
- get(t, r) {
79
- let e = this.has(t, r);
78
+ get(t, n) {
79
+ let e = this.has(t, n);
80
80
  if (e)
81
81
  return this.data.get(e);
82
82
  }
83
- setValue(t, r, e) {
84
- this.data.has(c.prefix(t, r)) && this.data.set(c.prefix(t, r), {
85
- ...this.data.get(c.prefix(t, r)),
86
- ...e
87
- });
83
+ setValue(t, n, e) {
84
+ const i = l.prefix(t, n), r = this.data.get(i);
85
+ r && (r.value = e, this.data.set(i, r));
88
86
  }
89
- has(t, r) {
90
- return this.data.has(c.prefix(t, r)) ? c.prefix(t, r) : this.data.has(c.prefix(t, "_global")) ? c.prefix(t, "_global") : void 0;
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;
91
89
  }
92
- static prefix(t, r) {
90
+ static prefix(t, n) {
93
91
  if (t.includes("//")) throw new Error("key cannot contain '//'");
94
- return `${r}//${t}`;
92
+ return `${n}//${t}`;
95
93
  }
96
94
  static extractPrefix(t) {
97
- return t.split("//");
95
+ const n = t.split("//");
96
+ return [n[0], n.slice(1).join("//")];
98
97
  }
99
- useEffect(t, r, e = null) {
100
- C(() => () => {
101
- e?.(), V(`[${c.prefix(t, r)}]`, "unmount effect"), this.data.get(c.prefix(t, r)).listeners?.length === 0 && this.clear(t, r);
102
- }, []);
98
+ useEffect(t, n, e = null) {
99
+ 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]);
103
104
  }
104
105
  }
105
- class O {
106
+ class q {
106
107
  constructor(t) {
107
108
  this.sharedData = t;
108
109
  }
109
- get(t, r) {
110
- let e, i = r;
110
+ _get(t, n) {
111
+ let e, i = n;
111
112
  if (typeof t != "string") {
112
- const { key: u, prefix: o } = t;
113
- e = u, i = o;
113
+ const { key: a, prefix: c } = t;
114
+ e = a, i = c;
114
115
  } else
115
- e = w(t);
116
- const n = i || "_global";
117
- return this.sharedData.get(e, n);
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
+ key: e,
120
+ prefix: r,
121
+ value: void 0
122
+ };
123
+ }
124
+ get(t, n) {
125
+ return this._get(t, n).value;
118
126
  }
119
- set(t, r, e) {
120
- let i, n = e;
127
+ set(t, n, e) {
128
+ let i, r = e;
121
129
  if (typeof t != "string") {
122
- const { key: u, prefix: o } = t;
123
- i = u, n = o;
130
+ const { key: a, prefix: c } = t;
131
+ i = a, r = c;
124
132
  } else
125
- i = w(t);
126
- const s = n || "_global";
127
- this.sharedData.init(i, s, r), this.sharedData.setValue(i, s, r), this.sharedData.callListeners(i, s);
128
- }
129
- update(t, r, e) {
130
- let i;
131
- typeof t == "string" ? i = this.get(t, e) : i = this.get(t);
132
- const n = r(i);
133
- typeof t == "string" ? this.set(t, n, e) : this.set(t, n);
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
+ }
137
+ update(t, n, e) {
138
+ const i = this._get(t, e);
139
+ if (i) {
140
+ const r = n(i.value);
141
+ this.set(i.key, r, i.prefix);
142
+ }
134
143
  }
135
- /**
136
- * clear all values from the shared data
137
- */
138
144
  clearAll() {
139
145
  this.sharedData.clearAll();
140
146
  }
141
- /**
142
- * clear all values from the shared data in a scope
143
- * @param scopeName
144
- */
145
147
  clearScope(t) {
146
- const r = t || "_global";
148
+ const n = t || "_global";
147
149
  this.sharedData.data.forEach((e, i) => {
148
- const [n, s] = c.extractPrefix(i);
149
- if (n === r) {
150
- this.sharedData.clear(s, n), this.sharedData.callListeners(s, n);
151
- return;
152
- }
150
+ const [r, s] = l.extractPrefix(i);
151
+ r === n && (this.sharedData.clear(s, r), this.sharedData.callListeners(s, r));
153
152
  });
154
153
  }
155
- /**
156
- * resolve a shared created object to a value
157
- * @param sharedCreated
158
- */
159
154
  resolve(t) {
160
- const { key: r, prefix: e } = t;
161
- return this.get(r, e);
155
+ const { key: n, prefix: e } = t;
156
+ return this.get(n, e);
162
157
  }
163
- clear(t, r) {
158
+ clear(t, n) {
164
159
  let e, i;
165
- typeof t == "string" ? (e = t, i = r || "_global") : (e = t.key, i = t.prefix), this.sharedData.clear(e, i);
160
+ typeof t == "string" ? (e = t, i = n || "_global") : (e = t.key, i = t.prefix), this.sharedData.clear(e, i);
166
161
  }
167
- /**
168
- * check if a value exists in the shared data
169
- * @param key
170
- * @param scopeName
171
- */
172
- has(t, r = "_global") {
173
- const e = r || "_global";
162
+ has(t, n = "_global") {
163
+ const e = n || "_global";
174
164
  return !!this.sharedData.has(t, e);
175
165
  }
176
- /**
177
- * get all values from the shared data
178
- */
179
166
  getAll() {
180
167
  const t = {};
181
- return this.sharedData.data.forEach((r, e) => {
182
- const [i, n] = c.extractPrefix(e);
183
- t[i] = t[i] || {}, t[i][n] = r;
168
+ return this.sharedData.data.forEach((n, e) => {
169
+ const [i, r] = l.extractPrefix(e);
170
+ t[i] = t[i] || {}, t[i][r] = n.value;
184
171
  }), t;
185
172
  }
186
- subscribe(t, r, e) {
187
- let i, n;
188
- return typeof t == "string" ? (i = t, n = e || "_global") : (i = t.key, n = t.prefix), this.sharedData.addListener(i, n, r), () => {
189
- this.sharedData.removeListener(i, n, r);
173
+ subscribe(t, n, e) {
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);
190
177
  };
191
178
  }
192
179
  }
193
- const D = (a) => {
194
- const t = G();
180
+ const j = (o) => {
181
+ const t = H();
195
182
  return {
196
- prefix: a ?? t?.scopeName ?? "_global"
183
+ prefix: o ?? t?.scopeName ?? "_global"
197
184
  };
198
185
  };
199
- function T(a) {
200
- return a && a.__esModule && Object.prototype.hasOwnProperty.call(a, "default") ? a.default : a;
186
+ function J(o) {
187
+ return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
201
188
  }
202
- var A, j;
203
- function W() {
204
- if (j) return A;
205
- j = 1;
206
- var a = typeof Element < "u", t = typeof Map == "function", r = typeof Set == "function", e = typeof ArrayBuffer == "function" && !!ArrayBuffer.isView;
207
- function i(n, s) {
208
- if (n === s) return !0;
209
- if (n && s && typeof n == "object" && typeof s == "object") {
210
- if (n.constructor !== s.constructor) return !1;
211
- var u, o, d;
212
- if (Array.isArray(n)) {
213
- if (u = n.length, u != s.length) return !1;
214
- for (o = u; o-- !== 0; )
215
- if (!i(n[o], s[o])) return !1;
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;
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;
216
203
  return !0;
217
204
  }
218
- var p;
219
- if (t && n instanceof Map && s instanceof Map) {
220
- if (n.size !== s.size) return !1;
221
- for (p = n.entries(); !(o = p.next()).done; )
222
- if (!s.has(o.value[0])) return !1;
223
- for (p = n.entries(); !(o = p.next()).done; )
224
- if (!i(o.value[1], s.get(o.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;
225
212
  return !0;
226
213
  }
227
- if (r && n instanceof Set && s instanceof Set) {
228
- if (n.size !== s.size) return !1;
229
- for (p = n.entries(); !(o = p.next()).done; )
230
- if (!s.has(o.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;
231
218
  return !0;
232
219
  }
233
- if (e && ArrayBuffer.isView(n) && ArrayBuffer.isView(s)) {
234
- if (u = n.length, u != s.length) return !1;
235
- for (o = u; o-- !== 0; )
236
- if (n[o] !== s[o]) 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;
237
224
  return !0;
238
225
  }
239
- if (n.constructor === RegExp) return n.source === s.source && n.flags === s.flags;
240
- if (n.valueOf !== Object.prototype.valueOf && typeof n.valueOf == "function" && typeof s.valueOf == "function") return n.valueOf() === s.valueOf();
241
- if (n.toString !== Object.prototype.toString && typeof n.toString == "function" && typeof s.toString == "function") return n.toString() === s.toString();
242
- if (d = Object.keys(n), u = d.length, u !== Object.keys(s).length) return !1;
243
- for (o = u; o-- !== 0; )
244
- if (!Object.prototype.hasOwnProperty.call(s, d[o])) return !1;
245
- if (a && n instanceof Element) return !1;
246
- for (o = u; o-- !== 0; )
247
- if (!((d[o] === "_owner" || d[o] === "__v" || d[o] === "__o") && n.$$typeof) && !i(n[d[o]], s[d[o]]))
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]]))
248
235
  return !1;
249
236
  return !0;
250
237
  }
251
- return n !== n && s !== s;
238
+ return r !== r && s !== s;
252
239
  }
253
- return A = function(s, u) {
240
+ return C = function(s, a) {
254
241
  try {
255
- return i(s, u);
256
- } catch (o) {
257
- if ((o.message || "").match(/stack|recursion/i))
242
+ return i(s, a);
243
+ } catch (c) {
244
+ if ((c.message || "").match(/stack|recursion/i))
258
245
  return console.warn("react-fast-compare cannot handle circular refs"), !1;
259
- throw o;
260
- }
261
- }, A;
262
- }
263
- var I = W();
264
- const K = /* @__PURE__ */ T(I);
265
- class H extends c {
266
- defaultValue() {
267
- return { value: void 0 };
268
- }
269
- initValue(t, r, e, i = !1) {
270
- super.init(t, r, { value: e }, i);
271
- }
272
- initStatic(t) {
273
- const { key: r, prefix: e, initialValue: i } = t;
274
- this.initValue(r, e, i, !0);
275
- }
276
- }
277
- class J extends O {
278
- constructor(t) {
279
- super(t);
280
- }
281
- get(t, r = "_global") {
282
- return typeof t != "string" ? super.get(t)?.value : super.get(t, r)?.value;
283
- }
284
- set(t, r, e = "_global") {
285
- if (typeof t != "string") {
286
- super.set(t, { value: r });
287
- return;
246
+ throw c;
288
247
  }
289
- super.set(t, { value: r }, e);
290
- }
291
- update(t, r, e = "_global") {
292
- let i;
293
- typeof t == "string" ? i = this.get(t, e) : i = this.get(t);
294
- const n = r(i);
295
- typeof t == "string" ? this.set(t, n, e) : this.set(t, n);
296
- }
248
+ }, C;
297
249
  }
298
- const S = new H(), tt = new J(S), et = (a, t) => S.createStatic({ initialValue: a }, t);
299
- function rt(a, t, r) {
300
- let e, i, n = r;
301
- if (typeof a != "string") {
302
- const { key: f, initialValue: b, prefix: g } = a;
303
- e = f, i = b, n = g;
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;
304
257
  } else
305
- e = w(a), i = t;
306
- const { prefix: s } = D(n);
307
- S.initValue(e, s, i);
308
- const u = L(() => (f) => (S.initValue(e, s, t), S.addListener(e, s, f), () => {
309
- S.removeListener(e, s, f);
310
- }), []), o = L(() => () => S.get(e, s)?.value, []), d = m(u, o), p = (f) => {
311
- const b = typeof f == "function" ? f(S.get(e, s)?.value) : f;
312
- b !== d && (S.setValue(e, s, { value: b }), S.callListeners(e, s));
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));
313
266
  };
314
- return S.useEffect(e, s), [
267
+ return h.useEffect(e, s), [
315
268
  d,
316
- p
269
+ u
317
270
  ];
318
271
  }
319
- function st(a, t, r) {
320
- let e, i = r;
321
- if (typeof a != "string") {
322
- const { key: p, prefix: f } = a;
323
- e = p, i = f;
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;
324
277
  } else
325
- e = w(a);
326
- const { prefix: n } = D(i), s = z(void 0), u = L(() => (p) => (S.addListener(e, n, p), () => {
327
- S.removeListener(e, n, p);
328
- }), []), o = L(() => () => {
329
- const p = S.get(e, n)?.value, f = t(p);
330
- return K(s.current, f) ? s.current : f;
331
- }, []), d = m(u, o);
332
- return S.useEffect(e, n), d;
278
+ e = E(o);
279
+ const { prefix: r } = j(i), s = k(void 0), a = b(() => (u) => (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;
333
286
  }
334
- class Q extends c {
335
- defaultValue() {
336
- return {
337
- fnState: {
338
- results: void 0,
339
- isLoading: !1,
340
- error: void 0
341
- }
342
- };
343
- }
344
- initValue(t, r, e = !1) {
345
- super.init(t, r, this.defaultValue(), e);
346
- }
347
- setValue(t, r, e) {
348
- super.setValue(t, r, e);
349
- }
350
- }
351
- class U extends O {
352
- constructor(t) {
353
- super(t);
354
- }
355
- get(t, r = "_global") {
356
- return typeof t != "string" ? super.get(t)?.fnState : super.get(t, r)?.fnState;
357
- }
358
- set(t, r, e = "_global") {
359
- if (typeof t != "string") {
360
- super.set(t, r);
361
- return;
362
- }
363
- super.set(t, r, e);
364
- }
365
- update(t, r, e = "_global") {
366
- let i;
367
- typeof t == "string" ? i = this.get(t, e) : i = this.get(t);
368
- const n = r(i);
369
- typeof t == "string" ? this.set(t, n, e) : this.set(t, n);
370
- }
371
- }
372
- const v = new Q(), nt = new U(v), it = (a, t) => v.createStatic({ fn: a }, t);
373
- function at(a, t, r) {
374
- let e, i, n = r;
375
- if (typeof a != "string") {
376
- const { key: f, fn: b, prefix: g } = a;
377
- e = f, i = b, n = g;
287
+ const y = new l(), A = new q(y), O = {
288
+ results: void 0,
289
+ isLoading: !1,
290
+ 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;
378
297
  } else
379
- e = w(a), i = t;
380
- const { prefix: s } = D(n);
381
- v.initValue(e, s);
382
- const u = L(
383
- () => (f) => (v.initValue(e, s), v.addListener(e, s, f), () => {
384
- v.removeListener(e, s, 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);
385
304
  }),
386
- []
387
- ), o = L(
388
- () => () => v.get(e, s).fnState,
389
- []
390
- ), d = m(u, o), p = async (f, ...b) => {
391
- const g = v.get(e, s);
392
- if (!f && (g.fnState.isLoading || g.fnState.results !== void 0)) return g.fnState;
393
- g.fnState = { ...g.fnState, isLoading: !0, error: void 0 }, v.callListeners(e, s);
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
+ 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);
394
313
  try {
395
- const h = await i(...b);
396
- g.fnState = { results: h, isLoading: !1, error: void 0 };
397
- } catch (h) {
398
- g.fnState = { ...g.fnState, isLoading: !1, error: h };
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);
399
318
  }
400
- v.callListeners(e, s);
401
319
  };
402
- return v.useEffect(e, s), {
320
+ return y.useEffect(e, s), {
403
321
  state: d,
404
322
  trigger: (...f) => {
405
- p(!1, ...f);
323
+ u(!1, ...f);
406
324
  },
407
325
  forceTrigger: (...f) => {
408
- p(!0, ...f);
326
+ u(!0, ...f);
409
327
  },
410
328
  clear: () => {
411
- const f = v.get(e, s);
412
- f && (f.fnState = v.defaultValue().fnState, v.callListeners(e, s));
329
+ A.set(e, O, s);
413
330
  }
414
331
  };
415
332
  }
416
- class X extends c {
417
- defaultValue() {
418
- return {
419
- fnState: {
420
- data: void 0,
421
- isLoading: !1,
422
- error: void 0,
423
- subscribed: !1
424
- }
425
- };
426
- }
427
- initValue(t, r, e = !1) {
428
- super.init(t, r, this.defaultValue(), e);
429
- }
430
- setValue(t, r, e) {
431
- super.setValue(t, r, e);
432
- }
433
- useEffect(t, r) {
434
- C(() => () => {
435
- V(`[${c.prefix(t, r)}]`, "unmount effect2"), this.get(t, r)?.listeners.length === 0 && this.unsubscribe(t, r);
436
- }, []), super.useEffect(t, r);
437
- }
438
- async unsubscribe(t, r) {
439
- const e = this.get(t, r);
440
- e && (e.unsubscribe && (e.unsubscribe(), e.unsubscribe = void 0), e.fnState = { ...e.fnState, subscribed: !1 }, this.callListeners(t, r));
441
- }
333
+ const g = new l(), w = new q(g), $ = {
334
+ data: void 0,
335
+ isLoading: !1,
336
+ error: void 0,
337
+ subscribed: !1,
338
+ 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));
442
343
  }
443
- class Y extends O {
444
- constructor(t) {
445
- super(t);
446
- }
447
- get(t, r = "_global") {
448
- return typeof t != "string" ? super.get(t)?.fnState : super.get(t, r)?.fnState;
449
- }
450
- set(t, r, e = "_global") {
451
- if (typeof t != "string") {
452
- super.set(t, r);
453
- return;
454
- }
455
- super.set(t, r, e);
456
- }
457
- update(t, r, e = "_global") {
458
- let i;
459
- typeof t == "string" ? i = this.get(t, e) : i = this.get(t);
460
- const n = r(i);
461
- typeof t == "string" ? this.set(t, n, e) : this.set(t, n);
462
- }
463
- }
464
- const l = new X(), ot = new Y(l), ft = (a, t) => l.createStatic({ subscriber: a }, t);
465
- function ct(a, t, r) {
466
- let e, i, n = r;
467
- if (typeof a != "string") {
468
- const { key: h, subscriber: x, prefix: E } = a;
469
- e = h, i = x, n = E;
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;
470
349
  } else
471
- e = w(a), i = t;
472
- const { prefix: s } = D(n);
473
- l.initValue(e, s);
474
- const u = L(
475
- () => (h) => (l.initValue(e, s), l.addListener(e, s, h), () => {
476
- l.removeListener(e, s, h);
350
+ e = E(o), i = t;
351
+ const { prefix: a } = j(r);
352
+ g.init(e, a, $);
353
+ const c = b(
354
+ () => (p) => (g.init(e, a, $), g.addListener(e, a, p), () => {
355
+ g.removeListener(e, a, p);
477
356
  }),
478
- []
479
- ), o = L(
480
- () => () => l.get(e, s).fnState,
481
- []
482
- ), d = m(u, o), p = (h) => {
483
- const x = l.get(e, s);
484
- x.fnState = { ...x.fnState, data: h }, l.callListeners(e, s);
485
- }, f = (h) => {
486
- const x = l.get(e, s);
487
- x.fnState = { ...x.fnState, isLoading: !1, data: void 0, error: h }, l.callListeners(e, s);
488
- }, b = () => {
489
- const h = l.get(e, s);
490
- h.fnState = { ...h.fnState, isLoading: !1 }, l.callListeners(e, s);
491
- }, g = async (h) => {
492
- const x = l.get(e, s);
493
- if (h && (await l.unsubscribe(e, s), x.fnState = { ...x.fnState, isLoading: !1, data: void 0, error: void 0, subscribed: !1 }), x.fnState.subscribed) return x.fnState;
494
- V("triggered !!"), x.fnState = { ...x.fnState, isLoading: !0, error: void 0 }, l.callListeners(e, s);
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);
495
371
  try {
496
- const E = await i(p, f, b), y = l.get(e, s);
497
- y.unsubscribe = E, y.fnState.subscribed = !0;
498
- } catch (E) {
499
- const y = l.get(e, s);
500
- y.fnState = { ...y.fnState, isLoading: !1, error: E };
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);
501
376
  }
502
- l.callListeners(e, s);
503
377
  };
504
- return l.useEffect(e, s), {
505
- state: d,
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,
506
384
  trigger: () => {
507
- g(!1);
385
+ S(!1);
508
386
  },
509
387
  forceTrigger: () => {
510
- g(!0);
388
+ S(!0);
511
389
  },
512
390
  unsubscribe: () => {
513
- l.unsubscribe(e, s);
391
+ F(e, a);
514
392
  }
515
393
  };
516
394
  }
517
395
  export {
518
- U as SharedFunctionsApi,
519
- J as SharedStatesApi,
520
- k as SharedStatesProvider,
521
- Y as SharedSubscriptionsApi,
522
- it as createSharedFunction,
523
- et as createSharedState,
524
- ft as createSharedSubscription,
525
- P as isDevMode,
526
- N as setDevMode,
527
- nt as sharedFunctionsApi,
528
- tt as sharedStatesApi,
529
- ot as sharedSubscriptionsApi,
530
- D as useSharedContext,
531
- at as useSharedFunction,
532
- rt as useSharedState,
533
- st as useSharedStateSelector,
534
- ct as useSharedSubscription
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,
405
+ j as useSharedContext,
406
+ ie as useSharedFunction,
407
+ re as useSharedState,
408
+ se as useSharedStateSelector,
409
+ oe as useSharedSubscription
535
410
  };
package/dist/main.min.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /*!
2
- * react-shared-states v1.0.23
2
+ * react-shared-states v2.0.1
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(l,E){typeof exports=="object"&&typeof module<"u"?E(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],E):(l=typeof globalThis<"u"?globalThis:l||self,E(l.ReactSharedStates={},l.jsxRuntime,l.React))})(this,(function(l,E,p){"use strict";l.isDevMode=!1;const B=a=>{l.isDevMode=a},V=(...a)=>{l.isDevMode&&console.log("%c[react-shared-states]","color: #007acc; font-weight: bold",...a)},y=a=>{if(!a)throw new Error("Value is empty");return a},O=()=>Math.random().toString(36).substring(2,15),P=p.createContext(void 0),$=({children:a,scopeName:t})=>{if(t&&t.includes("//"))throw new Error("scopeName cannot contain '//'");return t||(t=p.useMemo(()=>O(),[])),E.jsx(P.Provider,{value:{scopeName:t},children:a})},T=()=>p.useContext(P),C=[];class f{data=new Map;defaultValue(){return{}}addListener(t,r,e){this.data.has(f.prefix(t,r))||this.data.set(f.prefix(t,r),{...this.defaultValue(),listeners:[]}),this.data.get(f.prefix(t,r)).listeners.push(e)}removeListener(t,r,e){this.data.has(f.prefix(t,r))&&(this.data.get(f.prefix(t,r)).listeners=this.data.get(f.prefix(t,r)).listeners.filter(i=>i!==e))}callListeners(t,r){this.data.has(f.prefix(t,r))&&this.data.get(f.prefix(t,r)).listeners.forEach(e=>e())}init(t,r,e,i=!1){this.data.has(f.prefix(t,r))||this.data.set(f.prefix(t,r),{...e,isStatic:i,listeners:[]})}createStatic(t,r){const e=r??r??"_global",i={key:O(),prefix:e,...t};return C.push(i),this.initStatic(i),i}initStatic(t){const{key:r,prefix:e}=t;this.init(r,e,this.defaultValue(),!0)}clearAll(t=!1,r=!1){this.data.forEach((e,i)=>{const[n,s]=f.extractPrefix(i);this.clear(s,n,t,r)})}clear(t,r,e=!1,i=!1){e||this.callListeners(t,r);const n=this.data.get(f.prefix(t,r));if(!n)return;const s={...n};if(this.data.delete(f.prefix(t,r)),s.isStatic&&!i){const c=C.find(o=>o.key===t&&o.prefix===r);c&&this.initStatic(c)}}get(t,r){let e=this.has(t,r);if(e)return this.data.get(e)}setValue(t,r,e){this.data.has(f.prefix(t,r))&&this.data.set(f.prefix(t,r),{...this.data.get(f.prefix(t,r)),...e})}has(t,r){return this.data.has(f.prefix(t,r))?f.prefix(t,r):this.data.has(f.prefix(t,"_global"))?f.prefix(t,"_global"):void 0}static prefix(t,r){if(t.includes("//"))throw new Error("key cannot contain '//'");return`${r}//${t}`}static extractPrefix(t){return t.split("//")}useEffect(t,r,e=null){p.useEffect(()=>()=>{e?.(),V(`[${f.prefix(t,r)}]`,"unmount effect"),this.data.get(f.prefix(t,r)).listeners?.length===0&&this.clear(t,r)},[])}}class j{constructor(t){this.sharedData=t}get(t,r){let e,i=r;if(typeof t!="string"){const{key:c,prefix:o}=t;e=c,i=o}else e=y(t);const n=i||"_global";return this.sharedData.get(e,n)}set(t,r,e){let i,n=e;if(typeof t!="string"){const{key:c,prefix:o}=t;i=c,n=o}else i=y(t);const s=n||"_global";this.sharedData.init(i,s,r),this.sharedData.setValue(i,s,r),this.sharedData.callListeners(i,s)}update(t,r,e){let i;typeof t=="string"?i=this.get(t,e):i=this.get(t);const n=r(i);typeof t=="string"?this.set(t,n,e):this.set(t,n)}clearAll(){this.sharedData.clearAll()}clearScope(t){const r=t||"_global";this.sharedData.data.forEach((e,i)=>{const[n,s]=f.extractPrefix(i);if(n===r){this.sharedData.clear(s,n),this.sharedData.callListeners(s,n);return}})}resolve(t){const{key:r,prefix:e}=t;return this.get(r,e)}clear(t,r){let e,i;typeof t=="string"?(e=t,i=r||"_global"):(e=t.key,i=t.prefix),this.sharedData.clear(e,i)}has(t,r="_global"){const e=r||"_global";return!!this.sharedData.has(t,e)}getAll(){const t={};return this.sharedData.data.forEach((r,e)=>{const[i,n]=f.extractPrefix(e);t[i]=t[i]||{},t[i][n]=r}),t}subscribe(t,r,e){let i,n;return typeof t=="string"?(i=t,n=e||"_global"):(i=t.key,n=t.prefix),this.sharedData.addListener(i,n,r),()=>{this.sharedData.removeListener(i,n,r)}}}const w=a=>{const t=T();return{prefix:a??t?.scopeName??"_global"}};function z(a){return a&&a.__esModule&&Object.prototype.hasOwnProperty.call(a,"default")?a.default:a}var F,R;function G(){if(R)return F;R=1;var a=typeof Element<"u",t=typeof Map=="function",r=typeof Set=="function",e=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function i(n,s){if(n===s)return!0;if(n&&s&&typeof n=="object"&&typeof s=="object"){if(n.constructor!==s.constructor)return!1;var c,o,S;if(Array.isArray(n)){if(c=n.length,c!=s.length)return!1;for(o=c;o--!==0;)if(!i(n[o],s[o]))return!1;return!0}var h;if(t&&n instanceof Map&&s instanceof Map){if(n.size!==s.size)return!1;for(h=n.entries();!(o=h.next()).done;)if(!s.has(o.value[0]))return!1;for(h=n.entries();!(o=h.next()).done;)if(!i(o.value[1],s.get(o.value[0])))return!1;return!0}if(r&&n instanceof Set&&s instanceof Set){if(n.size!==s.size)return!1;for(h=n.entries();!(o=h.next()).done;)if(!s.has(o.value[0]))return!1;return!0}if(e&&ArrayBuffer.isView(n)&&ArrayBuffer.isView(s)){if(c=n.length,c!=s.length)return!1;for(o=c;o--!==0;)if(n[o]!==s[o])return!1;return!0}if(n.constructor===RegExp)return n.source===s.source&&n.flags===s.flags;if(n.valueOf!==Object.prototype.valueOf&&typeof n.valueOf=="function"&&typeof s.valueOf=="function")return n.valueOf()===s.valueOf();if(n.toString!==Object.prototype.toString&&typeof n.toString=="function"&&typeof s.toString=="function")return n.toString()===s.toString();if(S=Object.keys(n),c=S.length,c!==Object.keys(s).length)return!1;for(o=c;o--!==0;)if(!Object.prototype.hasOwnProperty.call(s,S[o]))return!1;if(a&&n instanceof Element)return!1;for(o=c;o--!==0;)if(!((S[o]==="_owner"||S[o]==="__v"||S[o]==="__o")&&n.$$typeof)&&!i(n[S[o]],s[S[o]]))return!1;return!0}return n!==n&&s!==s}return F=function(s,c){try{return i(s,c)}catch(o){if((o.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw o}},F}var W=G();const I=z(W);class K extends f{defaultValue(){return{value:void 0}}initValue(t,r,e,i=!1){super.init(t,r,{value:e},i)}initStatic(t){const{key:r,prefix:e,initialValue:i}=t;this.initValue(r,e,i,!0)}}class q extends j{constructor(t){super(t)}get(t,r="_global"){return typeof t!="string"?super.get(t)?.value:super.get(t,r)?.value}set(t,r,e="_global"){if(typeof t!="string"){super.set(t,{value:r});return}super.set(t,{value:r},e)}update(t,r,e="_global"){let i;typeof t=="string"?i=this.get(t,e):i=this.get(t);const n=r(i);typeof t=="string"?this.set(t,n,e):this.set(t,n)}}const v=new K,H=new q(v),J=(a,t)=>v.createStatic({initialValue:a},t);function Q(a,t,r){let e,i,n=r;if(typeof a!="string"){const{key:u,initialValue:m,prefix:x}=a;e=u,i=m,n=x}else e=y(a),i=t;const{prefix:s}=w(n);v.initValue(e,s,i);const c=p.useMemo(()=>u=>(v.initValue(e,s,t),v.addListener(e,s,u),()=>{v.removeListener(e,s,u)}),[]),o=p.useMemo(()=>()=>v.get(e,s)?.value,[]),S=p.useSyncExternalStore(c,o),h=u=>{const m=typeof u=="function"?u(v.get(e,s)?.value):u;m!==S&&(v.setValue(e,s,{value:m}),v.callListeners(e,s))};return v.useEffect(e,s),[S,h]}function U(a,t,r){let e,i=r;if(typeof a!="string"){const{key:h,prefix:u}=a;e=h,i=u}else e=y(a);const{prefix:n}=w(i),s=p.useRef(void 0),c=p.useMemo(()=>h=>(v.addListener(e,n,h),()=>{v.removeListener(e,n,h)}),[]),o=p.useMemo(()=>()=>{const h=v.get(e,n)?.value,u=t(h);return I(s.current,u)?s.current:u},[]),S=p.useSyncExternalStore(c,o);return v.useEffect(e,n),S}class X extends f{defaultValue(){return{fnState:{results:void 0,isLoading:!1,error:void 0}}}initValue(t,r,e=!1){super.init(t,r,this.defaultValue(),e)}setValue(t,r,e){super.setValue(t,r,e)}}class M extends j{constructor(t){super(t)}get(t,r="_global"){return typeof t!="string"?super.get(t)?.fnState:super.get(t,r)?.fnState}set(t,r,e="_global"){if(typeof t!="string"){super.set(t,r);return}super.set(t,r,e)}update(t,r,e="_global"){let i;typeof t=="string"?i=this.get(t,e):i=this.get(t);const n=r(i);typeof t=="string"?this.set(t,n,e):this.set(t,n)}}const b=new X,Y=new M(b),Z=(a,t)=>b.createStatic({fn:a},t);function N(a,t,r){let e,i,n=r;if(typeof a!="string"){const{key:u,fn:m,prefix:x}=a;e=u,i=m,n=x}else e=y(a),i=t;const{prefix:s}=w(n);b.initValue(e,s);const c=p.useMemo(()=>u=>(b.initValue(e,s),b.addListener(e,s,u),()=>{b.removeListener(e,s,u)}),[]),o=p.useMemo(()=>()=>b.get(e,s).fnState,[]),S=p.useSyncExternalStore(c,o),h=async(u,...m)=>{const x=b.get(e,s);if(!u&&(x.fnState.isLoading||x.fnState.results!==void 0))return x.fnState;x.fnState={...x.fnState,isLoading:!0,error:void 0},b.callListeners(e,s);try{const g=await i(...m);x.fnState={results:g,isLoading:!1,error:void 0}}catch(g){x.fnState={...x.fnState,isLoading:!1,error:g}}b.callListeners(e,s)};return b.useEffect(e,s),{state:S,trigger:(...u)=>{h(!1,...u)},forceTrigger:(...u)=>{h(!0,...u)},clear:()=>{const u=b.get(e,s);u&&(u.fnState=b.defaultValue().fnState,b.callListeners(e,s))}}}class k extends f{defaultValue(){return{fnState:{data:void 0,isLoading:!1,error:void 0,subscribed:!1}}}initValue(t,r,e=!1){super.init(t,r,this.defaultValue(),e)}setValue(t,r,e){super.setValue(t,r,e)}useEffect(t,r){p.useEffect(()=>()=>{V(`[${f.prefix(t,r)}]`,"unmount effect2"),this.get(t,r)?.listeners.length===0&&this.unsubscribe(t,r)},[]),super.useEffect(t,r)}async unsubscribe(t,r){const e=this.get(t,r);e&&(e.unsubscribe&&(e.unsubscribe(),e.unsubscribe=void 0),e.fnState={...e.fnState,subscribed:!1},this.callListeners(t,r))}}class _ extends j{constructor(t){super(t)}get(t,r="_global"){return typeof t!="string"?super.get(t)?.fnState:super.get(t,r)?.fnState}set(t,r,e="_global"){if(typeof t!="string"){super.set(t,r);return}super.set(t,r,e)}update(t,r,e="_global"){let i;typeof t=="string"?i=this.get(t,e):i=this.get(t);const n=r(i);typeof t=="string"?this.set(t,n,e):this.set(t,n)}}const d=new k,tt=new _(d),et=(a,t)=>d.createStatic({subscriber:a},t);function rt(a,t,r){let e,i,n=r;if(typeof a!="string"){const{key:g,subscriber:L,prefix:A}=a;e=g,i=L,n=A}else e=y(a),i=t;const{prefix:s}=w(n);d.initValue(e,s);const c=p.useMemo(()=>g=>(d.initValue(e,s),d.addListener(e,s,g),()=>{d.removeListener(e,s,g)}),[]),o=p.useMemo(()=>()=>d.get(e,s).fnState,[]),S=p.useSyncExternalStore(c,o),h=g=>{const L=d.get(e,s);L.fnState={...L.fnState,data:g},d.callListeners(e,s)},u=g=>{const L=d.get(e,s);L.fnState={...L.fnState,isLoading:!1,data:void 0,error:g},d.callListeners(e,s)},m=()=>{const g=d.get(e,s);g.fnState={...g.fnState,isLoading:!1},d.callListeners(e,s)},x=async g=>{const L=d.get(e,s);if(g&&(await d.unsubscribe(e,s),L.fnState={...L.fnState,isLoading:!1,data:void 0,error:void 0,subscribed:!1}),L.fnState.subscribed)return L.fnState;V("triggered !!"),L.fnState={...L.fnState,isLoading:!0,error:void 0},d.callListeners(e,s);try{const A=await i(h,u,m),D=d.get(e,s);D.unsubscribe=A,D.fnState.subscribed=!0}catch(A){const D=d.get(e,s);D.fnState={...D.fnState,isLoading:!1,error:A}}d.callListeners(e,s)};return d.useEffect(e,s),{state:S,trigger:()=>{x(!1)},forceTrigger:()=>{x(!0)},unsubscribe:()=>{d.unsubscribe(e,s)}}}l.SharedFunctionsApi=M,l.SharedStatesApi=q,l.SharedStatesProvider=$,l.SharedSubscriptionsApi=_,l.createSharedFunction=Z,l.createSharedState=J,l.createSharedSubscription=et,l.setDevMode=B,l.sharedFunctionsApi=Y,l.sharedStatesApi=H,l.sharedSubscriptionsApi=tt,l.useSharedContext=w,l.useSharedFunction=N,l.useSharedState=Q,l.useSharedStateSelector=U,l.useSharedSubscription=rt,Object.defineProperty(l,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),g=new h,J=new _(g),Q=(o,t)=>g.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);g.init(e,s,i);const a=d.useMemo(()=>f=>(g.init(e,s,t),g.addListener(e,s,f),()=>{g.removeListener(e,s,f)}),[e,s,t]),u=d.useMemo(()=>()=>g.get(e,s)?.value,[e,s]),p=d.useSyncExternalStore(a,u),l=f=>{const y=typeof f=="function"?f(g.get(e,s)?.value):f;z(y,p)||(g.setValue(e,s,y),g.callListeners(e,s))};return g.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=>(g.addListener(e,r,l),()=>{g.removeListener(e,r,l)}),[e,r]),u=d.useMemo(()=>()=>{const l=g.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 g.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 v=new h,E=new _(v),R={data:void 0,isLoading:!1,error:void 0,subscribed:!1,unsubscribe:void 0},N=(o,t,n)=>v.createStatic({subscriber:o},{...R,data:t?.initialValue},n);async function q(o,t){const n=v.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);v.init(e,a,R);const u=d.useMemo(()=>S=>(v.init(e,a,R),v.addListener(e,a,S),()=>{v.removeListener(e,a,S)}),[e,a]),p=d.useMemo(()=>()=>v.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=v.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"),v.get(e,a)?.listeners.length===0&&q(e,a)},[e,a]),v.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/dist/types.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export type PotentialPromise<T> = T | Promise<T>;
2
2
  export type AFunction<R = unknown, Args extends unknown[] = unknown[]> = (...args: Args) => PotentialPromise<R>;
3
3
  export type Prefix = "_global" | ({} & string);
4
- export interface SharedValue {
4
+ export interface SharedValue<T> {
5
+ value: T;
5
6
  listeners: AFunction[];
6
7
  isStatic?: true;
7
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-shared-states",
3
- "version": "1.0.23",
3
+ "version": "2.0.1",
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": [
@@ -47,7 +47,7 @@
47
47
  "react": "^19.1.1",
48
48
  "react-dom": "^19.1.1",
49
49
  "typescript": "^5.9.2",
50
- "vite": "7.1.7",
50
+ "vite": "7.1.9",
51
51
  "vite-plugin-banner": "^0.8.1",
52
52
  "vite-plugin-dts": "^4.5.4",
53
53
  "vitest": "^3.2.4"