beestate 0.1.11 → 0.1.13
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/createStoreRegistry.d.ts +7 -5
- package/dist/@types/createUsePropSelector.d.ts +3 -0
- package/dist/@types/createUsePropSelectorAll.d.ts +3 -0
- package/dist/@types/index.d.ts +5 -1
- 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 +5 -0
- 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 +12 -0
- package/dist/@types/types/UsePropSelectorAll.d.ts +9 -0
- package/dist/@types/unlock.d.ts +1 -0
- package/dist/cjs/index.cjs.js +215 -63
- package/dist/esm/index.esm.js +212 -64
- package/package.json +1 -1
- package/dist/@types/createUseSelector.d.ts +0 -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
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]);
|
|
@@ -261,6 +325,26 @@ function createUseProp(storeRegistry) {
|
|
|
261
325
|
};
|
|
262
326
|
}
|
|
263
327
|
|
|
328
|
+
function pickPropAll(stateAll, stateName, instanceKeys) {
|
|
329
|
+
let actualInstanceKeys;
|
|
330
|
+
if (instanceKeys == null) {
|
|
331
|
+
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)];
|
|
332
|
+
}
|
|
333
|
+
else if (typeof instanceKeys === `function`) {
|
|
334
|
+
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)].filter((instanceKey) => {
|
|
335
|
+
return instanceKeys(stateAll[instanceKey]);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
actualInstanceKeys = instanceKeys.filter((instanceKey) => {
|
|
340
|
+
return instanceKey in stateAll;
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return actualInstanceKeys.map((instanceKey) => {
|
|
344
|
+
return [instanceKey, stateAll[instanceKey][stateName]];
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
264
348
|
function createUsePropAll(storeRegistry) {
|
|
265
349
|
return function usePropAll(stateLink, instanceKeys) {
|
|
266
350
|
const stateLinkRef = useRef({ stateLink });
|
|
@@ -277,23 +361,7 @@ function createUsePropAll(storeRegistry) {
|
|
|
277
361
|
return internalStore.getStateAll();
|
|
278
362
|
}, [internalStore]);
|
|
279
363
|
const selector = useCallback((stateAll) => {
|
|
280
|
-
|
|
281
|
-
if (instanceKeys == null) {
|
|
282
|
-
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)];
|
|
283
|
-
}
|
|
284
|
-
else if (typeof instanceKeys === `function`) {
|
|
285
|
-
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)].filter((instanceKey) => {
|
|
286
|
-
return instanceKeys(stateAll[instanceKey]);
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
actualInstanceKeys = instanceKeys.filter((instanceKey) => {
|
|
291
|
-
return instanceKey in stateAll;
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
return actualInstanceKeys.map((instanceKey) => {
|
|
295
|
-
return [instanceKey, stateAll[instanceKey][actualStateLink.stateName]];
|
|
296
|
-
});
|
|
364
|
+
return pickPropAll(stateAll, actualStateLink.stateName, instanceKeys);
|
|
297
365
|
}, [actualStateLink, instanceKeys]);
|
|
298
366
|
return useSyncExternalStoreWithSelector(subscribe, getSnapshot, null, selector, isEqual);
|
|
299
367
|
};
|
|
@@ -309,6 +377,52 @@ function isEqual(prevStateAll, stateAll) {
|
|
|
309
377
|
}
|
|
310
378
|
}
|
|
311
379
|
|
|
380
|
+
function createUsePropSelector(storeRegistry) {
|
|
381
|
+
return function usePropSelector(stateLink, instanceKey) {
|
|
382
|
+
const stateLinkRef = useRef({ stateLink });
|
|
383
|
+
if (stateLinkRef.current.stateLink !== stateLink) {
|
|
384
|
+
console.warn(`State link reference mismatch. You must pass the same state link on every render.`);
|
|
385
|
+
}
|
|
386
|
+
const actualStateLink = stateLinkRef.current.stateLink;
|
|
387
|
+
const internalStoreProps = actualStateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
388
|
+
const storeContextValue = useContext(internalStoreProps.context);
|
|
389
|
+
const actualInstanceKey = instanceKey ?? storeContextValue?.instanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
390
|
+
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
391
|
+
const uninitializedState = storeContextValue?.instanceKey === actualInstanceKey
|
|
392
|
+
? storeContextValue.uninitializedState
|
|
393
|
+
: internalStoreProps.unlockedInitialState;
|
|
394
|
+
return useCallback(() => {
|
|
395
|
+
if (actualStateLink.stateName === INITIALIZED_STATE_NAME) {
|
|
396
|
+
return internalStore.isInitialized(actualInstanceKey);
|
|
397
|
+
}
|
|
398
|
+
else if (actualStateLink.stateName === INSTANCE_KEY_STATE_NAME) {
|
|
399
|
+
return actualInstanceKey;
|
|
400
|
+
}
|
|
401
|
+
else if (internalStore.isInitialized(actualInstanceKey)) {
|
|
402
|
+
return internalStore.getState(actualInstanceKey)[actualStateLink.stateName];
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
return uninitializedState[actualStateLink.stateName];
|
|
406
|
+
}
|
|
407
|
+
}, [actualInstanceKey, actualStateLink, internalStore, uninitializedState]);
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function createUsePropSelectorAll(storeRegistry) {
|
|
412
|
+
return function usePropSelectorAll(stateLink, instanceKeys) {
|
|
413
|
+
const stateLinkRef = useRef({ stateLink });
|
|
414
|
+
if (stateLinkRef.current.stateLink !== stateLink) {
|
|
415
|
+
console.warn(`State link reference mismatch. You must pass the same state link on every render.`);
|
|
416
|
+
}
|
|
417
|
+
const actualStateLink = stateLinkRef.current.stateLink;
|
|
418
|
+
const internalStoreProps = actualStateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
419
|
+
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
420
|
+
return useCallback(() => {
|
|
421
|
+
return pickPropAll(internalStore.getStateAll(), actualStateLink.stateName, instanceKeys);
|
|
422
|
+
}, [actualStateLink, instanceKeys, internalStore]);
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
312
426
|
function createUseStore(storeRegistry) {
|
|
313
427
|
function useStore(store, ...args) {
|
|
314
428
|
const storeRef = useRef({ store });
|
|
@@ -346,7 +460,7 @@ function createUseStore(storeRegistry) {
|
|
|
346
460
|
console.log(`Store instance initialization [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
347
461
|
// TODO Убрать в логгер
|
|
348
462
|
console.log({ storeRegistry });
|
|
349
|
-
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.
|
|
463
|
+
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.unlockedInitialState, prefill);
|
|
350
464
|
}
|
|
351
465
|
else {
|
|
352
466
|
console.warn(`Store instance is already initialized, initialization skipped [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
@@ -393,29 +507,55 @@ function createStoreRegistry() {
|
|
|
393
507
|
const useStore = createUseStore(storeRegistry);
|
|
394
508
|
const useProp = createUseProp(storeRegistry);
|
|
395
509
|
const usePropAll = createUsePropAll(storeRegistry);
|
|
510
|
+
const usePropSelector = createUsePropSelector(storeRegistry);
|
|
511
|
+
const usePropSelectorAll = createUsePropSelectorAll(storeRegistry);
|
|
396
512
|
const useDispatcher = createUseDispatcher(storeRegistry);
|
|
397
513
|
const useLatest = createUseLatest();
|
|
398
514
|
const StoreProvider = createStoreProvider(useStore);
|
|
399
515
|
return {
|
|
400
|
-
storeRegistry,
|
|
401
516
|
StoreProvider,
|
|
402
|
-
|
|
403
|
-
useProp,
|
|
404
|
-
usePropAll,
|
|
517
|
+
storeRegistry,
|
|
405
518
|
useDispatcher,
|
|
406
519
|
useLatest,
|
|
520
|
+
useProp,
|
|
521
|
+
usePropAll,
|
|
522
|
+
usePropSelector,
|
|
523
|
+
usePropSelectorAll,
|
|
524
|
+
useStore,
|
|
407
525
|
};
|
|
408
526
|
}
|
|
409
527
|
|
|
410
528
|
function createStore(name, initialState, reducerMap) {
|
|
411
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
|
+
}
|
|
412
550
|
const internalStoreProps = {
|
|
413
551
|
$$init: reducerMap.$$init,
|
|
414
552
|
$$reset: reducerMap.$$reset,
|
|
415
553
|
context,
|
|
416
554
|
initialState,
|
|
555
|
+
lockedStateNames,
|
|
417
556
|
name,
|
|
418
557
|
uniqKey: Symbol(`Store: ` + name),
|
|
558
|
+
unlockedInitialState,
|
|
419
559
|
};
|
|
420
560
|
const store = {
|
|
421
561
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
@@ -424,18 +564,18 @@ function createStore(name, initialState, reducerMap) {
|
|
|
424
564
|
store[key] = {
|
|
425
565
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
426
566
|
stateName: key,
|
|
427
|
-
type:
|
|
567
|
+
type: STATE_LINK_FLAG,
|
|
428
568
|
};
|
|
429
569
|
}
|
|
430
570
|
store[INITIALIZED_STATE_NAME] = {
|
|
431
571
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
432
572
|
stateName: INITIALIZED_STATE_NAME,
|
|
433
|
-
type:
|
|
573
|
+
type: STATE_LINK_FLAG,
|
|
434
574
|
};
|
|
435
575
|
store[INSTANCE_KEY_STATE_NAME] = {
|
|
436
576
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
437
577
|
stateName: INSTANCE_KEY_STATE_NAME,
|
|
438
|
-
type:
|
|
578
|
+
type: STATE_LINK_FLAG,
|
|
439
579
|
};
|
|
440
580
|
for (const key in reducerMap) {
|
|
441
581
|
if (!(key in initialState) && ![`$$init`, `$$reset`].includes(key)) {
|
|
@@ -443,13 +583,21 @@ function createStore(name, initialState, reducerMap) {
|
|
|
443
583
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
444
584
|
reducer: reducerMap[key],
|
|
445
585
|
reducerName: key,
|
|
446
|
-
type:
|
|
586
|
+
type: REDUCER_LINK_FLAG,
|
|
447
587
|
};
|
|
448
588
|
}
|
|
449
589
|
}
|
|
450
590
|
return store;
|
|
451
591
|
}
|
|
452
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
|
+
|
|
453
601
|
const requestStateDefault = {
|
|
454
602
|
isAborted: false,
|
|
455
603
|
isFulfilled: false,
|
|
@@ -534,6 +682,6 @@ const requestState = {
|
|
|
534
682
|
},
|
|
535
683
|
};
|
|
536
684
|
|
|
537
|
-
const { StoreProvider, useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
|
|
685
|
+
const { StoreProvider, useDispatcher, useProp, usePropAll, usePropSelector, usePropSelectorAll, useStore, } = createStoreRegistry();
|
|
538
686
|
|
|
539
|
-
export { DESTROY, InternalStore, StoreProvider, createStore, createStoreRegistry, requestState, requestStatus, useDispatcher, useProp, usePropAll, useStore };
|
|
687
|
+
export { DESTROY, InternalStore, LOCKED, OVERRIDE_LOCKED, StoreProvider, createStore, createStoreRegistry, requestState, requestStatus, useDispatcher, useProp, usePropAll, usePropSelector, usePropSelectorAll, useStore };
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function createUseSelector(): any;
|
|
@@ -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
|
-
};
|