beestate 0.1.12 → 0.1.14
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/dist/@types/InternalStore.d.ts +3 -3
- package/dist/@types/LOCKED.d.ts +2 -0
- package/dist/@types/OVERRIDE_LOCKED.d.ts +2 -0
- package/dist/@types/constants.d.ts +4 -2
- package/dist/@types/createStore.d.ts +4 -2
- package/dist/@types/index.d.ts +5 -0
- package/dist/@types/isLocked.d.ts +2 -0
- package/dist/@types/isLockedOverride.d.ts +2 -0
- package/dist/@types/mergePrefill.d.ts +4 -0
- package/dist/@types/pickPropAll.d.ts +3 -1
- package/dist/@types/types/InitInstance.d.ts +2 -1
- package/dist/@types/types/InternalStore/ApplyState.d.ts +2 -1
- package/dist/@types/types/InternalStore/GetState.d.ts +2 -1
- package/dist/@types/types/InternalStore/GetStateAll.d.ts +2 -1
- package/dist/@types/types/InternalStore/InitInstance.d.ts +3 -1
- package/dist/@types/types/InternalStore/PatchState.d.ts +8 -0
- package/dist/@types/types/InternalStoreProps.d.ts +3 -0
- package/dist/@types/types/Locked.d.ts +5 -0
- package/dist/@types/types/LockedOverride.d.ts +5 -0
- package/dist/@types/types/Patch.d.ts +6 -0
- package/dist/@types/types/Prefill.d.ts +5 -0
- package/dist/@types/types/Reducer.d.ts +3 -1
- package/dist/@types/types/ReducerLink.d.ts +2 -2
- package/dist/@types/types/ReducerMap.d.ts +2 -1
- package/dist/@types/types/{InitializedStateLink.d.ts → SpecialInitializedStateLink.d.ts} +3 -3
- package/dist/@types/types/{InstanceKeyStateLink.d.ts → SpecialInstanceKeyStateLink.d.ts} +3 -3
- package/dist/@types/types/StateLink.d.ts +2 -2
- package/dist/@types/types/Store.d.ts +5 -5
- package/dist/@types/types/StoreContextValue.d.ts +2 -1
- package/dist/@types/types/StoreInstance.d.ts +3 -1
- package/dist/@types/types/StoreProviderProps.d.ts +2 -1
- package/dist/@types/types/Unlocked.d.ts +2 -0
- package/dist/@types/types/UnlockedState.d.ts +5 -0
- package/dist/@types/types/UseProp.d.ts +6 -5
- package/dist/@types/types/UsePropAll.d.ts +3 -1
- package/dist/@types/types/UsePropSelector.d.ts +6 -5
- package/dist/@types/types/UsePropSelectorAll.d.ts +3 -1
- package/dist/@types/unlock.d.ts +1 -0
- package/dist/cjs/index.cjs.js +138 -42
- package/dist/esm/index.esm.js +137 -43
- package/package.json +1 -1
- package/dist/@types/types/InitializedStoreInstance.d.ts +0 -5
- package/dist/@types/types/InternalStore/GetInitializedInstance.d.ts +0 -6
- package/dist/@types/types/InternalStore/GetUninitializedInstance.d.ts +0 -6
- package/dist/@types/types/UninitializedStoreInstance.d.ts +0 -5
|
@@ -10,19 +10,19 @@ import type { SubscribeAll } from './types/InternalStore/SubscribeAll.tsx';
|
|
|
10
10
|
export declare class InternalStore<TStoreState extends StoreState = StoreState> {
|
|
11
11
|
private readonly instances;
|
|
12
12
|
private readonly listenersAll;
|
|
13
|
+
private readonly lockedStateNames;
|
|
13
14
|
readonly name: string;
|
|
14
15
|
private stateAll;
|
|
15
|
-
constructor(name: string);
|
|
16
|
+
constructor(name: string, lockedStateNames?: string[]);
|
|
16
17
|
private applyState;
|
|
17
18
|
dispatch: Dispatch<TStoreState>;
|
|
18
19
|
private ensureInstance;
|
|
19
20
|
private getInstance;
|
|
20
|
-
private getInitializedInstance;
|
|
21
21
|
getState: GetState<TStoreState>;
|
|
22
22
|
getStateAll: GetStateAll<TStoreState>;
|
|
23
|
-
private getUninitializedInstance;
|
|
24
23
|
initInstance: InitInstance<TStoreState>;
|
|
25
24
|
isInitialized: IsInitialized;
|
|
25
|
+
private patchState;
|
|
26
26
|
private pruneInstance;
|
|
27
27
|
resetInstance: ResetInstance<TStoreState>;
|
|
28
28
|
subscribe: Subscribe<TStoreState>;
|
|
@@ -4,6 +4,8 @@ export declare const INITIALIZED_STATE_NAME: "$$initialized";
|
|
|
4
4
|
export declare const INSTANCE_KEY_STATE_NAME: "$$instanceKey";
|
|
5
5
|
export declare const INTERNAL_BUG_MESSAGE = "This must never happen and may mean a bug inside the library itself. Please report it to the library developers.";
|
|
6
6
|
export declare const INTERNAL_STORE_PROPS_ACCESSOR: unique symbol;
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
7
|
+
export declare const LOCKED_FLAG: unique symbol;
|
|
8
|
+
export declare const LOCKED_OVERRIDE_FLAG: unique symbol;
|
|
9
|
+
export declare const REDUCER_LINK_FLAG: unique symbol;
|
|
10
|
+
export declare const STATE_LINK_FLAG: unique symbol;
|
|
9
11
|
export declare const STORE_INSTANCE_UNREGISTERED: unique symbol;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import type { LockedOverride } from './types/LockedOverride.tsx';
|
|
1
2
|
import type { Payload } from './types/Payload.tsx';
|
|
2
3
|
import type { ReducerMap } from './types/ReducerMap.tsx';
|
|
3
4
|
import type { Store } from './types/Store.tsx';
|
|
4
5
|
import type { StoreState } from './types/StoreState.tsx';
|
|
6
|
+
import type { Unlocked } from './types/Unlocked.tsx';
|
|
5
7
|
type CheckInitialState<TStoreState extends StoreState = StoreState> = {
|
|
6
|
-
[TKey in keyof TStoreState]: TKey extends `$$initialized` | `$$instanceKey` ? void : TStoreState[TKey];
|
|
8
|
+
[TKey in keyof TStoreState]: TKey extends `$$initialized` | `$$instanceKey` ? void : unknown extends Unlocked<TStoreState[TKey]> ? TStoreState[TKey] : undefined extends Unlocked<TStoreState[TKey]> ? never : TStoreState[TKey] extends LockedOverride ? never : TStoreState[TKey];
|
|
7
9
|
};
|
|
8
10
|
type CheckReducerMap<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
9
|
-
[TKey in keyof TReducerMap]: TKey extends `$$initialized` | `$$instanceKey` ? void : TKey extends keyof TStoreState ? void : TReducerMap[TKey] extends (
|
|
11
|
+
[TKey in keyof TReducerMap]: TKey extends `$$initialized` | `$$instanceKey` ? void : TKey extends keyof TStoreState ? void : TReducerMap[TKey] extends (...args: any) => any ? [Parameters<TReducerMap[TKey]>[1]] extends [void] ? TReducerMap[TKey] : [Parameters<TReducerMap[TKey]>[1]] extends [Payload] ? Parameters<TReducerMap[TKey]>[1] extends readonly unknown[] ? void : TReducerMap[TKey] : void : void;
|
|
10
12
|
};
|
|
11
13
|
export declare function createStore<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(name: TStoreName, initialState: TStoreState & CheckInitialState<TStoreState>, reducerMap: TReducerMap & CheckReducerMap<TStoreState, TReducerMap>): Store<TStoreName, TStoreState, TReducerMap>;
|
|
12
14
|
export {};
|
package/dist/@types/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { type InstanceKey } from './types/InstanceKey.tsx';
|
|
2
|
+
export { type Locked } from './types/Locked.tsx';
|
|
3
|
+
export { type Patch } from './types/Patch.tsx';
|
|
4
|
+
export { type Prefill } from './types/Prefill.tsx';
|
|
2
5
|
export { type RequestState } from './types/RequestState.tsx';
|
|
3
6
|
export { type RequestAbortedState } from './types/RequestState/RequestAbortedState.tsx';
|
|
4
7
|
export { type RequestFulfilledState } from './types/RequestState/RequestFulfilledState.tsx';
|
|
@@ -9,6 +12,8 @@ export { DESTROY } from './constants.tsx';
|
|
|
9
12
|
export { createStore } from './createStore.tsx';
|
|
10
13
|
export { createStoreRegistry } from './createStoreRegistry.tsx';
|
|
11
14
|
export { InternalStore } from './InternalStore.tsx';
|
|
15
|
+
export { LOCKED } from './LOCKED.tsx';
|
|
16
|
+
export { OVERRIDE_LOCKED } from './OVERRIDE_LOCKED.tsx';
|
|
12
17
|
export { requestState } from './requestState.tsx';
|
|
13
18
|
export { requestStatus } from './requestStatus.tsx';
|
|
14
19
|
export declare const StoreProvider: <TStoreName extends string = string, TStoreState extends import("./types/StoreState.tsx").StoreState = import("./types/StoreState.tsx").StoreState, TReducerMap extends import("./types/ReducerMap.tsx").ReducerMap<TStoreState> = import("./types/ReducerMap.tsx").ReducerMap<TStoreState>>(props: import("./types/StoreProviderProps.tsx").StoreProviderProps<TStoreName, TStoreState, TReducerMap>) => import("react/jsx-runtime").JSX.Element, useDispatcher: <TStoreState extends import("./types/StoreState.tsx").StoreState = import("./types/StoreState.tsx").StoreState, TReducerMap extends import("./types/ReducerMap.tsx").ReducerMap<TStoreState> = import("./types/ReducerMap.tsx").ReducerMap<TStoreState>, TReducerName extends string = string, TPayload extends import("./types/ReducerPayload.tsx").ReducerPayload<TReducerMap[TReducerName]> = import("./types/ReducerPayload.tsx").ReducerPayload<TReducerMap[TReducerName]>>(reducerLink: import("./types/ReducerLink.tsx").ReducerLink<string, TStoreState, TReducerMap, TReducerName>) => import("./types/Dispatcher.tsx").Dispatcher<TPayload>, useProp: import("./types/UseProp.tsx").UseProp, usePropAll: import("./types/UsePropAll.tsx").UsePropAll, usePropSelector: import("./types/UsePropSelector.tsx").UsePropSelector, usePropSelectorAll: import("./types/UsePropSelectorAll.tsx").UsePropSelectorAll, useStore: {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Prefill } from './types/Prefill.tsx';
|
|
2
|
+
import type { StoreState } from './types/StoreState.tsx';
|
|
3
|
+
import type { UnlockedState } from './types/UnlockedState.tsx';
|
|
4
|
+
export declare function mergePrefill<TStoreState extends StoreState = StoreState>(unlockedInitialState: UnlockedState<TStoreState>, prefill?: Prefill<TStoreState>): UnlockedState<TStoreState>;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { InstanceKey } from './types/InstanceKey.tsx';
|
|
2
2
|
import type { StoreState } from './types/StoreState.tsx';
|
|
3
|
-
|
|
3
|
+
import type { Unlocked } from './types/Unlocked.tsx';
|
|
4
|
+
import type { UnlockedState } from './types/UnlockedState.tsx';
|
|
5
|
+
export declare function pickPropAll<TStoreState extends StoreState = StoreState, TStateName extends keyof TStoreState = keyof TStoreState>(stateAll: Record<InstanceKey, UnlockedState<TStoreState>>, stateName: TStateName, instanceKeys?: InstanceKey[] | ((state: UnlockedState<TStoreState>) => boolean)): [instanceKey: InstanceKey, state: Unlocked<TStoreState[TStateName]>][];
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { Prefill } from './Prefill.tsx';
|
|
1
2
|
import type { ResetInstance } from './ResetInstance.tsx';
|
|
2
3
|
import type { StoreState } from './StoreState.tsx';
|
|
3
4
|
export type InitInstance<TStoreState extends StoreState = StoreState> = {
|
|
4
|
-
(prefill?:
|
|
5
|
+
(prefill?: Prefill<TStoreState>): ResetInstance;
|
|
5
6
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
2
2
|
import type { StoreState } from '../StoreState.tsx';
|
|
3
|
+
import type { UnlockedState } from '../UnlockedState.tsx';
|
|
3
4
|
export type ApplyState<TStoreState extends StoreState = StoreState> = {
|
|
4
|
-
(instanceKey: InstanceKey, state: undefined | TStoreState): void;
|
|
5
|
+
(instanceKey: InstanceKey, state: undefined | UnlockedState<TStoreState>): void;
|
|
5
6
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
2
2
|
import type { StoreState } from '../StoreState.tsx';
|
|
3
|
+
import type { UnlockedState } from '../UnlockedState.tsx';
|
|
3
4
|
export type GetState<TStoreState extends StoreState = StoreState> = {
|
|
4
|
-
(instanceKey: InstanceKey): TStoreState
|
|
5
|
+
(instanceKey: InstanceKey): UnlockedState<TStoreState>;
|
|
5
6
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
2
2
|
import type { StoreState } from '../StoreState.tsx';
|
|
3
|
+
import type { UnlockedState } from '../UnlockedState.tsx';
|
|
3
4
|
export type GetStateAll<TStoreState extends StoreState = StoreState> = {
|
|
4
|
-
(): Record<InstanceKey, TStoreState
|
|
5
|
+
(): Record<InstanceKey, UnlockedState<TStoreState>>;
|
|
5
6
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
2
|
+
import type { Prefill } from '../Prefill.tsx';
|
|
2
3
|
import type { Reducer } from '../Reducer.tsx';
|
|
3
4
|
import type { StoreState } from '../StoreState.tsx';
|
|
5
|
+
import type { UnlockedState } from '../UnlockedState.tsx';
|
|
4
6
|
export type InitInstance<TStoreState extends StoreState = StoreState> = {
|
|
5
|
-
(instanceKey: InstanceKey, initReducer: Reducer<TStoreState,
|
|
7
|
+
(instanceKey: InstanceKey, initReducer: Reducer<TStoreState, Prefill<TStoreState>>, unlockedInitialState: UnlockedState<TStoreState>, prefill?: Prefill<TStoreState>): void;
|
|
6
8
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
2
|
+
import type { Payload } from '../Payload.tsx';
|
|
3
|
+
import type { Reducer } from '../Reducer.tsx';
|
|
4
|
+
import type { StoreState } from '../StoreState.tsx';
|
|
5
|
+
import type { UnlockedState } from '../UnlockedState.tsx';
|
|
6
|
+
export type PatchState<TStoreState extends StoreState = StoreState> = {
|
|
7
|
+
<TPayload extends void | Payload = void | Payload>(instanceKey: InstanceKey, reducer: Reducer<TStoreState, TPayload>, payload: TPayload, prevState: UnlockedState<TStoreState>): UnlockedState<TStoreState>;
|
|
8
|
+
};
|
|
@@ -2,11 +2,14 @@ import React from 'react';
|
|
|
2
2
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
3
3
|
import type { StoreContextValue } from './StoreContextValue.tsx';
|
|
4
4
|
import type { StoreState } from './StoreState.tsx';
|
|
5
|
+
import type { UnlockedState } from './UnlockedState.tsx';
|
|
5
6
|
export type InternalStoreProps<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
6
7
|
$$init: TReducerMap[`$$init`];
|
|
7
8
|
$$reset: TReducerMap[`$$reset`];
|
|
8
9
|
context: React.Context<null | StoreContextValue<TStoreState>>;
|
|
9
10
|
initialState: TStoreState;
|
|
11
|
+
lockedStateNames: string[];
|
|
10
12
|
name: TStoreName;
|
|
11
13
|
uniqKey: symbol;
|
|
14
|
+
unlockedInitialState: UnlockedState<TStoreState>;
|
|
12
15
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Locked } from './Locked.tsx';
|
|
2
|
+
import type { LockedOverride } from './LockedOverride.tsx';
|
|
3
|
+
import type { StoreState } from './StoreState.tsx';
|
|
4
|
+
export type Patch<TStoreState extends StoreState = StoreState> = null | {
|
|
5
|
+
[TKey in keyof TStoreState]?: undefined | (TStoreState[TKey] extends Locked<infer TValue> ? LockedOverride<TValue> : TStoreState[TKey]);
|
|
6
|
+
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { Patch } from './Patch.tsx';
|
|
1
2
|
import type { Payload } from './Payload.tsx';
|
|
2
3
|
import type { StoreState } from './StoreState.tsx';
|
|
4
|
+
import type { UnlockedState } from './UnlockedState.tsx';
|
|
3
5
|
export type Reducer<TStoreState extends StoreState = StoreState, TPayload extends void | Payload = void | Payload> = {
|
|
4
|
-
(state: TStoreState
|
|
6
|
+
(state: UnlockedState<TStoreState>, payload: TPayload): Patch<TStoreState>;
|
|
5
7
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
2
|
-
import {
|
|
2
|
+
import { REDUCER_LINK_FLAG } from '../constants.tsx';
|
|
3
3
|
import type { InternalStoreProps } from './InternalStoreProps.tsx';
|
|
4
4
|
import type { Reducer } from './Reducer.tsx';
|
|
5
5
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
@@ -9,5 +9,5 @@ export type ReducerLink<TStoreName extends string = string, TStoreState extends
|
|
|
9
9
|
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
|
|
10
10
|
reducer: Reducer<TStoreState, ReducerPayload<TReducerMap[TReducerName]>>;
|
|
11
11
|
reducerName: TReducerName;
|
|
12
|
-
type: typeof
|
|
12
|
+
type: typeof REDUCER_LINK_FLAG;
|
|
13
13
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { Prefill } from './Prefill.tsx';
|
|
1
2
|
import type { Reducer } from './Reducer.tsx';
|
|
2
3
|
import type { StoreState } from './StoreState.tsx';
|
|
3
4
|
export type ReducerMap<TStoreState extends StoreState = StoreState> = Record<string, Reducer<TStoreState, void | any>> & {
|
|
4
|
-
$$init: Reducer<TStoreState,
|
|
5
|
+
$$init: Reducer<TStoreState, Prefill<TStoreState>>;
|
|
5
6
|
$$reset: Reducer<TStoreState, void>;
|
|
6
7
|
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { INITIALIZED_STATE_NAME } from '../constants.tsx';
|
|
2
2
|
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
3
|
-
import {
|
|
3
|
+
import { STATE_LINK_FLAG } from '../constants.tsx';
|
|
4
4
|
import type { InternalStoreProps } from './InternalStoreProps.tsx';
|
|
5
5
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
6
6
|
import type { StoreState } from './StoreState.tsx';
|
|
7
|
-
export type
|
|
7
|
+
export type SpecialInitializedStateLink<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
8
8
|
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
|
|
9
9
|
stateName: typeof INITIALIZED_STATE_NAME;
|
|
10
|
-
type: typeof
|
|
10
|
+
type: typeof STATE_LINK_FLAG;
|
|
11
11
|
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { INSTANCE_KEY_STATE_NAME } from '../constants.tsx';
|
|
2
2
|
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
3
|
-
import {
|
|
3
|
+
import { STATE_LINK_FLAG } from '../constants.tsx';
|
|
4
4
|
import type { InternalStoreProps } from './InternalStoreProps.tsx';
|
|
5
5
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
6
6
|
import type { StoreState } from './StoreState.tsx';
|
|
7
|
-
export type
|
|
7
|
+
export type SpecialInstanceKeyStateLink<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
8
8
|
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
|
|
9
9
|
stateName: typeof INSTANCE_KEY_STATE_NAME;
|
|
10
|
-
type: typeof
|
|
10
|
+
type: typeof STATE_LINK_FLAG;
|
|
11
11
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
2
|
-
import {
|
|
2
|
+
import { STATE_LINK_FLAG } from '../constants.tsx';
|
|
3
3
|
import type { InternalStoreProps } from './InternalStoreProps.tsx';
|
|
4
4
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
5
5
|
import type { StoreState } from './StoreState.tsx';
|
|
6
6
|
export type StateLink<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState> = {
|
|
7
7
|
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
|
|
8
8
|
stateName: TStateName;
|
|
9
|
-
type: typeof
|
|
9
|
+
type: typeof STATE_LINK_FLAG;
|
|
10
10
|
};
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
2
|
-
import type { InitializedStateLink } from './InitializedStateLink.tsx';
|
|
3
|
-
import type { InstanceKeyStateLink } from './InstanceKeyStateLink.tsx';
|
|
4
2
|
import type { InternalStoreProps } from './InternalStoreProps.tsx';
|
|
5
|
-
import type { ReducerMap } from './ReducerMap.tsx';
|
|
6
3
|
import type { ReducerLink } from './ReducerLink.tsx';
|
|
4
|
+
import type { ReducerMap } from './ReducerMap.tsx';
|
|
5
|
+
import type { SpecialInitializedStateLink } from './SpecialInitializedStateLink.tsx';
|
|
6
|
+
import type { SpecialInstanceKeyStateLink } from './SpecialInstanceKeyStateLink.tsx';
|
|
7
7
|
import type { StateLink } from './StateLink.tsx';
|
|
8
8
|
import type { StoreState } from './StoreState.tsx';
|
|
9
9
|
export type Store<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
10
10
|
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
|
|
11
11
|
$$init: void;
|
|
12
|
-
$$initialized:
|
|
13
|
-
$$instanceKey:
|
|
12
|
+
$$initialized: SpecialInitializedStateLink<TStoreName, TStoreState, TReducerMap>;
|
|
13
|
+
$$instanceKey: SpecialInstanceKeyStateLink<TStoreName, TStoreState, TReducerMap>;
|
|
14
14
|
$$reset: void;
|
|
15
15
|
} & {
|
|
16
16
|
[TKey in keyof TStoreState]: TKey extends string ? StateLink<TStoreName, TStoreState, TReducerMap, TKey> : never;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { InstanceKey } from './InstanceKey.tsx';
|
|
2
2
|
import type { StoreState } from './StoreState.tsx';
|
|
3
|
+
import type { UnlockedState } from './UnlockedState.tsx';
|
|
3
4
|
export type StoreContextValue<TStoreState extends StoreState = StoreState> = {
|
|
4
5
|
instanceKey: InstanceKey;
|
|
5
|
-
uninitializedState: TStoreState
|
|
6
|
+
uninitializedState: UnlockedState<TStoreState>;
|
|
6
7
|
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { DeferredAction } from './DeferredAction.tsx';
|
|
2
2
|
import type { Listener } from './InternalStore/Listener.tsx';
|
|
3
|
+
import type { Reducer } from './Reducer.tsx';
|
|
3
4
|
import type { StoreState } from './StoreState.tsx';
|
|
4
5
|
export type StoreInstance<TStoreState extends StoreState = StoreState> = {
|
|
5
6
|
deferredActions: DeferredAction<TStoreState>[];
|
|
6
7
|
listeners: Set<Listener<TStoreState>>;
|
|
7
|
-
|
|
8
|
+
updatedLockedWarnedReducers: Set<Reducer<TStoreState, any>>;
|
|
9
|
+
wrappedUnlockedWarnedReducers: Set<Reducer<TStoreState, any>>;
|
|
8
10
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { InstanceKey } from './InstanceKey.tsx';
|
|
3
|
+
import type { Prefill } from './Prefill.tsx';
|
|
3
4
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
4
5
|
import type { Store } from './Store.tsx';
|
|
5
6
|
import type { StoreState } from './StoreState.tsx';
|
|
6
7
|
export type StoreProviderProps<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = React.PropsWithChildren<{
|
|
7
8
|
instanceKey?: InstanceKey;
|
|
8
|
-
prefill?:
|
|
9
|
+
prefill?: Prefill<TStoreState>;
|
|
9
10
|
store: Store<TStoreName, TStoreState, TReducerMap>;
|
|
10
11
|
}>;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type { InitializedStateLink } from './InitializedStateLink.tsx';
|
|
2
1
|
import type { InstanceKey } from './InstanceKey.tsx';
|
|
3
|
-
import type { InstanceKeyStateLink } from './InstanceKeyStateLink.tsx';
|
|
4
2
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
3
|
+
import type { SpecialInitializedStateLink } from './SpecialInitializedStateLink.tsx';
|
|
4
|
+
import type { SpecialInstanceKeyStateLink } from './SpecialInstanceKeyStateLink.tsx';
|
|
5
5
|
import type { StateLink } from './StateLink.tsx';
|
|
6
6
|
import type { StoreState } from './StoreState.tsx';
|
|
7
|
+
import type { Unlocked } from './Unlocked.tsx';
|
|
7
8
|
export type UseProp = {
|
|
8
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink:
|
|
9
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink:
|
|
10
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKey?: InstanceKey): TStoreState[TStateName]
|
|
9
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink: SpecialInitializedStateLink<string, TStoreState, TReducerMap>, instanceKey?: InstanceKey): boolean;
|
|
10
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink: SpecialInstanceKeyStateLink<string, TStoreState, TReducerMap>, instanceKey?: InstanceKey): InstanceKey;
|
|
11
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKey?: InstanceKey): Unlocked<TStoreState[TStateName]>;
|
|
11
12
|
};
|
|
@@ -2,6 +2,8 @@ import type { InstanceKey } from './InstanceKey.tsx';
|
|
|
2
2
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
3
3
|
import type { StateLink } from './StateLink.tsx';
|
|
4
4
|
import type { StoreState } from './StoreState.tsx';
|
|
5
|
+
import type { Unlocked } from './Unlocked.tsx';
|
|
6
|
+
import type { UnlockedState } from './UnlockedState.tsx';
|
|
5
7
|
export type UsePropAll = {
|
|
6
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKeys?: InstanceKey[] | ((state: TStoreState) => boolean)): [instanceKey: InstanceKey, state: TStoreState[TStateName]][];
|
|
8
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKeys?: InstanceKey[] | ((state: UnlockedState<TStoreState>) => boolean)): [instanceKey: InstanceKey, state: Unlocked<TStoreState[TStateName]>][];
|
|
7
9
|
};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type { InitializedStateLink } from './InitializedStateLink.tsx';
|
|
2
1
|
import type { InstanceKey } from './InstanceKey.tsx';
|
|
3
|
-
import type { InstanceKeyStateLink } from './InstanceKeyStateLink.tsx';
|
|
4
2
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
3
|
+
import type { SpecialInitializedStateLink } from './SpecialInitializedStateLink.tsx';
|
|
4
|
+
import type { SpecialInstanceKeyStateLink } from './SpecialInstanceKeyStateLink.tsx';
|
|
5
5
|
import type { StateLink } from './StateLink.tsx';
|
|
6
6
|
import type { StoreState } from './StoreState.tsx';
|
|
7
|
+
import type { Unlocked } from './Unlocked.tsx';
|
|
7
8
|
export type UsePropSelector = {
|
|
8
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink:
|
|
9
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink:
|
|
10
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKey?: InstanceKey): () => TStoreState[TStateName]
|
|
9
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink: SpecialInitializedStateLink<string, TStoreState, TReducerMap>, instanceKey?: InstanceKey): () => boolean;
|
|
10
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(stateLink: SpecialInstanceKeyStateLink<string, TStoreState, TReducerMap>, instanceKey?: InstanceKey): () => InstanceKey;
|
|
11
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKey?: InstanceKey): () => Unlocked<TStoreState[TStateName]>;
|
|
11
12
|
};
|
|
@@ -2,6 +2,8 @@ import type { InstanceKey } from './InstanceKey.tsx';
|
|
|
2
2
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
3
3
|
import type { StateLink } from './StateLink.tsx';
|
|
4
4
|
import type { StoreState } from './StoreState.tsx';
|
|
5
|
+
import type { Unlocked } from './Unlocked.tsx';
|
|
6
|
+
import type { UnlockedState } from './UnlockedState.tsx';
|
|
5
7
|
export type UsePropSelectorAll = {
|
|
6
|
-
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKeys?: InstanceKey[] | ((state: TStoreState) => boolean)): () => [instanceKey: InstanceKey, state: TStoreState[TStateName]][];
|
|
8
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap, TStateName>, instanceKeys?: InstanceKey[] | ((state: UnlockedState<TStoreState>) => boolean)): () => [instanceKey: InstanceKey, state: Unlocked<TStoreState[TStateName]>][];
|
|
7
9
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function unlock(value: unknown): unknown;
|
package/dist/cjs/index.cjs.js
CHANGED
|
@@ -10,9 +10,39 @@ const INITIALIZED_STATE_NAME = `$$initialized`;
|
|
|
10
10
|
const INSTANCE_KEY_STATE_NAME = `$$instanceKey`;
|
|
11
11
|
const INTERNAL_BUG_MESSAGE = `This must never happen and may mean a bug inside the library itself. Please report it to the library developers.`;
|
|
12
12
|
const INTERNAL_STORE_PROPS_ACCESSOR = Symbol(`INTERNAL_STORE_PROPS`);
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
13
|
+
const LOCKED_FLAG = Symbol(`LOCKED`);
|
|
14
|
+
const LOCKED_OVERRIDE_FLAG = Symbol(`LOCKED_OVERRIDE`);
|
|
15
|
+
const REDUCER_LINK_FLAG = Symbol(`REDUCER_LINK`);
|
|
16
|
+
const STATE_LINK_FLAG = Symbol(`STATE_LINK`);
|
|
17
|
+
const STORE_INSTANCE_UNREGISTERED = Symbol(`STORE_INSTANCE_UNREGISTERED`);
|
|
18
|
+
|
|
19
|
+
function isLocked(value) {
|
|
20
|
+
return typeof value === `object` && value !== null && `kind` in value && value.kind === LOCKED_FLAG;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isLockedOverride(value) {
|
|
24
|
+
return typeof value === `object` && value !== null && `kind` in value && value.kind === LOCKED_OVERRIDE_FLAG;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function unlock(value) {
|
|
28
|
+
if (isLocked(value) || isLockedOverride(value)) {
|
|
29
|
+
return value.value;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function mergePrefill(unlockedInitialState, prefill = {}) {
|
|
37
|
+
const state = { ...unlockedInitialState };
|
|
38
|
+
for (const stateName of Object.keys(prefill)) {
|
|
39
|
+
const value = unlock(prefill[stateName]);
|
|
40
|
+
if (value !== undefined) {
|
|
41
|
+
state[stateName] = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return state;
|
|
45
|
+
}
|
|
16
46
|
|
|
17
47
|
function createStoreProvider(useStore) {
|
|
18
48
|
return function StoreProvider(props) {
|
|
@@ -24,17 +54,15 @@ function createStoreProvider(useStore) {
|
|
|
24
54
|
return initInstance(prefillRef.current.prefill);
|
|
25
55
|
});
|
|
26
56
|
const contextValue = React.useMemo(() => {
|
|
27
|
-
return { instanceKey: actualInstanceKey, uninitializedState:
|
|
57
|
+
return { instanceKey: actualInstanceKey, uninitializedState: mergePrefill(internalStoreProps.unlockedInitialState, prefillRef.current.prefill) };
|
|
28
58
|
}, [actualInstanceKey, internalStoreProps]);
|
|
29
59
|
return (jsxRuntime.jsx(Context.Provider, { value: contextValue, children: props.children }));
|
|
30
60
|
};
|
|
31
61
|
}
|
|
32
62
|
|
|
33
63
|
class InternalStore {
|
|
34
|
-
constructor(name) {
|
|
64
|
+
constructor(name, lockedStateNames = []) {
|
|
35
65
|
this.applyState = (instanceKey, state) => {
|
|
36
|
-
const instance = this.getInstance(instanceKey);
|
|
37
|
-
instance.state = state;
|
|
38
66
|
const stateAll = Object.assign(Object.create(null), this.stateAll);
|
|
39
67
|
if (state) {
|
|
40
68
|
stateAll[instanceKey] = state;
|
|
@@ -47,9 +75,9 @@ class InternalStore {
|
|
|
47
75
|
this.dispatch = (instanceKey, reducer, payload) => {
|
|
48
76
|
this.ensureInstance(instanceKey);
|
|
49
77
|
if (this.isInitialized(instanceKey)) {
|
|
50
|
-
const instance = this.
|
|
51
|
-
const prevState =
|
|
52
|
-
const nextState =
|
|
78
|
+
const instance = this.getInstance(instanceKey);
|
|
79
|
+
const prevState = this.getState(instanceKey);
|
|
80
|
+
const nextState = this.patchState(instanceKey, reducer, payload, prevState);
|
|
53
81
|
this.applyState(instanceKey, nextState);
|
|
54
82
|
instance.listeners.forEach((listener) => listener(instanceKey, reducer, payload));
|
|
55
83
|
this.listenersAll.forEach((listener) => listener(instanceKey, reducer, payload));
|
|
@@ -59,13 +87,13 @@ class InternalStore {
|
|
|
59
87
|
console.log({ payload, [`prev state`]: prevState, [`next state`]: nextState });
|
|
60
88
|
}
|
|
61
89
|
else {
|
|
62
|
-
const instance = this.
|
|
90
|
+
const instance = this.getInstance(instanceKey);
|
|
63
91
|
instance.deferredActions.push({ payload, reducer });
|
|
64
92
|
}
|
|
65
93
|
};
|
|
66
94
|
this.ensureInstance = (instanceKey) => {
|
|
67
95
|
if (!this.instances[instanceKey]) {
|
|
68
|
-
this.instances[instanceKey] = { deferredActions: [], listeners: new Set(),
|
|
96
|
+
this.instances[instanceKey] = { deferredActions: [], listeners: new Set(), updatedLockedWarnedReducers: new Set(), wrappedUnlockedWarnedReducers: new Set() };
|
|
69
97
|
}
|
|
70
98
|
};
|
|
71
99
|
this.getInstance = (instanceKey) => {
|
|
@@ -77,40 +105,38 @@ class InternalStore {
|
|
|
77
105
|
throw new Error(`Store instance record is missing [store: ${this.name}, instanceKey: ${String(instanceKey)}]. ${INTERNAL_BUG_MESSAGE}`);
|
|
78
106
|
}
|
|
79
107
|
};
|
|
80
|
-
this.
|
|
81
|
-
const instance = this.getInstance(instanceKey);
|
|
108
|
+
this.getState = (instanceKey) => {
|
|
82
109
|
if (this.isInitialized(instanceKey)) {
|
|
83
|
-
return
|
|
110
|
+
return this.stateAll[instanceKey];
|
|
84
111
|
}
|
|
85
112
|
else {
|
|
86
113
|
throw new Error(`Store instance is not initialized [store: ${this.name}, instanceKey: ${String(instanceKey)}]. ${INTERNAL_BUG_MESSAGE}`);
|
|
87
114
|
}
|
|
88
115
|
};
|
|
89
|
-
this.getState = (instanceKey) => {
|
|
90
|
-
const instance = this.getInitializedInstance(instanceKey);
|
|
91
|
-
return instance.state;
|
|
92
|
-
};
|
|
93
116
|
this.getStateAll = () => {
|
|
94
117
|
return this.stateAll;
|
|
95
118
|
};
|
|
96
|
-
|
|
119
|
+
// TODO Охота изменить порядок параметров
|
|
120
|
+
this.initInstance = (instanceKey, initReducer, unlockedInitialState, prefill = {}) => {
|
|
121
|
+
this.ensureInstance(instanceKey);
|
|
97
122
|
const instance = this.getInstance(instanceKey);
|
|
98
123
|
if (this.isInitialized(instanceKey)) {
|
|
99
124
|
throw new Error(`Store instance is already initialized [store: ${this.name}, instanceKey: ${String(instanceKey)}]. ${INTERNAL_BUG_MESSAGE}`);
|
|
100
125
|
}
|
|
101
|
-
else {
|
|
102
|
-
return instance;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
this.initInstance = (instanceKey, initReducer, initialState, prefill = {}) => {
|
|
106
|
-
this.ensureInstance(instanceKey);
|
|
107
|
-
const instance = this.getUninitializedInstance(instanceKey);
|
|
108
126
|
// TODO Смущает наличие `prefill` в этих двух вызовах...
|
|
109
127
|
// Такое ощущение, что он должен маячить где-то в одном из них.
|
|
110
128
|
// TODO Смущает наличие двух этих вызовов подряд...
|
|
111
129
|
// Такое ощущение, что надо бы обойтись одним.
|
|
112
|
-
|
|
113
|
-
|
|
130
|
+
// TODO Надо спросить у ИИ что будет если от первого вызова избавиться и зачем он тут нужен
|
|
131
|
+
const unlockedPrefill = {};
|
|
132
|
+
for (const stateName of Object.keys(prefill)) {
|
|
133
|
+
unlockedPrefill[stateName] = unlock(prefill[stateName]);
|
|
134
|
+
if (unlockedPrefill[stateName] !== prefill[stateName]) {
|
|
135
|
+
console.warn(`You used \`LOCKED\` or \`OVERRIDE_LOCKED\` in prefill [store: ${this.name}, instanceKey: ${String(instanceKey)}, property: ${stateName}]. Pass an unwrapped value instead.`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.applyState(instanceKey, mergePrefill(unlockedInitialState, unlockedPrefill));
|
|
139
|
+
this.dispatch(instanceKey, initReducer, unlockedPrefill);
|
|
114
140
|
// TODO Требуется комментарий в коде...
|
|
115
141
|
// Почему мы сначала сохраняем в переменную массив, и только потом идем по нему? Почему нельзя
|
|
116
142
|
// идти сразу по нему?
|
|
@@ -121,22 +147,59 @@ class InternalStore {
|
|
|
121
147
|
}
|
|
122
148
|
};
|
|
123
149
|
this.isInitialized = (instanceKey) => {
|
|
124
|
-
return
|
|
150
|
+
return instanceKey in this.stateAll;
|
|
151
|
+
};
|
|
152
|
+
this.patchState = (instanceKey, reducer, payload, prevState) => {
|
|
153
|
+
const patch = reducer(prevState, payload) ?? {};
|
|
154
|
+
const updatedLockedStateNames = [];
|
|
155
|
+
const wrappedStateNames = [];
|
|
156
|
+
let nextState = prevState;
|
|
157
|
+
for (const stateName of Object.keys(patch)) {
|
|
158
|
+
const value = unlock(patch[stateName]);
|
|
159
|
+
if (value !== undefined) {
|
|
160
|
+
if (this.lockedStateNames.includes(stateName)) {
|
|
161
|
+
if (!isLockedOverride(patch[stateName])) {
|
|
162
|
+
updatedLockedStateNames.push(stateName);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (value !== patch[stateName]) {
|
|
166
|
+
wrappedStateNames.push(stateName);
|
|
167
|
+
}
|
|
168
|
+
if (value !== nextState[stateName]) {
|
|
169
|
+
nextState = {
|
|
170
|
+
...nextState,
|
|
171
|
+
[stateName]: value,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const instance = this.getInstance(instanceKey);
|
|
177
|
+
if (updatedLockedStateNames.length && !instance.updatedLockedWarnedReducers.has(reducer)) {
|
|
178
|
+
instance.updatedLockedWarnedReducers.add(reducer);
|
|
179
|
+
console.warn(`You updated a property, which is marked as locked [store: ${this.name}, instanceKey: ${String(instanceKey)}, action: ${reducer.name}, property: ${updatedLockedStateNames.join(`, `)}]. Use \`OVERRIDE_LOCKED\`, if you really need to update it.`);
|
|
180
|
+
}
|
|
181
|
+
if (wrappedStateNames.length && !instance.wrappedUnlockedWarnedReducers.has(reducer)) {
|
|
182
|
+
instance.wrappedUnlockedWarnedReducers.add(reducer);
|
|
183
|
+
console.warn(`You used \`LOCKED\` or \`OVERRIDE_LOCKED\` for a property, which is not marked as locked [store: ${this.name}, instanceKey: ${String(instanceKey)}, action: ${reducer.name}, property: ${wrappedStateNames.join(`, `)}]. Return an unwrapped value instead.`);
|
|
184
|
+
}
|
|
185
|
+
return nextState;
|
|
125
186
|
};
|
|
126
187
|
this.pruneInstance = (instanceKey) => {
|
|
127
188
|
const instance = this.getInstance(instanceKey);
|
|
128
|
-
if (!
|
|
189
|
+
if (!this.isInitialized(instanceKey) && !instance.listeners.size && !instance.deferredActions.length) {
|
|
129
190
|
delete this.instances[instanceKey];
|
|
130
191
|
}
|
|
131
192
|
};
|
|
132
193
|
this.resetInstance = (instanceKey, resetReducer) => {
|
|
133
194
|
// TODO Сомнителен порядок вызовов всех этих инструкций, которые ниже
|
|
134
|
-
const instance = this.
|
|
195
|
+
const instance = this.getInstance(instanceKey);
|
|
135
196
|
// TODO Сомнение: что должно нести прощальное состояние сброса и какой быть роли $$reset.
|
|
136
197
|
// Пейлоад у сброса убран, есть идеи на этот счёт, но они ещё не оформились.
|
|
137
|
-
const prevState =
|
|
138
|
-
const nextState =
|
|
198
|
+
const prevState = this.getState(instanceKey);
|
|
199
|
+
const nextState = this.patchState(instanceKey, resetReducer, undefined, prevState);
|
|
139
200
|
this.applyState(instanceKey, undefined);
|
|
201
|
+
instance.updatedLockedWarnedReducers.clear();
|
|
202
|
+
instance.wrappedUnlockedWarnedReducers.clear();
|
|
140
203
|
instance.listeners.forEach((listener) => listener(instanceKey, resetReducer, undefined));
|
|
141
204
|
this.listenersAll.forEach((listener) => listener(instanceKey, resetReducer, undefined));
|
|
142
205
|
// TODO Убрать в логгер
|
|
@@ -172,6 +235,7 @@ class InternalStore {
|
|
|
172
235
|
};
|
|
173
236
|
this.instances = Object.create(null);
|
|
174
237
|
this.listenersAll = new Set();
|
|
238
|
+
this.lockedStateNames = lockedStateNames;
|
|
175
239
|
this.name = name;
|
|
176
240
|
this.stateAll = Object.create(null);
|
|
177
241
|
}
|
|
@@ -180,7 +244,7 @@ class InternalStore {
|
|
|
180
244
|
function ensureInternalStore(storeRegistry, internalStoreProps) {
|
|
181
245
|
let internalStore = storeRegistry[internalStoreProps.uniqKey];
|
|
182
246
|
if (!internalStore) {
|
|
183
|
-
internalStore = new InternalStore(internalStoreProps.name);
|
|
247
|
+
internalStore = new InternalStore(internalStoreProps.name, internalStoreProps.lockedStateNames);
|
|
184
248
|
storeRegistry[internalStoreProps.uniqKey] = internalStore;
|
|
185
249
|
}
|
|
186
250
|
return internalStore;
|
|
@@ -236,7 +300,7 @@ function createUseProp(storeRegistry) {
|
|
|
236
300
|
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
237
301
|
const uninitializedState = storeContextValue?.instanceKey === actualInstanceKey
|
|
238
302
|
? storeContextValue.uninitializedState
|
|
239
|
-
: internalStoreProps.
|
|
303
|
+
: internalStoreProps.unlockedInitialState;
|
|
240
304
|
const subscribe = React.useCallback((listener) => {
|
|
241
305
|
return internalStore.subscribe(actualInstanceKey, listener);
|
|
242
306
|
}, [actualInstanceKey, internalStore]);
|
|
@@ -328,7 +392,7 @@ function createUsePropSelector(storeRegistry) {
|
|
|
328
392
|
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
329
393
|
const uninitializedState = storeContextValue?.instanceKey === actualInstanceKey
|
|
330
394
|
? storeContextValue.uninitializedState
|
|
331
|
-
: internalStoreProps.
|
|
395
|
+
: internalStoreProps.unlockedInitialState;
|
|
332
396
|
return React.useCallback(() => {
|
|
333
397
|
if (actualStateLink.stateName === INITIALIZED_STATE_NAME) {
|
|
334
398
|
return internalStore.isInitialized(actualInstanceKey);
|
|
@@ -398,7 +462,7 @@ function createUseStore(storeRegistry) {
|
|
|
398
462
|
console.log(`Store instance initialization [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
399
463
|
// TODO Убрать в логгер
|
|
400
464
|
console.log({ storeRegistry });
|
|
401
|
-
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.
|
|
465
|
+
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.unlockedInitialState, prefill);
|
|
402
466
|
}
|
|
403
467
|
else {
|
|
404
468
|
console.warn(`Store instance is already initialized, initialization skipped [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
@@ -465,13 +529,35 @@ function createStoreRegistry() {
|
|
|
465
529
|
|
|
466
530
|
function createStore(name, initialState, reducerMap) {
|
|
467
531
|
const context = React.createContext(null);
|
|
532
|
+
const lockedStateNames = [];
|
|
533
|
+
const unlockedInitialState = { ...initialState };
|
|
534
|
+
for (const key in initialState) {
|
|
535
|
+
const value = initialState[key];
|
|
536
|
+
if (isLocked(value)) {
|
|
537
|
+
lockedStateNames.push(key);
|
|
538
|
+
unlockedInitialState[key] = value.value;
|
|
539
|
+
}
|
|
540
|
+
else if (isLockedOverride(value)) {
|
|
541
|
+
lockedStateNames.push(key);
|
|
542
|
+
unlockedInitialState[key] = value.value;
|
|
543
|
+
console.warn(`You used \`OVERRIDE_LOCKED\` in initial state [store: ${name}, property: ${key}]. Use \`LOCKED\` instead.`);
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
unlockedInitialState[key] = value;
|
|
547
|
+
}
|
|
548
|
+
if (unlockedInitialState[key] === undefined) {
|
|
549
|
+
console.warn(`Store property must not be \`undefined\`, use \`null\` instead [store: ${name}, property: ${key}]`);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
468
552
|
const internalStoreProps = {
|
|
469
553
|
$$init: reducerMap.$$init,
|
|
470
554
|
$$reset: reducerMap.$$reset,
|
|
471
555
|
context,
|
|
472
556
|
initialState,
|
|
557
|
+
lockedStateNames,
|
|
473
558
|
name,
|
|
474
559
|
uniqKey: Symbol(`Store: ` + name),
|
|
560
|
+
unlockedInitialState,
|
|
475
561
|
};
|
|
476
562
|
const store = {
|
|
477
563
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
@@ -480,18 +566,18 @@ function createStore(name, initialState, reducerMap) {
|
|
|
480
566
|
store[key] = {
|
|
481
567
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
482
568
|
stateName: key,
|
|
483
|
-
type:
|
|
569
|
+
type: STATE_LINK_FLAG,
|
|
484
570
|
};
|
|
485
571
|
}
|
|
486
572
|
store[INITIALIZED_STATE_NAME] = {
|
|
487
573
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
488
574
|
stateName: INITIALIZED_STATE_NAME,
|
|
489
|
-
type:
|
|
575
|
+
type: STATE_LINK_FLAG,
|
|
490
576
|
};
|
|
491
577
|
store[INSTANCE_KEY_STATE_NAME] = {
|
|
492
578
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
493
579
|
stateName: INSTANCE_KEY_STATE_NAME,
|
|
494
|
-
type:
|
|
580
|
+
type: STATE_LINK_FLAG,
|
|
495
581
|
};
|
|
496
582
|
for (const key in reducerMap) {
|
|
497
583
|
if (!(key in initialState) && ![`$$init`, `$$reset`].includes(key)) {
|
|
@@ -499,13 +585,21 @@ function createStore(name, initialState, reducerMap) {
|
|
|
499
585
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
500
586
|
reducer: reducerMap[key],
|
|
501
587
|
reducerName: key,
|
|
502
|
-
type:
|
|
588
|
+
type: REDUCER_LINK_FLAG,
|
|
503
589
|
};
|
|
504
590
|
}
|
|
505
591
|
}
|
|
506
592
|
return store;
|
|
507
593
|
}
|
|
508
594
|
|
|
595
|
+
function LOCKED(value) {
|
|
596
|
+
return Object.freeze({ kind: LOCKED_FLAG, value });
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function OVERRIDE_LOCKED(value) {
|
|
600
|
+
return Object.freeze({ kind: LOCKED_OVERRIDE_FLAG, value });
|
|
601
|
+
}
|
|
602
|
+
|
|
509
603
|
const requestStateDefault = {
|
|
510
604
|
isAborted: false,
|
|
511
605
|
isFulfilled: false,
|
|
@@ -594,6 +688,8 @@ const { StoreProvider, useDispatcher, useProp, usePropAll, usePropSelector, useP
|
|
|
594
688
|
|
|
595
689
|
exports.DESTROY = DESTROY;
|
|
596
690
|
exports.InternalStore = InternalStore;
|
|
691
|
+
exports.LOCKED = LOCKED;
|
|
692
|
+
exports.OVERRIDE_LOCKED = OVERRIDE_LOCKED;
|
|
597
693
|
exports.StoreProvider = StoreProvider;
|
|
598
694
|
exports.createStore = createStore;
|
|
599
695
|
exports.createStoreRegistry = createStoreRegistry;
|
package/dist/esm/index.esm.js
CHANGED
|
@@ -8,9 +8,39 @@ const INITIALIZED_STATE_NAME = `$$initialized`;
|
|
|
8
8
|
const INSTANCE_KEY_STATE_NAME = `$$instanceKey`;
|
|
9
9
|
const INTERNAL_BUG_MESSAGE = `This must never happen and may mean a bug inside the library itself. Please report it to the library developers.`;
|
|
10
10
|
const INTERNAL_STORE_PROPS_ACCESSOR = Symbol(`INTERNAL_STORE_PROPS`);
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
11
|
+
const LOCKED_FLAG = Symbol(`LOCKED`);
|
|
12
|
+
const LOCKED_OVERRIDE_FLAG = Symbol(`LOCKED_OVERRIDE`);
|
|
13
|
+
const REDUCER_LINK_FLAG = Symbol(`REDUCER_LINK`);
|
|
14
|
+
const STATE_LINK_FLAG = Symbol(`STATE_LINK`);
|
|
15
|
+
const STORE_INSTANCE_UNREGISTERED = Symbol(`STORE_INSTANCE_UNREGISTERED`);
|
|
16
|
+
|
|
17
|
+
function isLocked(value) {
|
|
18
|
+
return typeof value === `object` && value !== null && `kind` in value && value.kind === LOCKED_FLAG;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isLockedOverride(value) {
|
|
22
|
+
return typeof value === `object` && value !== null && `kind` in value && value.kind === LOCKED_OVERRIDE_FLAG;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function unlock(value) {
|
|
26
|
+
if (isLocked(value) || isLockedOverride(value)) {
|
|
27
|
+
return value.value;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function mergePrefill(unlockedInitialState, prefill = {}) {
|
|
35
|
+
const state = { ...unlockedInitialState };
|
|
36
|
+
for (const stateName of Object.keys(prefill)) {
|
|
37
|
+
const value = unlock(prefill[stateName]);
|
|
38
|
+
if (value !== undefined) {
|
|
39
|
+
state[stateName] = value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return state;
|
|
43
|
+
}
|
|
14
44
|
|
|
15
45
|
function createStoreProvider(useStore) {
|
|
16
46
|
return function StoreProvider(props) {
|
|
@@ -22,17 +52,15 @@ function createStoreProvider(useStore) {
|
|
|
22
52
|
return initInstance(prefillRef.current.prefill);
|
|
23
53
|
});
|
|
24
54
|
const contextValue = useMemo(() => {
|
|
25
|
-
return { instanceKey: actualInstanceKey, uninitializedState:
|
|
55
|
+
return { instanceKey: actualInstanceKey, uninitializedState: mergePrefill(internalStoreProps.unlockedInitialState, prefillRef.current.prefill) };
|
|
26
56
|
}, [actualInstanceKey, internalStoreProps]);
|
|
27
57
|
return (jsx(Context.Provider, { value: contextValue, children: props.children }));
|
|
28
58
|
};
|
|
29
59
|
}
|
|
30
60
|
|
|
31
61
|
class InternalStore {
|
|
32
|
-
constructor(name) {
|
|
62
|
+
constructor(name, lockedStateNames = []) {
|
|
33
63
|
this.applyState = (instanceKey, state) => {
|
|
34
|
-
const instance = this.getInstance(instanceKey);
|
|
35
|
-
instance.state = state;
|
|
36
64
|
const stateAll = Object.assign(Object.create(null), this.stateAll);
|
|
37
65
|
if (state) {
|
|
38
66
|
stateAll[instanceKey] = state;
|
|
@@ -45,9 +73,9 @@ class InternalStore {
|
|
|
45
73
|
this.dispatch = (instanceKey, reducer, payload) => {
|
|
46
74
|
this.ensureInstance(instanceKey);
|
|
47
75
|
if (this.isInitialized(instanceKey)) {
|
|
48
|
-
const instance = this.
|
|
49
|
-
const prevState =
|
|
50
|
-
const nextState =
|
|
76
|
+
const instance = this.getInstance(instanceKey);
|
|
77
|
+
const prevState = this.getState(instanceKey);
|
|
78
|
+
const nextState = this.patchState(instanceKey, reducer, payload, prevState);
|
|
51
79
|
this.applyState(instanceKey, nextState);
|
|
52
80
|
instance.listeners.forEach((listener) => listener(instanceKey, reducer, payload));
|
|
53
81
|
this.listenersAll.forEach((listener) => listener(instanceKey, reducer, payload));
|
|
@@ -57,13 +85,13 @@ class InternalStore {
|
|
|
57
85
|
console.log({ payload, [`prev state`]: prevState, [`next state`]: nextState });
|
|
58
86
|
}
|
|
59
87
|
else {
|
|
60
|
-
const instance = this.
|
|
88
|
+
const instance = this.getInstance(instanceKey);
|
|
61
89
|
instance.deferredActions.push({ payload, reducer });
|
|
62
90
|
}
|
|
63
91
|
};
|
|
64
92
|
this.ensureInstance = (instanceKey) => {
|
|
65
93
|
if (!this.instances[instanceKey]) {
|
|
66
|
-
this.instances[instanceKey] = { deferredActions: [], listeners: new Set(),
|
|
94
|
+
this.instances[instanceKey] = { deferredActions: [], listeners: new Set(), updatedLockedWarnedReducers: new Set(), wrappedUnlockedWarnedReducers: new Set() };
|
|
67
95
|
}
|
|
68
96
|
};
|
|
69
97
|
this.getInstance = (instanceKey) => {
|
|
@@ -75,40 +103,38 @@ class InternalStore {
|
|
|
75
103
|
throw new Error(`Store instance record is missing [store: ${this.name}, instanceKey: ${String(instanceKey)}]. ${INTERNAL_BUG_MESSAGE}`);
|
|
76
104
|
}
|
|
77
105
|
};
|
|
78
|
-
this.
|
|
79
|
-
const instance = this.getInstance(instanceKey);
|
|
106
|
+
this.getState = (instanceKey) => {
|
|
80
107
|
if (this.isInitialized(instanceKey)) {
|
|
81
|
-
return
|
|
108
|
+
return this.stateAll[instanceKey];
|
|
82
109
|
}
|
|
83
110
|
else {
|
|
84
111
|
throw new Error(`Store instance is not initialized [store: ${this.name}, instanceKey: ${String(instanceKey)}]. ${INTERNAL_BUG_MESSAGE}`);
|
|
85
112
|
}
|
|
86
113
|
};
|
|
87
|
-
this.getState = (instanceKey) => {
|
|
88
|
-
const instance = this.getInitializedInstance(instanceKey);
|
|
89
|
-
return instance.state;
|
|
90
|
-
};
|
|
91
114
|
this.getStateAll = () => {
|
|
92
115
|
return this.stateAll;
|
|
93
116
|
};
|
|
94
|
-
|
|
117
|
+
// TODO Охота изменить порядок параметров
|
|
118
|
+
this.initInstance = (instanceKey, initReducer, unlockedInitialState, prefill = {}) => {
|
|
119
|
+
this.ensureInstance(instanceKey);
|
|
95
120
|
const instance = this.getInstance(instanceKey);
|
|
96
121
|
if (this.isInitialized(instanceKey)) {
|
|
97
122
|
throw new Error(`Store instance is already initialized [store: ${this.name}, instanceKey: ${String(instanceKey)}]. ${INTERNAL_BUG_MESSAGE}`);
|
|
98
123
|
}
|
|
99
|
-
else {
|
|
100
|
-
return instance;
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
this.initInstance = (instanceKey, initReducer, initialState, prefill = {}) => {
|
|
104
|
-
this.ensureInstance(instanceKey);
|
|
105
|
-
const instance = this.getUninitializedInstance(instanceKey);
|
|
106
124
|
// TODO Смущает наличие `prefill` в этих двух вызовах...
|
|
107
125
|
// Такое ощущение, что он должен маячить где-то в одном из них.
|
|
108
126
|
// TODO Смущает наличие двух этих вызовов подряд...
|
|
109
127
|
// Такое ощущение, что надо бы обойтись одним.
|
|
110
|
-
|
|
111
|
-
|
|
128
|
+
// TODO Надо спросить у ИИ что будет если от первого вызова избавиться и зачем он тут нужен
|
|
129
|
+
const unlockedPrefill = {};
|
|
130
|
+
for (const stateName of Object.keys(prefill)) {
|
|
131
|
+
unlockedPrefill[stateName] = unlock(prefill[stateName]);
|
|
132
|
+
if (unlockedPrefill[stateName] !== prefill[stateName]) {
|
|
133
|
+
console.warn(`You used \`LOCKED\` or \`OVERRIDE_LOCKED\` in prefill [store: ${this.name}, instanceKey: ${String(instanceKey)}, property: ${stateName}]. Pass an unwrapped value instead.`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
this.applyState(instanceKey, mergePrefill(unlockedInitialState, unlockedPrefill));
|
|
137
|
+
this.dispatch(instanceKey, initReducer, unlockedPrefill);
|
|
112
138
|
// TODO Требуется комментарий в коде...
|
|
113
139
|
// Почему мы сначала сохраняем в переменную массив, и только потом идем по нему? Почему нельзя
|
|
114
140
|
// идти сразу по нему?
|
|
@@ -119,22 +145,59 @@ class InternalStore {
|
|
|
119
145
|
}
|
|
120
146
|
};
|
|
121
147
|
this.isInitialized = (instanceKey) => {
|
|
122
|
-
return
|
|
148
|
+
return instanceKey in this.stateAll;
|
|
149
|
+
};
|
|
150
|
+
this.patchState = (instanceKey, reducer, payload, prevState) => {
|
|
151
|
+
const patch = reducer(prevState, payload) ?? {};
|
|
152
|
+
const updatedLockedStateNames = [];
|
|
153
|
+
const wrappedStateNames = [];
|
|
154
|
+
let nextState = prevState;
|
|
155
|
+
for (const stateName of Object.keys(patch)) {
|
|
156
|
+
const value = unlock(patch[stateName]);
|
|
157
|
+
if (value !== undefined) {
|
|
158
|
+
if (this.lockedStateNames.includes(stateName)) {
|
|
159
|
+
if (!isLockedOverride(patch[stateName])) {
|
|
160
|
+
updatedLockedStateNames.push(stateName);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (value !== patch[stateName]) {
|
|
164
|
+
wrappedStateNames.push(stateName);
|
|
165
|
+
}
|
|
166
|
+
if (value !== nextState[stateName]) {
|
|
167
|
+
nextState = {
|
|
168
|
+
...nextState,
|
|
169
|
+
[stateName]: value,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const instance = this.getInstance(instanceKey);
|
|
175
|
+
if (updatedLockedStateNames.length && !instance.updatedLockedWarnedReducers.has(reducer)) {
|
|
176
|
+
instance.updatedLockedWarnedReducers.add(reducer);
|
|
177
|
+
console.warn(`You updated a property, which is marked as locked [store: ${this.name}, instanceKey: ${String(instanceKey)}, action: ${reducer.name}, property: ${updatedLockedStateNames.join(`, `)}]. Use \`OVERRIDE_LOCKED\`, if you really need to update it.`);
|
|
178
|
+
}
|
|
179
|
+
if (wrappedStateNames.length && !instance.wrappedUnlockedWarnedReducers.has(reducer)) {
|
|
180
|
+
instance.wrappedUnlockedWarnedReducers.add(reducer);
|
|
181
|
+
console.warn(`You used \`LOCKED\` or \`OVERRIDE_LOCKED\` for a property, which is not marked as locked [store: ${this.name}, instanceKey: ${String(instanceKey)}, action: ${reducer.name}, property: ${wrappedStateNames.join(`, `)}]. Return an unwrapped value instead.`);
|
|
182
|
+
}
|
|
183
|
+
return nextState;
|
|
123
184
|
};
|
|
124
185
|
this.pruneInstance = (instanceKey) => {
|
|
125
186
|
const instance = this.getInstance(instanceKey);
|
|
126
|
-
if (!
|
|
187
|
+
if (!this.isInitialized(instanceKey) && !instance.listeners.size && !instance.deferredActions.length) {
|
|
127
188
|
delete this.instances[instanceKey];
|
|
128
189
|
}
|
|
129
190
|
};
|
|
130
191
|
this.resetInstance = (instanceKey, resetReducer) => {
|
|
131
192
|
// TODO Сомнителен порядок вызовов всех этих инструкций, которые ниже
|
|
132
|
-
const instance = this.
|
|
193
|
+
const instance = this.getInstance(instanceKey);
|
|
133
194
|
// TODO Сомнение: что должно нести прощальное состояние сброса и какой быть роли $$reset.
|
|
134
195
|
// Пейлоад у сброса убран, есть идеи на этот счёт, но они ещё не оформились.
|
|
135
|
-
const prevState =
|
|
136
|
-
const nextState =
|
|
196
|
+
const prevState = this.getState(instanceKey);
|
|
197
|
+
const nextState = this.patchState(instanceKey, resetReducer, undefined, prevState);
|
|
137
198
|
this.applyState(instanceKey, undefined);
|
|
199
|
+
instance.updatedLockedWarnedReducers.clear();
|
|
200
|
+
instance.wrappedUnlockedWarnedReducers.clear();
|
|
138
201
|
instance.listeners.forEach((listener) => listener(instanceKey, resetReducer, undefined));
|
|
139
202
|
this.listenersAll.forEach((listener) => listener(instanceKey, resetReducer, undefined));
|
|
140
203
|
// TODO Убрать в логгер
|
|
@@ -170,6 +233,7 @@ class InternalStore {
|
|
|
170
233
|
};
|
|
171
234
|
this.instances = Object.create(null);
|
|
172
235
|
this.listenersAll = new Set();
|
|
236
|
+
this.lockedStateNames = lockedStateNames;
|
|
173
237
|
this.name = name;
|
|
174
238
|
this.stateAll = Object.create(null);
|
|
175
239
|
}
|
|
@@ -178,7 +242,7 @@ class InternalStore {
|
|
|
178
242
|
function ensureInternalStore(storeRegistry, internalStoreProps) {
|
|
179
243
|
let internalStore = storeRegistry[internalStoreProps.uniqKey];
|
|
180
244
|
if (!internalStore) {
|
|
181
|
-
internalStore = new InternalStore(internalStoreProps.name);
|
|
245
|
+
internalStore = new InternalStore(internalStoreProps.name, internalStoreProps.lockedStateNames);
|
|
182
246
|
storeRegistry[internalStoreProps.uniqKey] = internalStore;
|
|
183
247
|
}
|
|
184
248
|
return internalStore;
|
|
@@ -234,7 +298,7 @@ function createUseProp(storeRegistry) {
|
|
|
234
298
|
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
235
299
|
const uninitializedState = storeContextValue?.instanceKey === actualInstanceKey
|
|
236
300
|
? storeContextValue.uninitializedState
|
|
237
|
-
: internalStoreProps.
|
|
301
|
+
: internalStoreProps.unlockedInitialState;
|
|
238
302
|
const subscribe = useCallback((listener) => {
|
|
239
303
|
return internalStore.subscribe(actualInstanceKey, listener);
|
|
240
304
|
}, [actualInstanceKey, internalStore]);
|
|
@@ -326,7 +390,7 @@ function createUsePropSelector(storeRegistry) {
|
|
|
326
390
|
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
327
391
|
const uninitializedState = storeContextValue?.instanceKey === actualInstanceKey
|
|
328
392
|
? storeContextValue.uninitializedState
|
|
329
|
-
: internalStoreProps.
|
|
393
|
+
: internalStoreProps.unlockedInitialState;
|
|
330
394
|
return useCallback(() => {
|
|
331
395
|
if (actualStateLink.stateName === INITIALIZED_STATE_NAME) {
|
|
332
396
|
return internalStore.isInitialized(actualInstanceKey);
|
|
@@ -396,7 +460,7 @@ function createUseStore(storeRegistry) {
|
|
|
396
460
|
console.log(`Store instance initialization [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
397
461
|
// TODO Убрать в логгер
|
|
398
462
|
console.log({ storeRegistry });
|
|
399
|
-
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.
|
|
463
|
+
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.unlockedInitialState, prefill);
|
|
400
464
|
}
|
|
401
465
|
else {
|
|
402
466
|
console.warn(`Store instance is already initialized, initialization skipped [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
@@ -463,13 +527,35 @@ function createStoreRegistry() {
|
|
|
463
527
|
|
|
464
528
|
function createStore(name, initialState, reducerMap) {
|
|
465
529
|
const context = React.createContext(null);
|
|
530
|
+
const lockedStateNames = [];
|
|
531
|
+
const unlockedInitialState = { ...initialState };
|
|
532
|
+
for (const key in initialState) {
|
|
533
|
+
const value = initialState[key];
|
|
534
|
+
if (isLocked(value)) {
|
|
535
|
+
lockedStateNames.push(key);
|
|
536
|
+
unlockedInitialState[key] = value.value;
|
|
537
|
+
}
|
|
538
|
+
else if (isLockedOverride(value)) {
|
|
539
|
+
lockedStateNames.push(key);
|
|
540
|
+
unlockedInitialState[key] = value.value;
|
|
541
|
+
console.warn(`You used \`OVERRIDE_LOCKED\` in initial state [store: ${name}, property: ${key}]. Use \`LOCKED\` instead.`);
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
unlockedInitialState[key] = value;
|
|
545
|
+
}
|
|
546
|
+
if (unlockedInitialState[key] === undefined) {
|
|
547
|
+
console.warn(`Store property must not be \`undefined\`, use \`null\` instead [store: ${name}, property: ${key}]`);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
466
550
|
const internalStoreProps = {
|
|
467
551
|
$$init: reducerMap.$$init,
|
|
468
552
|
$$reset: reducerMap.$$reset,
|
|
469
553
|
context,
|
|
470
554
|
initialState,
|
|
555
|
+
lockedStateNames,
|
|
471
556
|
name,
|
|
472
557
|
uniqKey: Symbol(`Store: ` + name),
|
|
558
|
+
unlockedInitialState,
|
|
473
559
|
};
|
|
474
560
|
const store = {
|
|
475
561
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
@@ -478,18 +564,18 @@ function createStore(name, initialState, reducerMap) {
|
|
|
478
564
|
store[key] = {
|
|
479
565
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
480
566
|
stateName: key,
|
|
481
|
-
type:
|
|
567
|
+
type: STATE_LINK_FLAG,
|
|
482
568
|
};
|
|
483
569
|
}
|
|
484
570
|
store[INITIALIZED_STATE_NAME] = {
|
|
485
571
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
486
572
|
stateName: INITIALIZED_STATE_NAME,
|
|
487
|
-
type:
|
|
573
|
+
type: STATE_LINK_FLAG,
|
|
488
574
|
};
|
|
489
575
|
store[INSTANCE_KEY_STATE_NAME] = {
|
|
490
576
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
491
577
|
stateName: INSTANCE_KEY_STATE_NAME,
|
|
492
|
-
type:
|
|
578
|
+
type: STATE_LINK_FLAG,
|
|
493
579
|
};
|
|
494
580
|
for (const key in reducerMap) {
|
|
495
581
|
if (!(key in initialState) && ![`$$init`, `$$reset`].includes(key)) {
|
|
@@ -497,13 +583,21 @@ function createStore(name, initialState, reducerMap) {
|
|
|
497
583
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
498
584
|
reducer: reducerMap[key],
|
|
499
585
|
reducerName: key,
|
|
500
|
-
type:
|
|
586
|
+
type: REDUCER_LINK_FLAG,
|
|
501
587
|
};
|
|
502
588
|
}
|
|
503
589
|
}
|
|
504
590
|
return store;
|
|
505
591
|
}
|
|
506
592
|
|
|
593
|
+
function LOCKED(value) {
|
|
594
|
+
return Object.freeze({ kind: LOCKED_FLAG, value });
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function OVERRIDE_LOCKED(value) {
|
|
598
|
+
return Object.freeze({ kind: LOCKED_OVERRIDE_FLAG, value });
|
|
599
|
+
}
|
|
600
|
+
|
|
507
601
|
const requestStateDefault = {
|
|
508
602
|
isAborted: false,
|
|
509
603
|
isFulfilled: false,
|
|
@@ -590,4 +684,4 @@ const requestState = {
|
|
|
590
684
|
|
|
591
685
|
const { StoreProvider, useDispatcher, useProp, usePropAll, usePropSelector, usePropSelectorAll, useStore, } = createStoreRegistry();
|
|
592
686
|
|
|
593
|
-
export { DESTROY, InternalStore, StoreProvider, createStore, createStoreRegistry, requestState, requestStatus, useDispatcher, useProp, usePropAll, usePropSelector, usePropSelectorAll, useStore };
|
|
687
|
+
export { DESTROY, InternalStore, LOCKED, OVERRIDE_LOCKED, StoreProvider, createStore, createStoreRegistry, requestState, requestStatus, useDispatcher, useProp, usePropAll, usePropSelector, usePropSelectorAll, useStore };
|
package/package.json
CHANGED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { InitializedStoreInstance } from '../InitializedStoreInstance.tsx';
|
|
2
|
-
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
3
|
-
import type { StoreState } from '../StoreState.tsx';
|
|
4
|
-
export type GetInitializedInstance<TStoreState extends StoreState = StoreState> = {
|
|
5
|
-
(instanceKey: InstanceKey): InitializedStoreInstance<TStoreState>;
|
|
6
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { InstanceKey } from '../InstanceKey.tsx';
|
|
2
|
-
import type { StoreState } from '../StoreState.tsx';
|
|
3
|
-
import type { UninitializedStoreInstance } from '../UninitializedStoreInstance.tsx';
|
|
4
|
-
export type GetUninitializedInstance<TStoreState extends StoreState = StoreState> = {
|
|
5
|
-
(instanceKey: InstanceKey): UninitializedStoreInstance<TStoreState>;
|
|
6
|
-
};
|