react-shared-states 1.0.22 → 2.0.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.
- package/README.md +2 -2
- package/dist/SharedValuesManager.d.ts +23 -64
- package/dist/hooks/index.d.ts +3 -3
- package/dist/hooks/use-shared-function.d.ts +4 -41
- package/dist/hooks/use-shared-state.d.ts +3 -27
- package/dist/hooks/use-shared-subscription.d.ts +6 -44
- package/dist/main.esm.js +287 -414
- package/dist/main.min.js +2 -2
- package/dist/types.d.ts +2 -1
- package/package.json +2 -2
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
|
|
4
|
-
|
|
5
|
-
|
|
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,
|
|
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,
|
|
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
|
|
25
|
-
protected sharedData: SharedValuesManager<T
|
|
26
|
-
constructor(sharedData: SharedValuesManager<T
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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
|
}
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { useSharedState, sharedStatesApi, createSharedState,
|
|
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,
|
|
3
|
+
export { useSharedFunction, sharedFunctionsApi, createSharedFunction, } from './use-shared-function';
|
|
4
4
|
export type { SharedFunctionStateReturn } from './use-shared-function';
|
|
5
|
-
export { useSharedSubscription, sharedSubscriptionsApi, createSharedSubscription,
|
|
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
|
|
2
|
-
import { SharedValuesApi
|
|
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
|
-
|
|
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
|
|
2
|
-
import { SharedValuesApi
|
|
3
|
-
|
|
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
|
|
2
|
-
import { SharedValuesApi
|
|
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,20 @@ 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> = {
|
|
10
|
+
export type SharedSubscriptionValue<T> = {
|
|
11
11
|
data?: T;
|
|
12
12
|
isLoading: boolean;
|
|
13
13
|
error?: unknown;
|
|
14
14
|
subscribed?: boolean;
|
|
15
15
|
};
|
|
16
|
-
interface SharedSubscription<T> extends
|
|
17
|
-
fnState: SharedSubscriptionValue<T>;
|
|
16
|
+
interface SharedSubscription<T> extends SharedSubscriptionValue<T> {
|
|
18
17
|
unsubscribe?: Unsubscribe | void;
|
|
19
18
|
}
|
|
20
|
-
declare
|
|
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>;
|
|
60
22
|
}
|
|
61
|
-
export declare const createSharedSubscription: <T
|
|
23
|
+
export declare const createSharedSubscription: <T>(subscriber: Subscriber<T>, scopeName?: Prefix) => SharedSubscriptionCreated<T>;
|
|
62
24
|
export type SharedSubscriptionStateReturn<T> = {
|
|
63
25
|
readonly state: NonNullable<SharedSubscriptionValue<T>>;
|
|
64
26
|
readonly trigger: () => void;
|
package/dist/main.esm.js
CHANGED
|
@@ -1,535 +1,408 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-shared-states
|
|
2
|
+
* react-shared-states v2.0.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
|
|
9
|
-
import { createContext as
|
|
10
|
-
let
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
},
|
|
14
|
-
|
|
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(
|
|
15
15
|
"%c[react-shared-states]",
|
|
16
16
|
"color: #007acc; font-weight: bold",
|
|
17
|
-
...
|
|
17
|
+
...o
|
|
18
18
|
);
|
|
19
|
-
}, w = (
|
|
20
|
-
if (!
|
|
21
|
-
return
|
|
22
|
-
},
|
|
19
|
+
}, w = (o) => {
|
|
20
|
+
if (!o) throw new Error("Value is empty");
|
|
21
|
+
return o;
|
|
22
|
+
}, z = () => Math.random().toString(36).substring(2, 15), G = V(void 0), Z = ({ children: o, scopeName: t }) => {
|
|
23
23
|
if (t && t.includes("//")) throw new Error("scopeName cannot contain '//'");
|
|
24
|
-
return t || (t =
|
|
25
|
-
},
|
|
26
|
-
class
|
|
27
|
-
|
|
28
|
-
|
|
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(() => z(), [])), /* @__PURE__ */ T(G.Provider, { value: { scopeName: t }, children: o });
|
|
25
|
+
}, k = () => W(G), $ = [];
|
|
26
|
+
class d {
|
|
27
|
+
constructor(t = () => null) {
|
|
28
|
+
this.defaultValue = t;
|
|
39
29
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
30
|
+
data = /* @__PURE__ */ new Map();
|
|
31
|
+
addListener(t, n, e) {
|
|
32
|
+
const i = d.prefix(t, n), s = this.data.get(i);
|
|
33
|
+
s && s.listeners.push(e);
|
|
34
|
+
}
|
|
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));
|
|
38
|
+
}
|
|
39
|
+
callListeners(t, n) {
|
|
40
|
+
const e = d.prefix(t, n), i = this.data.get(e);
|
|
41
|
+
i && i.listeners.forEach((s) => s());
|
|
42
|
+
}
|
|
43
|
+
init(t, n, e, i = !1) {
|
|
44
|
+
const s = d.prefix(t, n);
|
|
45
|
+
this.data.has(s) || this.data.set(s, {
|
|
46
|
+
value: e,
|
|
47
|
+
isStatic: i || void 0,
|
|
47
48
|
listeners: []
|
|
48
49
|
});
|
|
49
50
|
}
|
|
50
|
-
createStatic(t,
|
|
51
|
-
const
|
|
52
|
-
key:
|
|
53
|
-
prefix:
|
|
51
|
+
createStatic(t, n, e) {
|
|
52
|
+
const i = e ?? "_global", s = {
|
|
53
|
+
key: z(),
|
|
54
|
+
prefix: i,
|
|
54
55
|
...t
|
|
55
56
|
};
|
|
56
|
-
return
|
|
57
|
+
return $.push(s), this.init(s.key, s.prefix, n, !0), this.defaultValue = () => n, s;
|
|
57
58
|
}
|
|
58
59
|
initStatic(t) {
|
|
59
|
-
const { key:
|
|
60
|
-
this.init(
|
|
60
|
+
const { key: n, prefix: e } = t;
|
|
61
|
+
this.init(n, e, this.defaultValue(), !0);
|
|
61
62
|
}
|
|
62
|
-
clearAll(t = !1,
|
|
63
|
+
clearAll(t = !1, n = !1) {
|
|
63
64
|
this.data.forEach((e, i) => {
|
|
64
|
-
const [
|
|
65
|
-
this.clear(
|
|
65
|
+
const [s, r] = d.extractPrefix(i);
|
|
66
|
+
this.clear(r, s, t, n);
|
|
66
67
|
});
|
|
67
68
|
}
|
|
68
|
-
clear(t,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
u && this.initStatic(u);
|
|
69
|
+
clear(t, n, e = !1, i = !1) {
|
|
70
|
+
const s = d.prefix(t, n);
|
|
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);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
-
get(t,
|
|
79
|
-
let e = this.has(t,
|
|
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,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
...e
|
|
87
|
-
});
|
|
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));
|
|
88
86
|
}
|
|
89
|
-
has(t,
|
|
90
|
-
return this.data.has(
|
|
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;
|
|
91
89
|
}
|
|
92
|
-
static prefix(t,
|
|
90
|
+
static prefix(t, n) {
|
|
93
91
|
if (t.includes("//")) throw new Error("key cannot contain '//'");
|
|
94
|
-
return `${
|
|
92
|
+
return `${n}//${t}`;
|
|
95
93
|
}
|
|
96
94
|
static extractPrefix(t) {
|
|
97
|
-
|
|
95
|
+
const n = t.split("//");
|
|
96
|
+
return [n[0], n.slice(1).join("//")];
|
|
98
97
|
}
|
|
99
|
-
useEffect(t,
|
|
100
|
-
|
|
101
|
-
e?.(),
|
|
102
|
-
|
|
98
|
+
useEffect(t, n, e = null) {
|
|
99
|
+
B(() => () => {
|
|
100
|
+
e?.(), P(`[${d.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
|
|
106
|
+
class K {
|
|
106
107
|
constructor(t) {
|
|
107
108
|
this.sharedData = t;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
let e, i =
|
|
110
|
+
_get(t, n) {
|
|
111
|
+
let e, i = n;
|
|
111
112
|
if (typeof t != "string") {
|
|
112
|
-
const { key:
|
|
113
|
-
e =
|
|
113
|
+
const { key: c, prefix: a } = t;
|
|
114
|
+
e = c, i = a;
|
|
114
115
|
} else
|
|
115
116
|
e = w(t);
|
|
116
|
-
const
|
|
117
|
-
return
|
|
117
|
+
const s = i || "_global", r = this.sharedData.get(e, s);
|
|
118
|
+
return r ? { value: r.value, key: e, prefix: s } : {
|
|
119
|
+
key: e,
|
|
120
|
+
prefix: s,
|
|
121
|
+
value: void 0
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
get(t, n) {
|
|
125
|
+
return this._get(t, n).value;
|
|
118
126
|
}
|
|
119
|
-
set(t,
|
|
120
|
-
let i,
|
|
127
|
+
set(t, n, e) {
|
|
128
|
+
let i, s = e;
|
|
121
129
|
if (typeof t != "string") {
|
|
122
|
-
const { key:
|
|
123
|
-
i =
|
|
130
|
+
const { key: c, prefix: a } = t;
|
|
131
|
+
i = c, s = a;
|
|
124
132
|
} else
|
|
125
133
|
i = w(t);
|
|
126
|
-
const
|
|
127
|
-
this.sharedData.init(i,
|
|
128
|
-
}
|
|
129
|
-
update(t,
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
const r = s || "_global";
|
|
135
|
+
this.sharedData.init(i, r, n), this.sharedData.setValue(i, r, n), this.sharedData.callListeners(i, r);
|
|
136
|
+
}
|
|
137
|
+
update(t, n, e) {
|
|
138
|
+
const i = this._get(t, e);
|
|
139
|
+
if (i) {
|
|
140
|
+
const s = n(i.value);
|
|
141
|
+
this.set(i.key, s, 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
|
|
148
|
+
const n = t || "_global";
|
|
147
149
|
this.sharedData.data.forEach((e, i) => {
|
|
148
|
-
const [
|
|
149
|
-
|
|
150
|
-
this.sharedData.clear(s, n), this.sharedData.callListeners(s, n);
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
150
|
+
const [s, r] = d.extractPrefix(i);
|
|
151
|
+
s === n && (this.sharedData.clear(r, s), this.sharedData.callListeners(r, s));
|
|
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:
|
|
161
|
-
return this.get(
|
|
155
|
+
const { key: n, prefix: e } = t;
|
|
156
|
+
return this.get(n, e);
|
|
162
157
|
}
|
|
163
|
-
clear(t,
|
|
158
|
+
clear(t, n) {
|
|
164
159
|
let e, i;
|
|
165
|
-
typeof t == "string" ? (e = t, 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
|
-
|
|
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((
|
|
182
|
-
const [i,
|
|
183
|
-
t[i] = t[i] || {}, t[i][
|
|
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;
|
|
184
171
|
}), t;
|
|
185
172
|
}
|
|
186
|
-
subscribe(t,
|
|
187
|
-
let i,
|
|
188
|
-
return typeof t == "string" ? (i = t,
|
|
189
|
-
this.sharedData.removeListener(i,
|
|
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);
|
|
190
177
|
};
|
|
191
178
|
}
|
|
192
179
|
}
|
|
193
|
-
const
|
|
194
|
-
const t =
|
|
180
|
+
const j = (o) => {
|
|
181
|
+
const t = k();
|
|
195
182
|
return {
|
|
196
|
-
prefix:
|
|
183
|
+
prefix: o ?? t?.scopeName ?? "_global"
|
|
197
184
|
};
|
|
198
185
|
};
|
|
199
|
-
function
|
|
200
|
-
return
|
|
186
|
+
function H(o) {
|
|
187
|
+
return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
|
|
201
188
|
}
|
|
202
|
-
var
|
|
203
|
-
function
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
var
|
|
207
|
-
function i(
|
|
208
|
-
if (
|
|
209
|
-
if (
|
|
210
|
-
if (
|
|
211
|
-
var
|
|
212
|
-
if (Array.isArray(
|
|
213
|
-
if (
|
|
214
|
-
for (
|
|
215
|
-
if (!i(
|
|
189
|
+
var C, q;
|
|
190
|
+
function J() {
|
|
191
|
+
if (q) return C;
|
|
192
|
+
q = 1;
|
|
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;
|
|
216
203
|
return !0;
|
|
217
204
|
}
|
|
218
|
-
var
|
|
219
|
-
if (t &&
|
|
220
|
-
if (
|
|
221
|
-
for (
|
|
222
|
-
if (!
|
|
223
|
-
for (
|
|
224
|
-
if (!i(
|
|
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;
|
|
225
212
|
return !0;
|
|
226
213
|
}
|
|
227
|
-
if (
|
|
228
|
-
if (
|
|
229
|
-
for (
|
|
230
|
-
if (!
|
|
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;
|
|
231
218
|
return !0;
|
|
232
219
|
}
|
|
233
|
-
if (e && ArrayBuffer.isView(
|
|
234
|
-
if (
|
|
235
|
-
for (
|
|
236
|
-
if (
|
|
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;
|
|
237
224
|
return !0;
|
|
238
225
|
}
|
|
239
|
-
if (
|
|
240
|
-
if (
|
|
241
|
-
if (
|
|
242
|
-
if (
|
|
243
|
-
for (
|
|
244
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
245
|
-
if (
|
|
246
|
-
for (
|
|
247
|
-
if (!((
|
|
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]]))
|
|
248
235
|
return !1;
|
|
249
236
|
return !0;
|
|
250
237
|
}
|
|
251
|
-
return
|
|
238
|
+
return s !== s && r !== r;
|
|
252
239
|
}
|
|
253
|
-
return
|
|
240
|
+
return C = function(r, c) {
|
|
254
241
|
try {
|
|
255
|
-
return i(
|
|
256
|
-
} catch (
|
|
257
|
-
if ((
|
|
242
|
+
return i(r, c);
|
|
243
|
+
} catch (a) {
|
|
244
|
+
if ((a.message || "").match(/stack|recursion/i))
|
|
258
245
|
return console.warn("react-fast-compare cannot handle circular refs"), !1;
|
|
259
|
-
throw
|
|
246
|
+
throw a;
|
|
260
247
|
}
|
|
261
|
-
},
|
|
248
|
+
}, C;
|
|
262
249
|
}
|
|
263
|
-
var
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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;
|
|
288
|
-
}
|
|
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
|
-
}
|
|
297
|
-
}
|
|
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 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;
|
|
254
|
+
if (typeof o != "string") {
|
|
255
|
+
const { key: f, initialValue: v, prefix: x } = o;
|
|
256
|
+
e = f, i = v, s = x;
|
|
304
257
|
} else
|
|
305
|
-
e = w(
|
|
306
|
-
const { prefix:
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
}), []),
|
|
311
|
-
const
|
|
312
|
-
|
|
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));
|
|
313
266
|
};
|
|
314
|
-
return
|
|
315
|
-
|
|
316
|
-
|
|
267
|
+
return h.useEffect(e, r), [
|
|
268
|
+
p,
|
|
269
|
+
l
|
|
317
270
|
];
|
|
318
271
|
}
|
|
319
|
-
function
|
|
320
|
-
let e, i =
|
|
321
|
-
if (typeof
|
|
322
|
-
const { key:
|
|
323
|
-
e =
|
|
272
|
+
function re(o, t, n) {
|
|
273
|
+
let e, i = n;
|
|
274
|
+
if (typeof o != "string") {
|
|
275
|
+
const { key: l, prefix: f } = o;
|
|
276
|
+
e = l, i = f;
|
|
324
277
|
} else
|
|
325
|
-
e = w(
|
|
326
|
-
const { prefix:
|
|
327
|
-
|
|
328
|
-
}), []),
|
|
329
|
-
const
|
|
330
|
-
return
|
|
331
|
-
}, []),
|
|
332
|
-
return
|
|
333
|
-
}
|
|
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
|
-
}
|
|
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;
|
|
371
286
|
}
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
287
|
+
const y = new d(), E = new K(y), D = {
|
|
288
|
+
results: void 0,
|
|
289
|
+
isLoading: !1,
|
|
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;
|
|
294
|
+
if (typeof o != "string") {
|
|
295
|
+
const { key: f, fn: v, prefix: x } = o;
|
|
296
|
+
e = f, i = v, s = x;
|
|
378
297
|
} else
|
|
379
|
-
e = w(
|
|
380
|
-
const { prefix:
|
|
381
|
-
|
|
382
|
-
const
|
|
383
|
-
() => (f) => (
|
|
384
|
-
|
|
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);
|
|
385
304
|
}),
|
|
386
|
-
[]
|
|
387
|
-
),
|
|
388
|
-
() => () =>
|
|
389
|
-
[]
|
|
390
|
-
),
|
|
391
|
-
const
|
|
392
|
-
if (!f && (
|
|
393
|
-
|
|
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);
|
|
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);
|
|
394
313
|
try {
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
} catch (
|
|
398
|
-
|
|
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);
|
|
399
318
|
}
|
|
400
|
-
v.callListeners(e, s);
|
|
401
319
|
};
|
|
402
|
-
return
|
|
403
|
-
state:
|
|
320
|
+
return y.useEffect(e, r), {
|
|
321
|
+
state: p,
|
|
404
322
|
trigger: (...f) => {
|
|
405
|
-
|
|
323
|
+
l(!1, ...f);
|
|
406
324
|
},
|
|
407
325
|
forceTrigger: (...f) => {
|
|
408
|
-
|
|
326
|
+
l(!0, ...f);
|
|
409
327
|
},
|
|
410
328
|
clear: () => {
|
|
411
|
-
|
|
412
|
-
f && (f.fnState = v.defaultValue().fnState, v.callListeners(e, s));
|
|
329
|
+
E.set(e, D, r);
|
|
413
330
|
}
|
|
414
331
|
};
|
|
415
332
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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
|
-
}
|
|
442
|
-
}
|
|
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
|
-
}
|
|
333
|
+
const g = new d(() => A), m = new K(g), A = {
|
|
334
|
+
data: void 0,
|
|
335
|
+
isLoading: !1,
|
|
336
|
+
error: void 0,
|
|
337
|
+
subscribed: !1,
|
|
338
|
+
unsubscribe: void 0
|
|
339
|
+
}, ie = (o, t) => g.createStatic({ subscriber: o }, A, t);
|
|
340
|
+
async function F(o, t) {
|
|
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));
|
|
463
343
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
e = h, i = x, n = E;
|
|
344
|
+
function oe(o, t, n) {
|
|
345
|
+
let e, i, s = n;
|
|
346
|
+
if (typeof o != "string") {
|
|
347
|
+
const { key: u, subscriber: S, prefix: L } = o;
|
|
348
|
+
e = u, i = S, s = L;
|
|
470
349
|
} else
|
|
471
|
-
e = w(
|
|
472
|
-
const { prefix:
|
|
473
|
-
|
|
474
|
-
const
|
|
475
|
-
() => (
|
|
476
|
-
|
|
350
|
+
e = w(o), i = t;
|
|
351
|
+
const { prefix: r } = j(s);
|
|
352
|
+
g.init(e, r, A);
|
|
353
|
+
const c = b(
|
|
354
|
+
() => (u) => (g.init(e, r, A), g.addListener(e, r, u), () => {
|
|
355
|
+
g.removeListener(e, r, u);
|
|
477
356
|
}),
|
|
478
|
-
[]
|
|
479
|
-
),
|
|
480
|
-
() => () =>
|
|
481
|
-
[]
|
|
482
|
-
),
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
},
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
|
|
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, 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);
|
|
495
371
|
try {
|
|
496
|
-
const
|
|
497
|
-
|
|
498
|
-
} catch (
|
|
499
|
-
|
|
500
|
-
y.fnState = { ...y.fnState, isLoading: !1, error: E };
|
|
372
|
+
const L = await i(l, f, v);
|
|
373
|
+
m.update(e, (_) => ({ ..._, unsubscribe: L, subscribed: !0, isLoading: !1 }), r);
|
|
374
|
+
} catch (L) {
|
|
375
|
+
m.update(e, (_) => ({ ..._, isLoading: !1, error: L }), r);
|
|
501
376
|
}
|
|
502
|
-
l.callListeners(e, s);
|
|
503
377
|
};
|
|
504
|
-
return
|
|
505
|
-
|
|
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,
|
|
506
382
|
trigger: () => {
|
|
507
|
-
|
|
383
|
+
x(!1);
|
|
508
384
|
},
|
|
509
385
|
forceTrigger: () => {
|
|
510
|
-
|
|
386
|
+
x(!0);
|
|
511
387
|
},
|
|
512
388
|
unsubscribe: () => {
|
|
513
|
-
|
|
389
|
+
F(e, r);
|
|
514
390
|
}
|
|
515
391
|
};
|
|
516
392
|
}
|
|
517
393
|
export {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
rt as useSharedState,
|
|
533
|
-
st as useSharedStateSelector,
|
|
534
|
-
ct as useSharedSubscription
|
|
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,
|
|
403
|
+
j as useSharedContext,
|
|
404
|
+
ne as useSharedFunction,
|
|
405
|
+
te as useSharedState,
|
|
406
|
+
re as useSharedStateSelector,
|
|
407
|
+
oe as useSharedSubscription
|
|
535
408
|
};
|
package/dist/main.min.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-shared-states
|
|
2
|
+
* react-shared-states v2.0.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(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(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"})}));
|
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": "
|
|
3
|
+
"version": "2.0.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": [
|
|
@@ -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.
|
|
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"
|