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/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]);
|
|
@@ -263,6 +327,26 @@ function createUseProp(storeRegistry) {
|
|
|
263
327
|
};
|
|
264
328
|
}
|
|
265
329
|
|
|
330
|
+
function pickPropAll(stateAll, stateName, instanceKeys) {
|
|
331
|
+
let actualInstanceKeys;
|
|
332
|
+
if (instanceKeys == null) {
|
|
333
|
+
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)];
|
|
334
|
+
}
|
|
335
|
+
else if (typeof instanceKeys === `function`) {
|
|
336
|
+
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)].filter((instanceKey) => {
|
|
337
|
+
return instanceKeys(stateAll[instanceKey]);
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
actualInstanceKeys = instanceKeys.filter((instanceKey) => {
|
|
342
|
+
return instanceKey in stateAll;
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
return actualInstanceKeys.map((instanceKey) => {
|
|
346
|
+
return [instanceKey, stateAll[instanceKey][stateName]];
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
266
350
|
function createUsePropAll(storeRegistry) {
|
|
267
351
|
return function usePropAll(stateLink, instanceKeys) {
|
|
268
352
|
const stateLinkRef = React.useRef({ stateLink });
|
|
@@ -279,23 +363,7 @@ function createUsePropAll(storeRegistry) {
|
|
|
279
363
|
return internalStore.getStateAll();
|
|
280
364
|
}, [internalStore]);
|
|
281
365
|
const selector = React.useCallback((stateAll) => {
|
|
282
|
-
|
|
283
|
-
if (instanceKeys == null) {
|
|
284
|
-
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)];
|
|
285
|
-
}
|
|
286
|
-
else if (typeof instanceKeys === `function`) {
|
|
287
|
-
actualInstanceKeys = [...Object.keys(stateAll), ...Object.getOwnPropertySymbols(stateAll)].filter((instanceKey) => {
|
|
288
|
-
return instanceKeys(stateAll[instanceKey]);
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
actualInstanceKeys = instanceKeys.filter((instanceKey) => {
|
|
293
|
-
return instanceKey in stateAll;
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
return actualInstanceKeys.map((instanceKey) => {
|
|
297
|
-
return [instanceKey, stateAll[instanceKey][actualStateLink.stateName]];
|
|
298
|
-
});
|
|
366
|
+
return pickPropAll(stateAll, actualStateLink.stateName, instanceKeys);
|
|
299
367
|
}, [actualStateLink, instanceKeys]);
|
|
300
368
|
return withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, null, selector, isEqual);
|
|
301
369
|
};
|
|
@@ -311,6 +379,52 @@ function isEqual(prevStateAll, stateAll) {
|
|
|
311
379
|
}
|
|
312
380
|
}
|
|
313
381
|
|
|
382
|
+
function createUsePropSelector(storeRegistry) {
|
|
383
|
+
return function usePropSelector(stateLink, instanceKey) {
|
|
384
|
+
const stateLinkRef = React.useRef({ stateLink });
|
|
385
|
+
if (stateLinkRef.current.stateLink !== stateLink) {
|
|
386
|
+
console.warn(`State link reference mismatch. You must pass the same state link on every render.`);
|
|
387
|
+
}
|
|
388
|
+
const actualStateLink = stateLinkRef.current.stateLink;
|
|
389
|
+
const internalStoreProps = actualStateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
390
|
+
const storeContextValue = React.useContext(internalStoreProps.context);
|
|
391
|
+
const actualInstanceKey = instanceKey ?? storeContextValue?.instanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
392
|
+
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
393
|
+
const uninitializedState = storeContextValue?.instanceKey === actualInstanceKey
|
|
394
|
+
? storeContextValue.uninitializedState
|
|
395
|
+
: internalStoreProps.unlockedInitialState;
|
|
396
|
+
return React.useCallback(() => {
|
|
397
|
+
if (actualStateLink.stateName === INITIALIZED_STATE_NAME) {
|
|
398
|
+
return internalStore.isInitialized(actualInstanceKey);
|
|
399
|
+
}
|
|
400
|
+
else if (actualStateLink.stateName === INSTANCE_KEY_STATE_NAME) {
|
|
401
|
+
return actualInstanceKey;
|
|
402
|
+
}
|
|
403
|
+
else if (internalStore.isInitialized(actualInstanceKey)) {
|
|
404
|
+
return internalStore.getState(actualInstanceKey)[actualStateLink.stateName];
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
return uninitializedState[actualStateLink.stateName];
|
|
408
|
+
}
|
|
409
|
+
}, [actualInstanceKey, actualStateLink, internalStore, uninitializedState]);
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function createUsePropSelectorAll(storeRegistry) {
|
|
414
|
+
return function usePropSelectorAll(stateLink, instanceKeys) {
|
|
415
|
+
const stateLinkRef = React.useRef({ stateLink });
|
|
416
|
+
if (stateLinkRef.current.stateLink !== stateLink) {
|
|
417
|
+
console.warn(`State link reference mismatch. You must pass the same state link on every render.`);
|
|
418
|
+
}
|
|
419
|
+
const actualStateLink = stateLinkRef.current.stateLink;
|
|
420
|
+
const internalStoreProps = actualStateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
421
|
+
const internalStore = ensureInternalStore(storeRegistry, internalStoreProps);
|
|
422
|
+
return React.useCallback(() => {
|
|
423
|
+
return pickPropAll(internalStore.getStateAll(), actualStateLink.stateName, instanceKeys);
|
|
424
|
+
}, [actualStateLink, instanceKeys, internalStore]);
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
314
428
|
function createUseStore(storeRegistry) {
|
|
315
429
|
function useStore(store, ...args) {
|
|
316
430
|
const storeRef = React.useRef({ store });
|
|
@@ -348,7 +462,7 @@ function createUseStore(storeRegistry) {
|
|
|
348
462
|
console.log(`Store instance initialization [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
349
463
|
// TODO Убрать в логгер
|
|
350
464
|
console.log({ storeRegistry });
|
|
351
|
-
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.
|
|
465
|
+
internalStore.initInstance(instanceKey, internalStoreProps.$$init, internalStoreProps.unlockedInitialState, prefill);
|
|
352
466
|
}
|
|
353
467
|
else {
|
|
354
468
|
console.warn(`Store instance is already initialized, initialization skipped [store: ${internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
@@ -395,29 +509,55 @@ function createStoreRegistry() {
|
|
|
395
509
|
const useStore = createUseStore(storeRegistry);
|
|
396
510
|
const useProp = createUseProp(storeRegistry);
|
|
397
511
|
const usePropAll = createUsePropAll(storeRegistry);
|
|
512
|
+
const usePropSelector = createUsePropSelector(storeRegistry);
|
|
513
|
+
const usePropSelectorAll = createUsePropSelectorAll(storeRegistry);
|
|
398
514
|
const useDispatcher = createUseDispatcher(storeRegistry);
|
|
399
515
|
const useLatest = createUseLatest();
|
|
400
516
|
const StoreProvider = createStoreProvider(useStore);
|
|
401
517
|
return {
|
|
402
|
-
storeRegistry,
|
|
403
518
|
StoreProvider,
|
|
404
|
-
|
|
405
|
-
useProp,
|
|
406
|
-
usePropAll,
|
|
519
|
+
storeRegistry,
|
|
407
520
|
useDispatcher,
|
|
408
521
|
useLatest,
|
|
522
|
+
useProp,
|
|
523
|
+
usePropAll,
|
|
524
|
+
usePropSelector,
|
|
525
|
+
usePropSelectorAll,
|
|
526
|
+
useStore,
|
|
409
527
|
};
|
|
410
528
|
}
|
|
411
529
|
|
|
412
530
|
function createStore(name, initialState, reducerMap) {
|
|
413
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
|
+
}
|
|
414
552
|
const internalStoreProps = {
|
|
415
553
|
$$init: reducerMap.$$init,
|
|
416
554
|
$$reset: reducerMap.$$reset,
|
|
417
555
|
context,
|
|
418
556
|
initialState,
|
|
557
|
+
lockedStateNames,
|
|
419
558
|
name,
|
|
420
559
|
uniqKey: Symbol(`Store: ` + name),
|
|
560
|
+
unlockedInitialState,
|
|
421
561
|
};
|
|
422
562
|
const store = {
|
|
423
563
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
@@ -426,18 +566,18 @@ function createStore(name, initialState, reducerMap) {
|
|
|
426
566
|
store[key] = {
|
|
427
567
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
428
568
|
stateName: key,
|
|
429
|
-
type:
|
|
569
|
+
type: STATE_LINK_FLAG,
|
|
430
570
|
};
|
|
431
571
|
}
|
|
432
572
|
store[INITIALIZED_STATE_NAME] = {
|
|
433
573
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
434
574
|
stateName: INITIALIZED_STATE_NAME,
|
|
435
|
-
type:
|
|
575
|
+
type: STATE_LINK_FLAG,
|
|
436
576
|
};
|
|
437
577
|
store[INSTANCE_KEY_STATE_NAME] = {
|
|
438
578
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
439
579
|
stateName: INSTANCE_KEY_STATE_NAME,
|
|
440
|
-
type:
|
|
580
|
+
type: STATE_LINK_FLAG,
|
|
441
581
|
};
|
|
442
582
|
for (const key in reducerMap) {
|
|
443
583
|
if (!(key in initialState) && ![`$$init`, `$$reset`].includes(key)) {
|
|
@@ -445,13 +585,21 @@ function createStore(name, initialState, reducerMap) {
|
|
|
445
585
|
[INTERNAL_STORE_PROPS_ACCESSOR]: internalStoreProps,
|
|
446
586
|
reducer: reducerMap[key],
|
|
447
587
|
reducerName: key,
|
|
448
|
-
type:
|
|
588
|
+
type: REDUCER_LINK_FLAG,
|
|
449
589
|
};
|
|
450
590
|
}
|
|
451
591
|
}
|
|
452
592
|
return store;
|
|
453
593
|
}
|
|
454
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
|
+
|
|
455
603
|
const requestStateDefault = {
|
|
456
604
|
isAborted: false,
|
|
457
605
|
isFulfilled: false,
|
|
@@ -536,10 +684,12 @@ const requestState = {
|
|
|
536
684
|
},
|
|
537
685
|
};
|
|
538
686
|
|
|
539
|
-
const { StoreProvider, useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
|
|
687
|
+
const { StoreProvider, useDispatcher, useProp, usePropAll, usePropSelector, usePropSelectorAll, useStore, } = createStoreRegistry();
|
|
540
688
|
|
|
541
689
|
exports.DESTROY = DESTROY;
|
|
542
690
|
exports.InternalStore = InternalStore;
|
|
691
|
+
exports.LOCKED = LOCKED;
|
|
692
|
+
exports.OVERRIDE_LOCKED = OVERRIDE_LOCKED;
|
|
543
693
|
exports.StoreProvider = StoreProvider;
|
|
544
694
|
exports.createStore = createStore;
|
|
545
695
|
exports.createStoreRegistry = createStoreRegistry;
|
|
@@ -548,4 +698,6 @@ exports.requestStatus = requestStatus;
|
|
|
548
698
|
exports.useDispatcher = useDispatcher;
|
|
549
699
|
exports.useProp = useProp;
|
|
550
700
|
exports.usePropAll = usePropAll;
|
|
701
|
+
exports.usePropSelector = usePropSelector;
|
|
702
|
+
exports.usePropSelectorAll = usePropSelectorAll;
|
|
551
703
|
exports.useStore = useStore;
|