beestate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/InternalStore.d.ts +62 -0
- package/dist/StoreProvider.d.ts +8 -0
- package/dist/cjs/index.cjs.js +343 -0
- package/dist/constants.d.ts +6 -0
- package/dist/createStore.d.ts +10 -0
- package/dist/createStoreRegistry.d.ts +9 -0
- package/dist/createUseDispatcher.d.ts +7 -0
- package/dist/createUseSelector.d.ts +1 -0
- package/dist/createUseState.d.ts +6 -0
- package/dist/createUseStateAll.d.ts +6 -0
- package/dist/createUseStore.d.ts +13 -0
- package/dist/esm/index.esm.js +333 -0
- package/dist/index.d.ts +10 -0
- package/dist/types/Dispatcher.d.ts +8 -0
- package/dist/types/InitInstance.d.ts +5 -0
- package/dist/types/InitPayload.d.ts +4 -0
- package/dist/types/InstanceKey.d.ts +1 -0
- package/dist/types/InternalStoreProps.d.ts +12 -0
- package/dist/types/Payload.d.ts +1 -0
- package/dist/types/Reducer.d.ts +5 -0
- package/dist/types/ReducerLink.d.ts +11 -0
- package/dist/types/ReducerMap.d.ts +6 -0
- package/dist/types/ReducerPayload.d.ts +4 -0
- package/dist/types/RegisterDefaultInstance.d.ts +6 -0
- package/dist/types/RegisterInstance.d.ts +7 -0
- package/dist/types/ResetInstance.d.ts +5 -0
- package/dist/types/ResetPayload.d.ts +4 -0
- package/dist/types/StateLink.d.ts +10 -0
- package/dist/types/Store.d.ts +16 -0
- package/dist/types/StoreController.d.ts +9 -0
- package/dist/types/StoreControllerInstance.d.ts +7 -0
- package/dist/types/StoreRegistry.d.ts +2 -0
- package/dist/types/StoreState.d.ts +1 -0
- package/dist/types/index.d.ts +20 -0
- package/package.json +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kudo contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type InstanceKey } from './types/index.tsx';
|
|
2
|
+
import { type Payload } from './types/index.tsx';
|
|
3
|
+
import { type Reducer } from './types/index.tsx';
|
|
4
|
+
import { type StoreState } from './types/index.tsx';
|
|
5
|
+
export type Dispatch<TStoreState extends StoreState = StoreState> = {
|
|
6
|
+
<TPayload extends Payload = Payload>(instanceKey: InstanceKey, payload: TPayload, reducer: Reducer<TStoreState, TPayload>): void;
|
|
7
|
+
};
|
|
8
|
+
export type GetSnapshot<TStoreState extends StoreState = StoreState> = {
|
|
9
|
+
(instanceKey: InstanceKey): TStoreState;
|
|
10
|
+
};
|
|
11
|
+
export type GetSnapshotAll<TStoreState extends StoreState = StoreState> = {
|
|
12
|
+
(): Record<symbol | string, TStoreState>;
|
|
13
|
+
};
|
|
14
|
+
export type Listener = {
|
|
15
|
+
(): void;
|
|
16
|
+
};
|
|
17
|
+
export type ListenerAll = {
|
|
18
|
+
(): void;
|
|
19
|
+
};
|
|
20
|
+
export type Subscribe = {
|
|
21
|
+
(instanceKey: InstanceKey, listener: Listener): Unsubscribe;
|
|
22
|
+
};
|
|
23
|
+
export type Unsubscribe = {
|
|
24
|
+
(): void;
|
|
25
|
+
};
|
|
26
|
+
export type SubscribeAll = {
|
|
27
|
+
(listener: ListenerAll): UnsubscribeAll;
|
|
28
|
+
};
|
|
29
|
+
export type UnsubscribeAll = {
|
|
30
|
+
(): void;
|
|
31
|
+
};
|
|
32
|
+
export type Update<TStoreState extends StoreState = StoreState> = {
|
|
33
|
+
(instanceKey: InstanceKey, newState: TStoreState): void;
|
|
34
|
+
};
|
|
35
|
+
export type InitInstance<TStoreState extends StoreState = StoreState, TInitPayload extends void | Payload = void | Payload, TResetPayload extends void | Payload = void | Payload> = {
|
|
36
|
+
(instanceKey: InstanceKey, initialState: TStoreState, initPayload: TInitPayload, initReducer: Reducer<TStoreState, TInitPayload>): ResetInstance<TStoreState, TResetPayload>;
|
|
37
|
+
};
|
|
38
|
+
export type ResetInstance<TStoreState extends StoreState = StoreState, TResetPayload extends void | Payload = void | Payload> = {
|
|
39
|
+
(resetPayload: TResetPayload, resetReducer: Reducer<TStoreState, TResetPayload>): void;
|
|
40
|
+
};
|
|
41
|
+
export type GetState<TStoreState extends StoreState = StoreState> = {
|
|
42
|
+
(instanceKey: InstanceKey): TStoreState;
|
|
43
|
+
};
|
|
44
|
+
export type GetStateAll<TStoreState extends StoreState = StoreState> = {
|
|
45
|
+
(): Record<InstanceKey, TStoreState>;
|
|
46
|
+
};
|
|
47
|
+
export declare class InternalStore<TStoreState extends StoreState = StoreState, TInitPayload extends void | Payload = void | Payload, TResetPayload extends void | Payload = void | Payload> {
|
|
48
|
+
private listeners;
|
|
49
|
+
private listenersAll;
|
|
50
|
+
name: string;
|
|
51
|
+
private state;
|
|
52
|
+
constructor(name: string);
|
|
53
|
+
getStateAll: GetStateAll<TStoreState>;
|
|
54
|
+
getState: GetState<TStoreState>;
|
|
55
|
+
initInstance: InitInstance<TStoreState, TInitPayload, TResetPayload>;
|
|
56
|
+
subscribe: Subscribe;
|
|
57
|
+
subscribeAll: SubscribeAll;
|
|
58
|
+
getSnapshot: GetSnapshot<TStoreState>;
|
|
59
|
+
getSnapshotAll: GetSnapshotAll<TStoreState>;
|
|
60
|
+
dispatch: Dispatch<TStoreState>;
|
|
61
|
+
update: Update<TStoreState>;
|
|
62
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type InstanceKey } from './types/index.tsx';
|
|
3
|
+
import { type Store } from './types/index.tsx';
|
|
4
|
+
export type StoreProviderProps = React.PropsWithChildren<{
|
|
5
|
+
store: Store;
|
|
6
|
+
instanceKey: InstanceKey;
|
|
7
|
+
}>;
|
|
8
|
+
export declare function StoreProvider(props: StoreProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var withSelector = require('use-sync-external-store/with-selector');
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_INSTANCE_KEY = Symbol(`DEFAULT_INSTANCE_KEY`);
|
|
8
|
+
const DESTROY = Symbol(`DESTROY_INSTRUCTION`);
|
|
9
|
+
const INTERNAL_STORE_PROPS_ACCESSOR = Symbol(`INTERNAL_STORE_PROPS`);
|
|
10
|
+
const REDUCER_LINK = Symbol(`REDUCER_LINK`);
|
|
11
|
+
const STATE_LINK = Symbol(`STATE_LINK`);
|
|
12
|
+
const STORE_INSTANCE_UNREGISTERED = Symbol(`STORE_INSTANCE_UNREGISTERED_INSTRUCTION`);
|
|
13
|
+
|
|
14
|
+
function createUseDispatcher(storeRegistry) {
|
|
15
|
+
return function useDispatcher(reducerLink) {
|
|
16
|
+
const contextInstanceKey = React.useContext(reducerLink[INTERNAL_STORE_PROPS_ACCESSOR].context);
|
|
17
|
+
return React.useMemo(() => {
|
|
18
|
+
const internalStoreProps = reducerLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
19
|
+
function dispatch(...args) {
|
|
20
|
+
let instanceKey;
|
|
21
|
+
let payload;
|
|
22
|
+
if ([`number`, `symbol`, `string`].includes(typeof args[0])) {
|
|
23
|
+
instanceKey = args[0];
|
|
24
|
+
payload = args[1];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
payload = args[0];
|
|
28
|
+
}
|
|
29
|
+
const actualInstanceKey = instanceKey ?? contextInstanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
30
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
31
|
+
if (!(internalStoreProps.uniqKey in storeRegistry)) {
|
|
32
|
+
throw new Error(`Store "${internalStoreProps.name}" is not registered.`);
|
|
33
|
+
}
|
|
34
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
35
|
+
if (!(storeRegistry[internalStoreProps.uniqKey].instances[actualInstanceKey])) {
|
|
36
|
+
throw new Error(`Store "${internalStoreProps.name}" is not initialized for instance "${String(actualInstanceKey)}".`);
|
|
37
|
+
}
|
|
38
|
+
const internalStore = storeRegistry[internalStoreProps.uniqKey].internalStore;
|
|
39
|
+
internalStore.dispatch(actualInstanceKey, payload, reducerLink.reducer);
|
|
40
|
+
}
|
|
41
|
+
return dispatch;
|
|
42
|
+
}, [contextInstanceKey, reducerLink]);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createUseState(storeRegistry) {
|
|
47
|
+
return function useState(stateLink, instanceKey) {
|
|
48
|
+
const internalStoreProps = stateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
49
|
+
const contextInstanceKey = React.useContext(internalStoreProps.context);
|
|
50
|
+
const actualInstanceKey = instanceKey ?? contextInstanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
51
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
52
|
+
if (!(internalStoreProps.uniqKey in storeRegistry)) {
|
|
53
|
+
throw new Error(`Store "${internalStoreProps.name}" is not registered.`);
|
|
54
|
+
}
|
|
55
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
56
|
+
if (!(storeRegistry[internalStoreProps.uniqKey].instances[actualInstanceKey])) {
|
|
57
|
+
throw new Error(`Store "${internalStoreProps.name}" is not initialized for instance "${String(actualInstanceKey)}".`);
|
|
58
|
+
}
|
|
59
|
+
const internalStore = storeRegistry[internalStoreProps.uniqKey].internalStore;
|
|
60
|
+
const subscribe = React.useCallback((listener) => {
|
|
61
|
+
return internalStore.subscribe(actualInstanceKey, listener);
|
|
62
|
+
}, [actualInstanceKey, internalStore]);
|
|
63
|
+
const getSnapshot = React.useCallback(() => {
|
|
64
|
+
return internalStore.getSnapshot(actualInstanceKey);
|
|
65
|
+
}, [actualInstanceKey, internalStore]);
|
|
66
|
+
const selector = React.useCallback((state) => {
|
|
67
|
+
return state[stateLink.stateName];
|
|
68
|
+
}, [stateLink]);
|
|
69
|
+
return withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, selector);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function createUseStateAll(storeRegistry) {
|
|
74
|
+
return function useStateAll(stateLink, instanceKeys) {
|
|
75
|
+
const internalStoreProps = stateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
76
|
+
const prevStateAllRef = React.useRef({ prevStateAll: [] });
|
|
77
|
+
if (!(internalStoreProps.uniqKey in storeRegistry)) {
|
|
78
|
+
throw new Error(`Store "${internalStoreProps.name}" is not registered.`);
|
|
79
|
+
}
|
|
80
|
+
const internalStore = storeRegistry[internalStoreProps.uniqKey].internalStore;
|
|
81
|
+
const subscribe = React.useCallback((listener) => {
|
|
82
|
+
return internalStore.subscribeAll(listener);
|
|
83
|
+
}, [internalStore]);
|
|
84
|
+
const getSnapshot = React.useCallback(() => {
|
|
85
|
+
return internalStore.getSnapshotAll();
|
|
86
|
+
}, [internalStore]);
|
|
87
|
+
const selector = React.useCallback((state) => {
|
|
88
|
+
let stateAll;
|
|
89
|
+
if (instanceKeys == null) {
|
|
90
|
+
stateAll = Object.entries(state)
|
|
91
|
+
.map(([instanceKey, state]) => {
|
|
92
|
+
return [instanceKey, state[stateLink.stateName]];
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else if (typeof instanceKeys === `function`) {
|
|
96
|
+
stateAll = Object.entries(state)
|
|
97
|
+
.filter(([_instanceKey, state]) => {
|
|
98
|
+
return instanceKeys(state);
|
|
99
|
+
})
|
|
100
|
+
.map(([instanceKey, state]) => {
|
|
101
|
+
return [instanceKey, state[stateLink.stateName]];
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
stateAll = instanceKeys.map((instanceKey) => {
|
|
106
|
+
return [instanceKey, state[instanceKey][stateLink.stateName]];
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (isEqual(prevStateAllRef.current.prevStateAll, stateAll)) {
|
|
110
|
+
prevStateAllRef.current.prevStateAll = stateAll;
|
|
111
|
+
return stateAll;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return prevStateAllRef.current.prevStateAll;
|
|
115
|
+
}
|
|
116
|
+
}, [instanceKeys, stateLink]);
|
|
117
|
+
return withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, selector);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function isEqual(prevStateAll, stateAll) {
|
|
121
|
+
if (prevStateAll.length !== stateAll.length) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return prevStateAll.every(([prevInstanceKey, prevState]) => {
|
|
126
|
+
const stateAllEntry = stateAll.find(([instanceKey]) => {
|
|
127
|
+
return prevInstanceKey === instanceKey;
|
|
128
|
+
});
|
|
129
|
+
return stateAllEntry && stateAllEntry[1] === prevState;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
class InternalStore {
|
|
135
|
+
constructor(name) {
|
|
136
|
+
this.getStateAll = () => {
|
|
137
|
+
return this.state;
|
|
138
|
+
};
|
|
139
|
+
this.getState = (instanceKey) => {
|
|
140
|
+
return this.state[instanceKey];
|
|
141
|
+
};
|
|
142
|
+
this.initInstance = (instanceKey, initialState, initPayload, initReducer) => {
|
|
143
|
+
const initState = initReducer(initialState, initPayload);
|
|
144
|
+
this.state = {
|
|
145
|
+
...this.state,
|
|
146
|
+
[instanceKey]: initState,
|
|
147
|
+
};
|
|
148
|
+
// TODO Убрать в логгер
|
|
149
|
+
console.log(`initInstance [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
150
|
+
console.dir(this.state);
|
|
151
|
+
return (resetPayload, resetReducer) => {
|
|
152
|
+
const resetState = resetReducer(initState, resetPayload);
|
|
153
|
+
// TODO Тут место для уничтожения состояния экземпляра хранилища через `delete this.state[instanceKey];`
|
|
154
|
+
this.state = { ...this.state, [instanceKey]: resetState };
|
|
155
|
+
// TODO Убрать в логгер
|
|
156
|
+
console.log(`resetInstance [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
157
|
+
console.dir(this.state);
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
this.subscribe = (instanceKey, listener) => {
|
|
161
|
+
if (!this.listeners[instanceKey]) {
|
|
162
|
+
this.listeners[instanceKey] = new Set();
|
|
163
|
+
}
|
|
164
|
+
this.listeners[instanceKey].add(listener);
|
|
165
|
+
return () => {
|
|
166
|
+
this.listeners[instanceKey].delete(listener);
|
|
167
|
+
if (this.listeners[instanceKey].size === 0) {
|
|
168
|
+
delete this.listeners[instanceKey];
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
this.subscribeAll = (listener) => {
|
|
173
|
+
this.listenersAll.add(listener);
|
|
174
|
+
return () => {
|
|
175
|
+
this.listenersAll.delete(listener);
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
this.getSnapshot = (instanceKey) => {
|
|
179
|
+
return this.state[instanceKey];
|
|
180
|
+
};
|
|
181
|
+
this.getSnapshotAll = () => {
|
|
182
|
+
return this.state;
|
|
183
|
+
};
|
|
184
|
+
this.dispatch = (instanceKey, payload, reducer) => {
|
|
185
|
+
const changes = reducer(this.state[instanceKey], payload);
|
|
186
|
+
this.update(instanceKey, changes);
|
|
187
|
+
};
|
|
188
|
+
this.update = (instanceKey, changes) => {
|
|
189
|
+
if (this.state[instanceKey] !== changes) {
|
|
190
|
+
this.state = { ...this.state, [instanceKey]: changes };
|
|
191
|
+
this.listeners[instanceKey].forEach((listener) => listener());
|
|
192
|
+
this.listenersAll.forEach((listener) => listener());
|
|
193
|
+
// TODO Убрать в логгер
|
|
194
|
+
console.log(`update [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
195
|
+
console.dir(this.state);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
this.listeners = {};
|
|
199
|
+
this.listenersAll = new Set();
|
|
200
|
+
this.name = name;
|
|
201
|
+
this.state = {};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function createUseStore(storeRegistry) {
|
|
206
|
+
function useStore(store, ...args) {
|
|
207
|
+
let instanceKeys;
|
|
208
|
+
let registerInstance;
|
|
209
|
+
if (typeof args[0] === `function`) {
|
|
210
|
+
instanceKeys = [];
|
|
211
|
+
registerInstance = args[0];
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
instanceKeys = args[0];
|
|
215
|
+
registerInstance = args[1];
|
|
216
|
+
}
|
|
217
|
+
const actualInstanceKeys = instanceKeys.length ? instanceKeys : [DEFAULT_INSTANCE_KEY];
|
|
218
|
+
const storeRef = React.useRef({ store });
|
|
219
|
+
const ownerKeyRef = React.useRef({ ownerKey: Symbol(`ownerKey`) });
|
|
220
|
+
const initInstancesRef = React.useRef({ initInstances: {} });
|
|
221
|
+
React.useMemo(() => {
|
|
222
|
+
const initInstances = initInstancesRef.current.initInstances;
|
|
223
|
+
const store = storeRef.current.store;
|
|
224
|
+
for (const instanceKey of actualInstanceKeys) {
|
|
225
|
+
if (!initInstances[instanceKey]) {
|
|
226
|
+
const ownerKey = ownerKeyRef.current.ownerKey;
|
|
227
|
+
const initInstance = (initPayload) => {
|
|
228
|
+
const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
229
|
+
let storeController = storeRegistry[internalStoreProps.uniqKey];
|
|
230
|
+
if (!storeController) {
|
|
231
|
+
const internalStore = new InternalStore(internalStoreProps.name);
|
|
232
|
+
storeController = { internalStore, instances: {} };
|
|
233
|
+
storeRegistry[internalStoreProps.uniqKey] = storeController;
|
|
234
|
+
// TODO Убрать в логгер
|
|
235
|
+
console.log(`registerStore [store: ${storeController.internalStore.name}]:`);
|
|
236
|
+
console.dir(storeRegistry);
|
|
237
|
+
}
|
|
238
|
+
if (!storeController.instances[instanceKey]) {
|
|
239
|
+
const resetState = storeController.internalStore.initInstance(instanceKey, internalStoreProps.initialState, initPayload, internalStoreProps[`$$init`]);
|
|
240
|
+
storeController.instances[instanceKey] = { owners: [], resetState };
|
|
241
|
+
}
|
|
242
|
+
storeController.instances[instanceKey].owners.push(ownerKey);
|
|
243
|
+
return (resetPayload) => {
|
|
244
|
+
storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
|
|
245
|
+
if (storeController.instances[instanceKey].owners.length === 0) {
|
|
246
|
+
// TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
|
|
247
|
+
storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps[`$$reset`]);
|
|
248
|
+
delete storeController.instances[instanceKey];
|
|
249
|
+
}
|
|
250
|
+
delete initInstances[instanceKey];
|
|
251
|
+
return STORE_INSTANCE_UNREGISTERED;
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
initInstances[instanceKey] = {
|
|
255
|
+
resetInstance: instanceKey === DEFAULT_INSTANCE_KEY
|
|
256
|
+
? registerInstance(initInstance)
|
|
257
|
+
: registerInstance(initInstance, instanceKey),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
for (const instanceKey in initInstances) {
|
|
262
|
+
if (!actualInstanceKeys.includes(instanceKey)) {
|
|
263
|
+
initInstances[instanceKey].resetInstance();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
268
|
+
actualInstanceKeys);
|
|
269
|
+
React.useEffect(() => {
|
|
270
|
+
const initInstances = initInstancesRef.current.initInstances;
|
|
271
|
+
return () => {
|
|
272
|
+
for (const instanceKey in initInstances) {
|
|
273
|
+
initInstances[instanceKey].resetInstance();
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
}, []);
|
|
277
|
+
}
|
|
278
|
+
return useStore;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function createStoreRegistry() {
|
|
282
|
+
const storeRegistry = {};
|
|
283
|
+
const useDispatcher = createUseDispatcher(storeRegistry);
|
|
284
|
+
const useState = createUseState(storeRegistry);
|
|
285
|
+
const useStateAll = createUseStateAll(storeRegistry);
|
|
286
|
+
const useStore = createUseStore(storeRegistry);
|
|
287
|
+
return {
|
|
288
|
+
useDispatcher,
|
|
289
|
+
useState,
|
|
290
|
+
useStateAll,
|
|
291
|
+
useStore,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function createStore(name, initialState, reducers) {
|
|
296
|
+
const context = React.createContext(null);
|
|
297
|
+
const internalStoreProps = {
|
|
298
|
+
[`$$init`]: reducers[`$$init`],
|
|
299
|
+
[`$$reset`]: reducers[`$$reset`],
|
|
300
|
+
context,
|
|
301
|
+
initialState,
|
|
302
|
+
name,
|
|
303
|
+
uniqKey: Symbol(`Store: ` + name),
|
|
304
|
+
};
|
|
305
|
+
const store = {
|
|
306
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
307
|
+
};
|
|
308
|
+
for (const key in initialState) {
|
|
309
|
+
store[key] = {
|
|
310
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
311
|
+
stateName: key,
|
|
312
|
+
type: STATE_LINK,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
for (const key in reducers) {
|
|
316
|
+
if (!(key in initialState) && ![`$$init`, `$$reset`].includes(key)) {
|
|
317
|
+
store[key] = {
|
|
318
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
319
|
+
reducer: reducers[key],
|
|
320
|
+
reducerName: key,
|
|
321
|
+
type: REDUCER_LINK,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return store;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function StoreProvider(props) {
|
|
329
|
+
const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
|
|
330
|
+
return (jsxRuntime.jsx(Context.Provider, { value: props.instanceKey, children: props.children }));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const { useDispatcher, useState, useStateAll, useStore, } = createStoreRegistry();
|
|
334
|
+
|
|
335
|
+
exports.DESTROY = DESTROY;
|
|
336
|
+
exports.InternalStore = InternalStore;
|
|
337
|
+
exports.StoreProvider = StoreProvider;
|
|
338
|
+
exports.createStore = createStore;
|
|
339
|
+
exports.createStoreRegistry = createStoreRegistry;
|
|
340
|
+
exports.useDispatcher = useDispatcher;
|
|
341
|
+
exports.useState = useState;
|
|
342
|
+
exports.useStateAll = useStateAll;
|
|
343
|
+
exports.useStore = useStore;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const DEFAULT_INSTANCE_KEY: unique symbol;
|
|
2
|
+
export declare const DESTROY: unique symbol;
|
|
3
|
+
export declare const INTERNAL_STORE_PROPS_ACCESSOR: unique symbol;
|
|
4
|
+
export declare const REDUCER_LINK: unique symbol;
|
|
5
|
+
export declare const STATE_LINK: unique symbol;
|
|
6
|
+
export declare const STORE_INSTANCE_UNREGISTERED: unique symbol;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Payload } from './types/index.tsx';
|
|
2
|
+
import { type Reducer } from './types/index.tsx';
|
|
3
|
+
import { type ReducerMap } from './types/index.tsx';
|
|
4
|
+
import { type Store } from './types/index.tsx';
|
|
5
|
+
import { type StoreState } from './types/index.tsx';
|
|
6
|
+
type CheckReducerMap<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
7
|
+
[TKey in keyof TReducerMap]: TKey extends keyof TStoreState ? void : TReducerMap[TKey] extends (state: any, payload: void) => any ? TReducerMap[TKey] : TReducerMap[TKey] extends (state: any, payload: infer TPayload) => any ? TPayload extends Payload ? TReducerMap[TKey] : Reducer<TStoreState> : void;
|
|
8
|
+
};
|
|
9
|
+
export declare function createStore<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>(name: TStoreName, initialState: TStoreState, reducers: CheckReducerMap<TStoreState, TReducerMap>): Store<TStoreName, TStoreState, TReducerMap>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function createStoreRegistry(): {
|
|
2
|
+
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, TReducer extends import("./types/Reducer.tsx").Reducer<TStoreState, any> = import("./types/Reducer.tsx").Reducer<TStoreState, any>>(reducerLink: import("./types/ReducerLink.tsx").ReducerLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TReducerName, TReducer>) => import("./types/Dispatcher.tsx").Dispatcher<TStoreState, TReducer>;
|
|
3
|
+
useState: <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>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: import("./types/StateLink.tsx").StateLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TStateName>, instanceKey?: import("./index.tsx").InstanceKey) => TStoreState[TStateName];
|
|
4
|
+
useStateAll: <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>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: import("./types/StateLink.tsx").StateLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TStateName>, instanceKeys?: import("./index.tsx").InstanceKey[] | ((state: TStoreState) => boolean)) => [instanceKey: import("./index.tsx").InstanceKey, state: TStoreState[TStateName]][];
|
|
5
|
+
useStore: {
|
|
6
|
+
<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>, TInitPayload extends import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap> = import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap>, TResetPayload extends import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap> = import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap>>(store: import("./types/Store.tsx").Store<string, TStoreState, TReducerMap>, registerInstance: import("./types/RegisterDefaultInstance.tsx").RegisterDefaultInstance<TInitPayload, TResetPayload>): void;
|
|
7
|
+
<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>, TInitPayload extends import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap> = import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap>, TResetPayload extends import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap> = import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap>>(store: import("./types/Store.tsx").Store<string, TStoreState, TReducerMap>, instanceKeys: import("./index.tsx").InstanceKey[], registerInstance: import("./types/RegisterInstance.tsx").RegisterInstance<TInitPayload, TResetPayload>): void;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ReducerMap } from './types/index.tsx';
|
|
2
|
+
import { type Dispatcher } from './types/index.tsx';
|
|
3
|
+
import { type StoreState } from './types/index.tsx';
|
|
4
|
+
import { type Reducer } from './types/index.tsx';
|
|
5
|
+
import { type ReducerLink } from './types/index.tsx';
|
|
6
|
+
import { type StoreRegistry } from './types/index.tsx';
|
|
7
|
+
export declare function createUseDispatcher(storeRegistry: StoreRegistry): <TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TReducerName extends string = string, TReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>>(reducerLink: ReducerLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TReducerName, TReducer>) => Dispatcher<TStoreState, TReducer>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createUseSelector(): any;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type InstanceKey } from './types/index.tsx';
|
|
2
|
+
import { type ReducerMap } from './types/index.tsx';
|
|
3
|
+
import { type StateLink } from './types/index.tsx';
|
|
4
|
+
import { type StoreRegistry } from './types/index.tsx';
|
|
5
|
+
import { type StoreState } from './types/index.tsx';
|
|
6
|
+
export declare function createUseState(storeRegistry: StoreRegistry): <TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TStateName>, instanceKey?: InstanceKey) => TStoreState[TStateName];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type InstanceKey } from './types/index.tsx';
|
|
2
|
+
import { type ReducerMap } from './types/index.tsx';
|
|
3
|
+
import { type StateLink } from './types/index.tsx';
|
|
4
|
+
import { type StoreRegistry } from './types/index.tsx';
|
|
5
|
+
import { type StoreState } from './types/index.tsx';
|
|
6
|
+
export declare function createUseStateAll(storeRegistry: StoreRegistry): <TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: StateLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TStateName>, instanceKeys?: InstanceKey[] | ((state: TStoreState) => boolean)) => [instanceKey: InstanceKey, state: TStoreState[TStateName]][];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type InitPayload } from './types/index.tsx';
|
|
2
|
+
import { type InstanceKey } from './types/index.tsx';
|
|
3
|
+
import { type ReducerMap } from './types/index.tsx';
|
|
4
|
+
import { type RegisterDefaultInstance } from './types/index.tsx';
|
|
5
|
+
import { type RegisterInstance } from './types/index.tsx';
|
|
6
|
+
import { type ResetPayload } from './types/index.tsx';
|
|
7
|
+
import { type Store } from './types/index.tsx';
|
|
8
|
+
import { type StoreRegistry } from './types/index.tsx';
|
|
9
|
+
import { type StoreState } from './types/index.tsx';
|
|
10
|
+
export declare function createUseStore(storeRegistry: StoreRegistry): {
|
|
11
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TInitPayload extends InitPayload<TStoreState, TReducerMap> = InitPayload<TStoreState, TReducerMap>, TResetPayload extends ResetPayload<TStoreState, TReducerMap> = ResetPayload<TStoreState, TReducerMap>>(store: Store<string, TStoreState, TReducerMap>, registerInstance: RegisterDefaultInstance<TInitPayload, TResetPayload>): void;
|
|
12
|
+
<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>, TInitPayload extends InitPayload<TStoreState, TReducerMap> = InitPayload<TStoreState, TReducerMap>, TResetPayload extends ResetPayload<TStoreState, TReducerMap> = ResetPayload<TStoreState, TReducerMap>>(store: Store<string, TStoreState, TReducerMap>, instanceKeys: InstanceKey[], registerInstance: RegisterInstance<TInitPayload, TResetPayload>): void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import React, { useContext, useMemo, useCallback, useRef, useEffect } from 'react';
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_INSTANCE_KEY = Symbol(`DEFAULT_INSTANCE_KEY`);
|
|
6
|
+
const DESTROY = Symbol(`DESTROY_INSTRUCTION`);
|
|
7
|
+
const INTERNAL_STORE_PROPS_ACCESSOR = Symbol(`INTERNAL_STORE_PROPS`);
|
|
8
|
+
const REDUCER_LINK = Symbol(`REDUCER_LINK`);
|
|
9
|
+
const STATE_LINK = Symbol(`STATE_LINK`);
|
|
10
|
+
const STORE_INSTANCE_UNREGISTERED = Symbol(`STORE_INSTANCE_UNREGISTERED_INSTRUCTION`);
|
|
11
|
+
|
|
12
|
+
function createUseDispatcher(storeRegistry) {
|
|
13
|
+
return function useDispatcher(reducerLink) {
|
|
14
|
+
const contextInstanceKey = useContext(reducerLink[INTERNAL_STORE_PROPS_ACCESSOR].context);
|
|
15
|
+
return useMemo(() => {
|
|
16
|
+
const internalStoreProps = reducerLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
17
|
+
function dispatch(...args) {
|
|
18
|
+
let instanceKey;
|
|
19
|
+
let payload;
|
|
20
|
+
if ([`number`, `symbol`, `string`].includes(typeof args[0])) {
|
|
21
|
+
instanceKey = args[0];
|
|
22
|
+
payload = args[1];
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
payload = args[0];
|
|
26
|
+
}
|
|
27
|
+
const actualInstanceKey = instanceKey ?? contextInstanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
28
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
29
|
+
if (!(internalStoreProps.uniqKey in storeRegistry)) {
|
|
30
|
+
throw new Error(`Store "${internalStoreProps.name}" is not registered.`);
|
|
31
|
+
}
|
|
32
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
33
|
+
if (!(storeRegistry[internalStoreProps.uniqKey].instances[actualInstanceKey])) {
|
|
34
|
+
throw new Error(`Store "${internalStoreProps.name}" is not initialized for instance "${String(actualInstanceKey)}".`);
|
|
35
|
+
}
|
|
36
|
+
const internalStore = storeRegistry[internalStoreProps.uniqKey].internalStore;
|
|
37
|
+
internalStore.dispatch(actualInstanceKey, payload, reducerLink.reducer);
|
|
38
|
+
}
|
|
39
|
+
return dispatch;
|
|
40
|
+
}, [contextInstanceKey, reducerLink]);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function createUseState(storeRegistry) {
|
|
45
|
+
return function useState(stateLink, instanceKey) {
|
|
46
|
+
const internalStoreProps = stateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
47
|
+
const contextInstanceKey = useContext(internalStoreProps.context);
|
|
48
|
+
const actualInstanceKey = instanceKey ?? contextInstanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
49
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
50
|
+
if (!(internalStoreProps.uniqKey in storeRegistry)) {
|
|
51
|
+
throw new Error(`Store "${internalStoreProps.name}" is not registered.`);
|
|
52
|
+
}
|
|
53
|
+
// TODO Вынести в отдельную функцию, т.к. уже встречается в проекте пару раз
|
|
54
|
+
if (!(storeRegistry[internalStoreProps.uniqKey].instances[actualInstanceKey])) {
|
|
55
|
+
throw new Error(`Store "${internalStoreProps.name}" is not initialized for instance "${String(actualInstanceKey)}".`);
|
|
56
|
+
}
|
|
57
|
+
const internalStore = storeRegistry[internalStoreProps.uniqKey].internalStore;
|
|
58
|
+
const subscribe = useCallback((listener) => {
|
|
59
|
+
return internalStore.subscribe(actualInstanceKey, listener);
|
|
60
|
+
}, [actualInstanceKey, internalStore]);
|
|
61
|
+
const getSnapshot = useCallback(() => {
|
|
62
|
+
return internalStore.getSnapshot(actualInstanceKey);
|
|
63
|
+
}, [actualInstanceKey, internalStore]);
|
|
64
|
+
const selector = useCallback((state) => {
|
|
65
|
+
return state[stateLink.stateName];
|
|
66
|
+
}, [stateLink]);
|
|
67
|
+
return useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, selector);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function createUseStateAll(storeRegistry) {
|
|
72
|
+
return function useStateAll(stateLink, instanceKeys) {
|
|
73
|
+
const internalStoreProps = stateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
74
|
+
const prevStateAllRef = useRef({ prevStateAll: [] });
|
|
75
|
+
if (!(internalStoreProps.uniqKey in storeRegistry)) {
|
|
76
|
+
throw new Error(`Store "${internalStoreProps.name}" is not registered.`);
|
|
77
|
+
}
|
|
78
|
+
const internalStore = storeRegistry[internalStoreProps.uniqKey].internalStore;
|
|
79
|
+
const subscribe = useCallback((listener) => {
|
|
80
|
+
return internalStore.subscribeAll(listener);
|
|
81
|
+
}, [internalStore]);
|
|
82
|
+
const getSnapshot = useCallback(() => {
|
|
83
|
+
return internalStore.getSnapshotAll();
|
|
84
|
+
}, [internalStore]);
|
|
85
|
+
const selector = useCallback((state) => {
|
|
86
|
+
let stateAll;
|
|
87
|
+
if (instanceKeys == null) {
|
|
88
|
+
stateAll = Object.entries(state)
|
|
89
|
+
.map(([instanceKey, state]) => {
|
|
90
|
+
return [instanceKey, state[stateLink.stateName]];
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else if (typeof instanceKeys === `function`) {
|
|
94
|
+
stateAll = Object.entries(state)
|
|
95
|
+
.filter(([_instanceKey, state]) => {
|
|
96
|
+
return instanceKeys(state);
|
|
97
|
+
})
|
|
98
|
+
.map(([instanceKey, state]) => {
|
|
99
|
+
return [instanceKey, state[stateLink.stateName]];
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
stateAll = instanceKeys.map((instanceKey) => {
|
|
104
|
+
return [instanceKey, state[instanceKey][stateLink.stateName]];
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (isEqual(prevStateAllRef.current.prevStateAll, stateAll)) {
|
|
108
|
+
prevStateAllRef.current.prevStateAll = stateAll;
|
|
109
|
+
return stateAll;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
return prevStateAllRef.current.prevStateAll;
|
|
113
|
+
}
|
|
114
|
+
}, [instanceKeys, stateLink]);
|
|
115
|
+
return useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, selector);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function isEqual(prevStateAll, stateAll) {
|
|
119
|
+
if (prevStateAll.length !== stateAll.length) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return prevStateAll.every(([prevInstanceKey, prevState]) => {
|
|
124
|
+
const stateAllEntry = stateAll.find(([instanceKey]) => {
|
|
125
|
+
return prevInstanceKey === instanceKey;
|
|
126
|
+
});
|
|
127
|
+
return stateAllEntry && stateAllEntry[1] === prevState;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
class InternalStore {
|
|
133
|
+
constructor(name) {
|
|
134
|
+
this.getStateAll = () => {
|
|
135
|
+
return this.state;
|
|
136
|
+
};
|
|
137
|
+
this.getState = (instanceKey) => {
|
|
138
|
+
return this.state[instanceKey];
|
|
139
|
+
};
|
|
140
|
+
this.initInstance = (instanceKey, initialState, initPayload, initReducer) => {
|
|
141
|
+
const initState = initReducer(initialState, initPayload);
|
|
142
|
+
this.state = {
|
|
143
|
+
...this.state,
|
|
144
|
+
[instanceKey]: initState,
|
|
145
|
+
};
|
|
146
|
+
// TODO Убрать в логгер
|
|
147
|
+
console.log(`initInstance [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
148
|
+
console.dir(this.state);
|
|
149
|
+
return (resetPayload, resetReducer) => {
|
|
150
|
+
const resetState = resetReducer(initState, resetPayload);
|
|
151
|
+
// TODO Тут место для уничтожения состояния экземпляра хранилища через `delete this.state[instanceKey];`
|
|
152
|
+
this.state = { ...this.state, [instanceKey]: resetState };
|
|
153
|
+
// TODO Убрать в логгер
|
|
154
|
+
console.log(`resetInstance [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
155
|
+
console.dir(this.state);
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
this.subscribe = (instanceKey, listener) => {
|
|
159
|
+
if (!this.listeners[instanceKey]) {
|
|
160
|
+
this.listeners[instanceKey] = new Set();
|
|
161
|
+
}
|
|
162
|
+
this.listeners[instanceKey].add(listener);
|
|
163
|
+
return () => {
|
|
164
|
+
this.listeners[instanceKey].delete(listener);
|
|
165
|
+
if (this.listeners[instanceKey].size === 0) {
|
|
166
|
+
delete this.listeners[instanceKey];
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
this.subscribeAll = (listener) => {
|
|
171
|
+
this.listenersAll.add(listener);
|
|
172
|
+
return () => {
|
|
173
|
+
this.listenersAll.delete(listener);
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
this.getSnapshot = (instanceKey) => {
|
|
177
|
+
return this.state[instanceKey];
|
|
178
|
+
};
|
|
179
|
+
this.getSnapshotAll = () => {
|
|
180
|
+
return this.state;
|
|
181
|
+
};
|
|
182
|
+
this.dispatch = (instanceKey, payload, reducer) => {
|
|
183
|
+
const changes = reducer(this.state[instanceKey], payload);
|
|
184
|
+
this.update(instanceKey, changes);
|
|
185
|
+
};
|
|
186
|
+
this.update = (instanceKey, changes) => {
|
|
187
|
+
if (this.state[instanceKey] !== changes) {
|
|
188
|
+
this.state = { ...this.state, [instanceKey]: changes };
|
|
189
|
+
this.listeners[instanceKey].forEach((listener) => listener());
|
|
190
|
+
this.listenersAll.forEach((listener) => listener());
|
|
191
|
+
// TODO Убрать в логгер
|
|
192
|
+
console.log(`update [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
193
|
+
console.dir(this.state);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
this.listeners = {};
|
|
197
|
+
this.listenersAll = new Set();
|
|
198
|
+
this.name = name;
|
|
199
|
+
this.state = {};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function createUseStore(storeRegistry) {
|
|
204
|
+
function useStore(store, ...args) {
|
|
205
|
+
let instanceKeys;
|
|
206
|
+
let registerInstance;
|
|
207
|
+
if (typeof args[0] === `function`) {
|
|
208
|
+
instanceKeys = [];
|
|
209
|
+
registerInstance = args[0];
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
instanceKeys = args[0];
|
|
213
|
+
registerInstance = args[1];
|
|
214
|
+
}
|
|
215
|
+
const actualInstanceKeys = instanceKeys.length ? instanceKeys : [DEFAULT_INSTANCE_KEY];
|
|
216
|
+
const storeRef = useRef({ store });
|
|
217
|
+
const ownerKeyRef = useRef({ ownerKey: Symbol(`ownerKey`) });
|
|
218
|
+
const initInstancesRef = useRef({ initInstances: {} });
|
|
219
|
+
useMemo(() => {
|
|
220
|
+
const initInstances = initInstancesRef.current.initInstances;
|
|
221
|
+
const store = storeRef.current.store;
|
|
222
|
+
for (const instanceKey of actualInstanceKeys) {
|
|
223
|
+
if (!initInstances[instanceKey]) {
|
|
224
|
+
const ownerKey = ownerKeyRef.current.ownerKey;
|
|
225
|
+
const initInstance = (initPayload) => {
|
|
226
|
+
const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
227
|
+
let storeController = storeRegistry[internalStoreProps.uniqKey];
|
|
228
|
+
if (!storeController) {
|
|
229
|
+
const internalStore = new InternalStore(internalStoreProps.name);
|
|
230
|
+
storeController = { internalStore, instances: {} };
|
|
231
|
+
storeRegistry[internalStoreProps.uniqKey] = storeController;
|
|
232
|
+
// TODO Убрать в логгер
|
|
233
|
+
console.log(`registerStore [store: ${storeController.internalStore.name}]:`);
|
|
234
|
+
console.dir(storeRegistry);
|
|
235
|
+
}
|
|
236
|
+
if (!storeController.instances[instanceKey]) {
|
|
237
|
+
const resetState = storeController.internalStore.initInstance(instanceKey, internalStoreProps.initialState, initPayload, internalStoreProps[`$$init`]);
|
|
238
|
+
storeController.instances[instanceKey] = { owners: [], resetState };
|
|
239
|
+
}
|
|
240
|
+
storeController.instances[instanceKey].owners.push(ownerKey);
|
|
241
|
+
return (resetPayload) => {
|
|
242
|
+
storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
|
|
243
|
+
if (storeController.instances[instanceKey].owners.length === 0) {
|
|
244
|
+
// TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
|
|
245
|
+
storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps[`$$reset`]);
|
|
246
|
+
delete storeController.instances[instanceKey];
|
|
247
|
+
}
|
|
248
|
+
delete initInstances[instanceKey];
|
|
249
|
+
return STORE_INSTANCE_UNREGISTERED;
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
initInstances[instanceKey] = {
|
|
253
|
+
resetInstance: instanceKey === DEFAULT_INSTANCE_KEY
|
|
254
|
+
? registerInstance(initInstance)
|
|
255
|
+
: registerInstance(initInstance, instanceKey),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const instanceKey in initInstances) {
|
|
260
|
+
if (!actualInstanceKeys.includes(instanceKey)) {
|
|
261
|
+
initInstances[instanceKey].resetInstance();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
266
|
+
actualInstanceKeys);
|
|
267
|
+
useEffect(() => {
|
|
268
|
+
const initInstances = initInstancesRef.current.initInstances;
|
|
269
|
+
return () => {
|
|
270
|
+
for (const instanceKey in initInstances) {
|
|
271
|
+
initInstances[instanceKey].resetInstance();
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}, []);
|
|
275
|
+
}
|
|
276
|
+
return useStore;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function createStoreRegistry() {
|
|
280
|
+
const storeRegistry = {};
|
|
281
|
+
const useDispatcher = createUseDispatcher(storeRegistry);
|
|
282
|
+
const useState = createUseState(storeRegistry);
|
|
283
|
+
const useStateAll = createUseStateAll(storeRegistry);
|
|
284
|
+
const useStore = createUseStore(storeRegistry);
|
|
285
|
+
return {
|
|
286
|
+
useDispatcher,
|
|
287
|
+
useState,
|
|
288
|
+
useStateAll,
|
|
289
|
+
useStore,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function createStore(name, initialState, reducers) {
|
|
294
|
+
const context = React.createContext(null);
|
|
295
|
+
const internalStoreProps = {
|
|
296
|
+
[`$$init`]: reducers[`$$init`],
|
|
297
|
+
[`$$reset`]: reducers[`$$reset`],
|
|
298
|
+
context,
|
|
299
|
+
initialState,
|
|
300
|
+
name,
|
|
301
|
+
uniqKey: Symbol(`Store: ` + name),
|
|
302
|
+
};
|
|
303
|
+
const store = {
|
|
304
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
305
|
+
};
|
|
306
|
+
for (const key in initialState) {
|
|
307
|
+
store[key] = {
|
|
308
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
309
|
+
stateName: key,
|
|
310
|
+
type: STATE_LINK,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
for (const key in reducers) {
|
|
314
|
+
if (!(key in initialState) && ![`$$init`, `$$reset`].includes(key)) {
|
|
315
|
+
store[key] = {
|
|
316
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
317
|
+
reducer: reducers[key],
|
|
318
|
+
reducerName: key,
|
|
319
|
+
type: REDUCER_LINK,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return store;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function StoreProvider(props) {
|
|
327
|
+
const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
|
|
328
|
+
return (jsx(Context.Provider, { value: props.instanceKey, children: props.children }));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const { useDispatcher, useState, useStateAll, useStore, } = createStoreRegistry();
|
|
332
|
+
|
|
333
|
+
export { DESTROY, InternalStore, StoreProvider, createStore, createStoreRegistry, useDispatcher, useState, useStateAll, useStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { type InstanceKey } from './types/index.tsx';
|
|
2
|
+
export { DESTROY } from './constants.tsx';
|
|
3
|
+
export { createStore } from './createStore.tsx';
|
|
4
|
+
export { createStoreRegistry } from './createStoreRegistry.tsx';
|
|
5
|
+
export { InternalStore } from './InternalStore.tsx';
|
|
6
|
+
export { StoreProvider } from './StoreProvider.tsx';
|
|
7
|
+
export declare const 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, TReducer extends import("./types/Reducer.tsx").Reducer<TStoreState, any> = import("./types/Reducer.tsx").Reducer<TStoreState, any>>(reducerLink: import("./types/ReducerLink.tsx").ReducerLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TReducerName, TReducer>) => import("./types/Dispatcher.tsx").Dispatcher<TStoreState, TReducer>, useState: <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>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: import("./types/StateLink.tsx").StateLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TStateName>, instanceKey?: import("./index.tsx").InstanceKey) => TStoreState[TStateName], useStateAll: <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>, TStateName extends keyof TStoreState = keyof TStoreState>(stateLink: import("./types/StateLink.tsx").StateLink<string, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TStateName>, instanceKeys?: import("./index.tsx").InstanceKey[] | ((state: TStoreState) => boolean)) => [instanceKey: import("./index.tsx").InstanceKey, state: TStoreState[TStateName]][], useStore: {
|
|
8
|
+
<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>, TInitPayload extends import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap> = import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap>, TResetPayload extends import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap> = import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap>>(store: import("./types/Store.tsx").Store<string, TStoreState, TReducerMap>, registerInstance: import("./types/RegisterDefaultInstance.tsx").RegisterDefaultInstance<TInitPayload, TResetPayload>): void;
|
|
9
|
+
<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>, TInitPayload extends import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap> = import("./types/InitPayload.tsx").InitPayload<TStoreState, TReducerMap>, TResetPayload extends import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap> = import("./types/ResetPayload.tsx").ResetPayload<TStoreState, TReducerMap>>(store: import("./types/Store.tsx").Store<string, TStoreState, TReducerMap>, instanceKeys: import("./index.tsx").InstanceKey[], registerInstance: import("./types/RegisterInstance.tsx").RegisterInstance<TInitPayload, TResetPayload>): void;
|
|
10
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type InstanceKey } from './';
|
|
2
|
+
import { type Reducer } from './';
|
|
3
|
+
import { type ReducerPayload } from './';
|
|
4
|
+
import { type StoreState } from './';
|
|
5
|
+
export type Dispatcher<TStoreState extends StoreState = StoreState, TReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>> = {
|
|
6
|
+
(payload: ReducerPayload<TStoreState, TReducer>): void;
|
|
7
|
+
(instanceKey: InstanceKey, payload: ReducerPayload<TStoreState, TReducer>): void;
|
|
8
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Payload } from './';
|
|
2
|
+
import { type ResetInstance } from './';
|
|
3
|
+
export type InitInstance<TInitPayload extends void | Payload = void | Payload, TResetPayload extends void | Payload = void | Payload> = {
|
|
4
|
+
(payload: TInitPayload): ResetInstance<TResetPayload>;
|
|
5
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ReducerMap } from './';
|
|
2
|
+
import { type ReducerPayload } from './';
|
|
3
|
+
import { type StoreState } from './';
|
|
4
|
+
export type InitPayload<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = ReducerPayload<TStoreState, TReducerMap[`$$init`]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type InstanceKey = number | string | symbol;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type InstanceKey } from './';
|
|
3
|
+
import { type Reducer } from './';
|
|
4
|
+
import { type StoreState } from './';
|
|
5
|
+
export type InternalStoreProps<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TInitReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>, TResetReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>> = {
|
|
6
|
+
[`$$init`]: TInitReducer;
|
|
7
|
+
[`$$reset`]: TResetReducer;
|
|
8
|
+
context: React.Context<null | InstanceKey>;
|
|
9
|
+
initialState: TStoreState;
|
|
10
|
+
name: TStoreName;
|
|
11
|
+
uniqKey: symbol;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Payload = Record<string, any>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
2
|
+
import { REDUCER_LINK } from '../constants.tsx';
|
|
3
|
+
import { type InternalStoreProps } from './';
|
|
4
|
+
import { type Reducer } from './';
|
|
5
|
+
import { type StoreState } from './';
|
|
6
|
+
export type ReducerLink<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TInitReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>, TResetReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>, TReducerName extends string = string, TReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>> = {
|
|
7
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TInitReducer, TResetReducer>;
|
|
8
|
+
reducer: TReducer;
|
|
9
|
+
reducerName: TReducerName;
|
|
10
|
+
type: typeof REDUCER_LINK;
|
|
11
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Reducer } from './';
|
|
2
|
+
import { type StoreState } from './';
|
|
3
|
+
export type ReducerMap<TStoreState extends StoreState = StoreState> = Record<string, Reducer<TStoreState, void | any>> & {
|
|
4
|
+
[`$$init`]: Reducer<TStoreState, void | any>;
|
|
5
|
+
[`$$reset`]: Reducer<TStoreState, void | any>;
|
|
6
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type Payload } from './';
|
|
2
|
+
import { type Reducer } from './';
|
|
3
|
+
import { type StoreState } from './';
|
|
4
|
+
export type ReducerPayload<TStoreState extends StoreState = StoreState, TReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>> = Parameters<TReducer>[1] extends Payload ? Parameters<TReducer>[1] : void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { STORE_INSTANCE_UNREGISTERED } from '../constants.tsx';
|
|
2
|
+
import { type InitInstance } from './';
|
|
3
|
+
import { type Payload } from './';
|
|
4
|
+
export type RegisterDefaultInstance<TInitPayload extends void | Payload = void | Payload, TResetPayload extends void | Payload = void | Payload> = {
|
|
5
|
+
(initInstance: InitInstance<TInitPayload, TResetPayload>, instanceKey: void): () => typeof STORE_INSTANCE_UNREGISTERED;
|
|
6
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { STORE_INSTANCE_UNREGISTERED } from '../constants.tsx';
|
|
2
|
+
import { type InitInstance } from './';
|
|
3
|
+
import { type InstanceKey } from './';
|
|
4
|
+
import { type Payload } from './';
|
|
5
|
+
export type RegisterInstance<TInitPayload extends void | Payload = void | Payload, TResetPayload extends void | Payload = void | Payload> = {
|
|
6
|
+
(initInstance: InitInstance<TInitPayload, TResetPayload>, instanceKey: InstanceKey): () => typeof STORE_INSTANCE_UNREGISTERED;
|
|
7
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ReducerMap } from './';
|
|
2
|
+
import { type ReducerPayload } from './';
|
|
3
|
+
import { type StoreState } from './';
|
|
4
|
+
export type ResetPayload<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = ReducerPayload<TStoreState, TReducerMap[`$$reset`]>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
2
|
+
import { STATE_LINK } from '../constants.tsx';
|
|
3
|
+
import { type InternalStoreProps } from './';
|
|
4
|
+
import { type Reducer } from './';
|
|
5
|
+
import { type StoreState } from './';
|
|
6
|
+
export type StateLink<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TInitReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>, TResetReducer extends Reducer<TStoreState, any> = Reducer<TStoreState, any>, TStateName extends keyof TStoreState = keyof TStoreState> = {
|
|
7
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TInitReducer, TResetReducer>;
|
|
8
|
+
stateName: TStateName;
|
|
9
|
+
type: typeof STATE_LINK;
|
|
10
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { INTERNAL_STORE_PROPS_ACCESSOR } from '../constants.tsx';
|
|
2
|
+
import { type InternalStoreProps } from './';
|
|
3
|
+
import { type ReducerMap } from './';
|
|
4
|
+
import { type ReducerLink } from './';
|
|
5
|
+
import { type StateLink } from './';
|
|
6
|
+
import { type StoreState } from './';
|
|
7
|
+
export type Store<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
8
|
+
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`]>;
|
|
9
|
+
} & {
|
|
10
|
+
[`$$init`]: void;
|
|
11
|
+
[`$$reset`]: void;
|
|
12
|
+
} & {
|
|
13
|
+
[TKey in Extract<keyof TStoreState, string>]: StateLink<TStoreName, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TKey>;
|
|
14
|
+
} & {
|
|
15
|
+
[TKey in Extract<keyof TReducerMap, string>]: TKey extends keyof TStoreState ? never : ReducerLink<TStoreName, TStoreState, TReducerMap[`$$init`], TReducerMap[`$$reset`], TKey, TReducerMap[TKey]>;
|
|
16
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InternalStore } from '../InternalStore.tsx';
|
|
2
|
+
import { type InstanceKey } from './';
|
|
3
|
+
import { type ReducerMap } from './';
|
|
4
|
+
import { type StoreControllerInstance } from './StoreControllerInstance.tsx';
|
|
5
|
+
import { type StoreState } from './';
|
|
6
|
+
export type StoreController<TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
7
|
+
internalStore: InternalStore<TStoreState, Parameters<TReducerMap[`$$init`]>[1], Parameters<TReducerMap[`$$reset`]>[1]>;
|
|
8
|
+
instances: Record<InstanceKey, StoreControllerInstance<TStoreState, Parameters<TReducerMap[`$$reset`]>[1]>>;
|
|
9
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { StoreState } from './';
|
|
2
|
+
import { type Payload } from './';
|
|
3
|
+
import { type Reducer } from './';
|
|
4
|
+
export type StoreControllerInstance<TStoreState extends StoreState = StoreState, TResetPayload extends void | Payload = void | Payload> = {
|
|
5
|
+
owners: symbol[];
|
|
6
|
+
resetState: (resetPayload: TResetPayload, resetReducer: Reducer<TStoreState, TResetPayload>) => void;
|
|
7
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type StoreState = Record<string, any>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export * from './Dispatcher.tsx';
|
|
2
|
+
export * from './InitInstance.tsx';
|
|
3
|
+
export * from './InstanceKey.tsx';
|
|
4
|
+
export * from './InitPayload.tsx';
|
|
5
|
+
export * from './InternalStoreProps.tsx';
|
|
6
|
+
export * from './Payload.tsx';
|
|
7
|
+
export * from './Reducer.tsx';
|
|
8
|
+
export * from './ReducerLink.tsx';
|
|
9
|
+
export * from './ReducerMap.tsx';
|
|
10
|
+
export * from './ReducerPayload.tsx';
|
|
11
|
+
export * from './RegisterDefaultInstance.tsx';
|
|
12
|
+
export * from './RegisterInstance.tsx';
|
|
13
|
+
export * from './ResetInstance.tsx';
|
|
14
|
+
export * from './ResetPayload.tsx';
|
|
15
|
+
export * from './StateLink.tsx';
|
|
16
|
+
export * from './Store.tsx';
|
|
17
|
+
export * from './StoreController.tsx';
|
|
18
|
+
export * from './StoreControllerInstance.tsx';
|
|
19
|
+
export * from './StoreRegistry.tsx';
|
|
20
|
+
export * from './StoreState.tsx';
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"use-sync-external-store": "^1.5.0"
|
|
4
|
+
},
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"README.md",
|
|
8
|
+
"LICENSE"
|
|
9
|
+
],
|
|
10
|
+
"main": "dist/cjs/index.cjs.js",
|
|
11
|
+
"module": "dist/esm/index.esm.js",
|
|
12
|
+
"name": "beestate",
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"react": ">=18.0.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "npm run clear && rollup --config ./rollup.config.js",
|
|
18
|
+
"clear": "rm -rf dist_lib",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"types": "dist/@types",
|
|
22
|
+
"version": "0.1.0"
|
|
23
|
+
}
|