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 +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 +11 -45
- package/dist/main.esm.js +289 -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,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
|
|
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
|
|
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>;
|
|
22
|
+
triggerImmediately?: boolean;
|
|
60
23
|
}
|
|
61
|
-
export declare const createSharedSubscription: <T>(subscriber: Subscriber<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
|
|
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
|
|
9
|
-
import { createContext as
|
|
10
|
-
let
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
},
|
|
14
|
-
|
|
8
|
+
import { jsx as M } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as T, useMemo as b, useContext as W, useEffect as P, useSyncExternalStore as _, useRef as k } from "react";
|
|
10
|
+
let z = !1;
|
|
11
|
+
const Z = (o) => {
|
|
12
|
+
z = o;
|
|
13
|
+
}, K = (...o) => {
|
|
14
|
+
z && console.log(
|
|
15
15
|
"%c[react-shared-states]",
|
|
16
16
|
"color: #007acc; font-weight: bold",
|
|
17
|
-
...
|
|
17
|
+
...o
|
|
18
18
|
);
|
|
19
|
-
},
|
|
20
|
-
if (!
|
|
21
|
-
return
|
|
22
|
-
},
|
|
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 =
|
|
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(() => 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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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,
|
|
51
|
-
const
|
|
52
|
-
key:
|
|
53
|
-
prefix:
|
|
51
|
+
createStatic(t, n, e) {
|
|
52
|
+
const i = e ?? "_global", r = {
|
|
53
|
+
key: G(),
|
|
54
|
+
prefix: i,
|
|
54
55
|
...t
|
|
55
56
|
};
|
|
56
|
-
return
|
|
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:
|
|
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(s,
|
|
65
|
+
const [r, s] = l.extractPrefix(i);
|
|
66
|
+
this.clear(s, r, 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 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,
|
|
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 = l.prefix(t, n), r = this.data.get(i);
|
|
85
|
+
r && (r.value = e, this.data.set(i, r));
|
|
88
86
|
}
|
|
89
|
-
has(t,
|
|
90
|
-
return this.data.has(
|
|
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,
|
|
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
|
+
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
|
|
106
|
+
class q {
|
|
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: a, prefix: c } = t;
|
|
114
|
+
e = a, i = c;
|
|
114
115
|
} else
|
|
115
|
-
e =
|
|
116
|
-
const
|
|
117
|
-
return
|
|
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,
|
|
120
|
-
let i,
|
|
127
|
+
set(t, n, e) {
|
|
128
|
+
let i, r = e;
|
|
121
129
|
if (typeof t != "string") {
|
|
122
|
-
const { key:
|
|
123
|
-
i =
|
|
130
|
+
const { key: a, prefix: c } = t;
|
|
131
|
+
i = a, r = c;
|
|
124
132
|
} else
|
|
125
|
-
i =
|
|
126
|
-
const s =
|
|
127
|
-
this.sharedData.init(i, s,
|
|
128
|
-
}
|
|
129
|
-
update(t,
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
|
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 [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:
|
|
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, r] = l.extractPrefix(e);
|
|
170
|
+
t[i] = t[i] || {}, t[i][r] = 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, 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
|
|
194
|
-
const t =
|
|
180
|
+
const j = (o) => {
|
|
181
|
+
const t = H();
|
|
195
182
|
return {
|
|
196
|
-
prefix:
|
|
183
|
+
prefix: o ?? t?.scopeName ?? "_global"
|
|
197
184
|
};
|
|
198
185
|
};
|
|
199
|
-
function
|
|
200
|
-
return
|
|
186
|
+
function J(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, 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
|
|
219
|
-
if (t &&
|
|
220
|
-
if (
|
|
221
|
-
for (
|
|
222
|
-
if (!s.has(
|
|
223
|
-
for (
|
|
224
|
-
if (!i(
|
|
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 (
|
|
228
|
-
if (
|
|
229
|
-
for (
|
|
230
|
-
if (!s.has(
|
|
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(
|
|
234
|
-
if (
|
|
235
|
-
for (
|
|
236
|
-
if (
|
|
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 (
|
|
240
|
-
if (
|
|
241
|
-
if (
|
|
242
|
-
if (d = Object.keys(
|
|
243
|
-
for (
|
|
244
|
-
if (!Object.prototype.hasOwnProperty.call(s, d[
|
|
245
|
-
if (
|
|
246
|
-
for (
|
|
247
|
-
if (!((d[
|
|
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
|
|
238
|
+
return r !== r && s !== s;
|
|
252
239
|
}
|
|
253
|
-
return
|
|
240
|
+
return C = function(s, a) {
|
|
254
241
|
try {
|
|
255
|
-
return i(s,
|
|
256
|
-
} catch (
|
|
257
|
-
if ((
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
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 =
|
|
306
|
-
const { prefix: s } =
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
}), []),
|
|
311
|
-
const
|
|
312
|
-
|
|
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
|
|
267
|
+
return h.useEffect(e, s), [
|
|
315
268
|
d,
|
|
316
|
-
|
|
269
|
+
u
|
|
317
270
|
];
|
|
318
271
|
}
|
|
319
|
-
function
|
|
320
|
-
let e, i =
|
|
321
|
-
if (typeof
|
|
322
|
-
const { key:
|
|
323
|
-
e =
|
|
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 =
|
|
326
|
-
const { prefix:
|
|
327
|
-
|
|
328
|
-
}), []),
|
|
329
|
-
const
|
|
330
|
-
return
|
|
331
|
-
}, []), d =
|
|
332
|
-
return
|
|
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
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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 =
|
|
380
|
-
const { prefix: s } =
|
|
381
|
-
|
|
382
|
-
const
|
|
383
|
-
() => (f) => (
|
|
384
|
-
|
|
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
|
-
),
|
|
388
|
-
() => () =>
|
|
389
|
-
[]
|
|
390
|
-
), d =
|
|
391
|
-
const
|
|
392
|
-
if (!f && (
|
|
393
|
-
|
|
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
|
|
396
|
-
|
|
397
|
-
} catch (
|
|
398
|
-
|
|
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
|
|
320
|
+
return y.useEffect(e, s), {
|
|
403
321
|
state: d,
|
|
404
322
|
trigger: (...f) => {
|
|
405
|
-
|
|
323
|
+
u(!1, ...f);
|
|
406
324
|
},
|
|
407
325
|
forceTrigger: (...f) => {
|
|
408
|
-
|
|
326
|
+
u(!0, ...f);
|
|
409
327
|
},
|
|
410
328
|
clear: () => {
|
|
411
|
-
|
|
412
|
-
f && (f.fnState = v.defaultValue().fnState, v.callListeners(e, s));
|
|
329
|
+
A.set(e, O, s);
|
|
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
|
-
}
|
|
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
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
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 =
|
|
472
|
-
const { prefix:
|
|
473
|
-
|
|
474
|
-
const
|
|
475
|
-
() => (
|
|
476
|
-
|
|
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
|
-
),
|
|
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, 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
|
|
497
|
-
|
|
498
|
-
} catch (
|
|
499
|
-
|
|
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
|
|
505
|
-
|
|
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
|
-
|
|
385
|
+
S(!1);
|
|
508
386
|
},
|
|
509
387
|
forceTrigger: () => {
|
|
510
|
-
|
|
388
|
+
S(!0);
|
|
511
389
|
},
|
|
512
390
|
unsubscribe: () => {
|
|
513
|
-
|
|
391
|
+
F(e, a);
|
|
514
392
|
}
|
|
515
393
|
};
|
|
516
394
|
}
|
|
517
395
|
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
|
|
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
|
|
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": "
|
|
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.
|
|
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"
|