react-shared-states 1.0.7 → 1.0.11
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/.editorconfig +3 -0
- package/README.md +519 -70
- package/dist/SharedValuesManager.d.ts +73 -0
- package/dist/context/SharedStatesContext.d.ts +5 -4
- package/dist/hooks/index.d.ts +6 -3
- package/dist/hooks/use-shared-function.d.ts +44 -17
- package/dist/hooks/use-shared-state.d.ts +24 -4
- package/dist/hooks/use-shared-subscription.d.ts +51 -22
- package/dist/lib/utils.d.ts +2 -2
- package/dist/main.esm.js +411 -355
- package/dist/main.min.js +5 -5
- package/dist/types.d.ts +6 -2
- package/package.json +6 -2
- package/tests/index.test.tsx +526 -0
- package/vitest.config.ts +8 -0
- package/dist/SharedData.d.ts +0 -61
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AFunction, Prefix, SharedCreated, SharedValue } from './types';
|
|
2
|
+
export declare const staticStores: SharedCreated[];
|
|
3
|
+
export declare abstract class SharedValuesManager<T extends SharedValue, V> {
|
|
4
|
+
data: Map<string, T>;
|
|
5
|
+
defaultValue(): V;
|
|
6
|
+
addListener(key: string, prefix: Prefix, listener: AFunction): void;
|
|
7
|
+
removeListener(key: string, prefix: Prefix, listener: AFunction): void;
|
|
8
|
+
callListeners(key: string, prefix: Prefix): void;
|
|
9
|
+
init(key: string, prefix: Prefix, data: V, isStatic?: boolean): void;
|
|
10
|
+
createStatic<X extends SharedCreated>(rest: Omit<X, 'key' | 'prefix'>, scopeName?: Prefix): {
|
|
11
|
+
key: string;
|
|
12
|
+
prefix: Prefix;
|
|
13
|
+
} & Omit<X, "key" | "prefix">;
|
|
14
|
+
initStatic(sharedCreated: SharedCreated): void;
|
|
15
|
+
clearAll(withoutListeners?: boolean, withStatic?: boolean): void;
|
|
16
|
+
clear(key: string, prefix: Prefix, withoutListeners?: boolean, withStatic?: boolean): void;
|
|
17
|
+
get(key: string, prefix: Prefix): T | undefined;
|
|
18
|
+
setValue(key: string, prefix: Prefix, data: V): void;
|
|
19
|
+
has(key: string, prefix: Prefix): string | undefined;
|
|
20
|
+
static prefix(key: string, prefix: Prefix): string;
|
|
21
|
+
static extractPrefix(mapKey: string): string[];
|
|
22
|
+
useEffect(key: string, prefix: Prefix, unsub?: (() => void) | null): void;
|
|
23
|
+
}
|
|
24
|
+
export declare class SharedValuesApi<T extends SharedValue, V, R = T> {
|
|
25
|
+
protected sharedData: SharedValuesManager<T, V>;
|
|
26
|
+
constructor(sharedData: SharedValuesManager<T, V>);
|
|
27
|
+
/**
|
|
28
|
+
* get a value from the shared data
|
|
29
|
+
* @param key
|
|
30
|
+
* @param scopeName
|
|
31
|
+
*/
|
|
32
|
+
get<S extends string = string>(key: S, scopeName: Prefix): R;
|
|
33
|
+
get<S extends string = string>(sharedCreated: SharedCreated): R;
|
|
34
|
+
/**
|
|
35
|
+
* set a value in the shared data
|
|
36
|
+
* @param key
|
|
37
|
+
* @param value
|
|
38
|
+
* @param scopeName
|
|
39
|
+
*/
|
|
40
|
+
set<S extends string = string>(key: S, value: V, scopeName: Prefix): void;
|
|
41
|
+
set<S extends string = string>(sharedCreated: SharedCreated, value: V): void;
|
|
42
|
+
/**
|
|
43
|
+
* clear all values from the shared data
|
|
44
|
+
*/
|
|
45
|
+
clearAll(): void;
|
|
46
|
+
/**
|
|
47
|
+
* clear all values from the shared data in a scope
|
|
48
|
+
* @param scopeName
|
|
49
|
+
*/
|
|
50
|
+
clearScope(scopeName?: Prefix): void;
|
|
51
|
+
/**
|
|
52
|
+
* resolve a shared created object to a value
|
|
53
|
+
* @param sharedCreated
|
|
54
|
+
*/
|
|
55
|
+
resolve(sharedCreated: SharedCreated): R;
|
|
56
|
+
/**
|
|
57
|
+
* clear a value from the shared data
|
|
58
|
+
* @param key
|
|
59
|
+
* @param scopeName
|
|
60
|
+
*/
|
|
61
|
+
clear(key: string, scopeName: Prefix): void;
|
|
62
|
+
clear(sharedCreated: SharedCreated): void;
|
|
63
|
+
/**
|
|
64
|
+
* check if a value exists in the shared data
|
|
65
|
+
* @param key
|
|
66
|
+
* @param scopeName
|
|
67
|
+
*/
|
|
68
|
+
has(key: string, scopeName?: Prefix): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* get all values from the shared data
|
|
71
|
+
*/
|
|
72
|
+
getAll(): Record<string, Record<string, any>>;
|
|
73
|
+
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { Prefix } from '../types';
|
|
3
3
|
export interface SharedStatesType {
|
|
4
4
|
scopeName: string;
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
export declare const SharedStatesContext: import('react').Context<SharedStatesType | undefined>;
|
|
7
|
+
interface SharedStatesProviderProps extends PropsWithChildren {
|
|
8
|
+
scopeName?: Prefix;
|
|
8
9
|
}
|
|
9
|
-
export declare const SharedStatesProvider:
|
|
10
|
+
export declare const SharedStatesProvider: ({ children, scopeName }: SharedStatesProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
10
11
|
export declare const useSharedStatesContext: () => SharedStatesType | undefined;
|
|
11
12
|
export {};
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export { useSharedState, sharedStatesApi } from './use-shared-state';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { useSharedState, sharedStatesApi, createSharedState, SharedStatesApi } from './use-shared-state';
|
|
2
|
+
export type { SharedStateCreated } from './use-shared-state';
|
|
3
|
+
export { useSharedFunction, sharedFunctionsApi, createSharedFunction, SharedFunctionsApi } from './use-shared-function';
|
|
4
|
+
export type { SharedFunctionStateReturn } from './use-shared-function';
|
|
5
|
+
export { useSharedSubscription, sharedSubscriptionsApi, createSharedSubscription, SharedSubscriptionsApi } from './use-shared-subscription';
|
|
6
|
+
export type { SharedSubscriptionStateReturn } from './use-shared-subscription';
|
|
@@ -1,25 +1,52 @@
|
|
|
1
|
-
import { AFunction, Prefix } from '../types';
|
|
2
|
-
import {
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
error?: unknown;
|
|
8
|
-
};
|
|
1
|
+
import { AFunction, Prefix, SharedCreated, SharedValue } from '../types';
|
|
2
|
+
import { SharedValuesApi, SharedValuesManager } from '../SharedValuesManager';
|
|
3
|
+
type SharedFunctionValue<T> = {
|
|
4
|
+
results?: T;
|
|
5
|
+
isLoading: boolean;
|
|
6
|
+
error?: unknown;
|
|
9
7
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
set<T, S extends string = string>(key: S, fnState: SharedFunctionsState<T>, scopeName?: Prefix): void;
|
|
8
|
+
interface SharedFunction<T> extends SharedValue {
|
|
9
|
+
fnState: SharedFunctionValue<T>;
|
|
13
10
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
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
|
+
}
|
|
39
|
+
export declare const sharedFunctionsApi: SharedFunctionsApi;
|
|
40
|
+
interface SharedFunctionCreated<T, Args extends unknown[]> extends SharedCreated {
|
|
41
|
+
fn: AFunction<T, Args>;
|
|
42
|
+
}
|
|
43
|
+
export declare const createSharedFunction: <T, Args extends unknown[]>(fn: AFunction<T, Args>, scopeName?: Prefix) => SharedFunctionCreated<T, Args>;
|
|
44
|
+
export type SharedFunctionStateReturn<T, Args extends unknown[]> = {
|
|
45
|
+
readonly state: NonNullable<SharedFunctionValue<T>>;
|
|
21
46
|
readonly trigger: (...args: Args) => void;
|
|
22
47
|
readonly forceTrigger: (...args: Args) => void;
|
|
23
48
|
readonly clear: () => void;
|
|
24
49
|
};
|
|
50
|
+
export declare function useSharedFunction<T, Args extends unknown[], S extends string = string>(key: S, fn: AFunction<T, Args>, scopeName?: Prefix): SharedFunctionStateReturn<T, Args>;
|
|
51
|
+
export declare function useSharedFunction<T, Args extends unknown[]>(sharedFunctionCreated: SharedFunctionCreated<T, Args>): SharedFunctionStateReturn<T, Args>;
|
|
25
52
|
export {};
|
|
@@ -1,11 +1,31 @@
|
|
|
1
|
-
import { Prefix } from '../types';
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Prefix, SharedCreated, SharedValue } from '../types';
|
|
2
|
+
import { SharedValuesApi, SharedValuesManager } from '../SharedValuesManager';
|
|
3
|
+
interface SharedState<T> extends SharedValue {
|
|
4
|
+
value: T;
|
|
5
|
+
}
|
|
6
|
+
declare class SharedStatesManager extends SharedValuesManager<SharedState<unknown>, {
|
|
4
7
|
value: unknown;
|
|
5
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);
|
|
6
19
|
get<T, S extends string = string>(key: S, scopeName?: Prefix): T;
|
|
20
|
+
get<T>(sharedStateCreated: SharedStateCreated<T>): T;
|
|
7
21
|
set<T, S extends string = string>(key: S, value: T, scopeName?: Prefix): void;
|
|
22
|
+
set<T>(sharedStateCreated: SharedStateCreated<T>, value: T): void;
|
|
8
23
|
}
|
|
9
24
|
export declare const sharedStatesApi: SharedStatesApi;
|
|
10
|
-
export
|
|
25
|
+
export interface SharedStateCreated<T> extends SharedCreated {
|
|
26
|
+
initialValue: T;
|
|
27
|
+
}
|
|
28
|
+
export declare const createSharedState: <T>(initialValue: T, scopeName?: Prefix) => SharedStateCreated<T>;
|
|
29
|
+
export declare function useSharedState<T, S extends string>(key: S, initialValue: T, scopeName?: Prefix): readonly [T, (v: T | ((prev: T) => T)) => void];
|
|
30
|
+
export declare function useSharedState<T>(sharedStateCreated: SharedStateCreated<T>): readonly [T, (v: T | ((prev: T) => T)) => void];
|
|
11
31
|
export {};
|
|
@@ -1,35 +1,64 @@
|
|
|
1
|
-
import { PotentialPromise, Prefix } from '../types';
|
|
2
|
-
import {
|
|
3
|
-
type Unsubscribe = () => void;
|
|
1
|
+
import { PotentialPromise, Prefix, SharedCreated, SharedValue } from '../types';
|
|
2
|
+
import { SharedValuesApi, SharedValuesManager } from '../SharedValuesManager';
|
|
3
|
+
export type Unsubscribe = () => void;
|
|
4
4
|
export declare namespace SubscriberEvents {
|
|
5
5
|
type OnError = (error: unknown) => void;
|
|
6
6
|
type OnCompletion = () => void;
|
|
7
7
|
type Set<T> = (value: T) => void;
|
|
8
8
|
}
|
|
9
|
-
type Subscriber<T> = (set: SubscriberEvents.Set<T>, onError: SubscriberEvents.OnError, onCompletion: SubscriberEvents.OnCompletion) => PotentialPromise<Unsubscribe | void | undefined>;
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
subscribed: boolean;
|
|
16
|
-
};
|
|
17
|
-
unsubscribe?: Unsubscribe | void;
|
|
9
|
+
export type Subscriber<T> = (set: SubscriberEvents.Set<T>, onError: SubscriberEvents.OnError, onCompletion: SubscriberEvents.OnCompletion) => PotentialPromise<Unsubscribe | void | undefined>;
|
|
10
|
+
type SharedSubscriptionValue<T> = {
|
|
11
|
+
data?: T;
|
|
12
|
+
isLoading: boolean;
|
|
13
|
+
error?: unknown;
|
|
14
|
+
subscribed?: boolean;
|
|
18
15
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
interface SharedSubscription<T> extends SharedValue {
|
|
17
|
+
fnState: SharedSubscriptionValue<T>;
|
|
18
|
+
unsubscribe?: Unsubscribe | void;
|
|
22
19
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
declare class SharedSubscriptionsManager extends SharedValuesManager<SharedSubscription<unknown>, {
|
|
21
|
+
fnState: SharedSubscriptionValue<unknown>;
|
|
22
|
+
}> {
|
|
23
|
+
defaultValue(): {
|
|
24
|
+
fnState: {
|
|
25
|
+
data: undefined;
|
|
26
|
+
isLoading: boolean;
|
|
27
|
+
error: undefined;
|
|
28
|
+
subscribed: boolean;
|
|
29
|
+
};
|
|
30
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
|
+
}
|
|
51
|
+
export declare const sharedSubscriptionsApi: SharedSubscriptionsApi;
|
|
52
|
+
interface SharedSubscriptionCreated<T> extends SharedCreated {
|
|
53
|
+
subscriber: Subscriber<T>;
|
|
54
|
+
}
|
|
55
|
+
export declare const createSharedSubscription: <T, Args extends unknown[]>(subscriber: Subscriber<T>, scopeName?: Prefix) => SharedSubscriptionCreated<T>;
|
|
56
|
+
export type SharedSubscriptionStateReturn<T> = {
|
|
57
|
+
readonly state: NonNullable<SharedSubscriptionValue<T>>;
|
|
31
58
|
readonly trigger: () => void;
|
|
32
59
|
readonly forceTrigger: () => void;
|
|
33
60
|
readonly unsubscribe: () => void;
|
|
34
61
|
};
|
|
62
|
+
export declare function useSharedSubscription<T, S extends string = string>(key: S, subscriber: Subscriber<T>, scopeName?: Prefix): SharedSubscriptionStateReturn<T>;
|
|
63
|
+
export declare function useSharedSubscription<T>(sharedSubscriptionCreated: SharedSubscriptionCreated<T>): SharedSubscriptionStateReturn<T>;
|
|
35
64
|
export {};
|
package/dist/lib/utils.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { NonEmptyString } from '../types';
|
|
2
1
|
export declare const log: (...args: any[]) => void;
|
|
3
|
-
export declare const ensureNonEmptyString: <
|
|
2
|
+
export declare const ensureNonEmptyString: <X extends string>(value: X) => X;
|
|
3
|
+
export declare const random: () => string;
|