beestate 0.1.6 → 0.1.8
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.
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { InstanceKey } from './types/InstanceKey.tsx';
|
|
3
|
+
import type { ReducerMap } from './types/ReducerMap.tsx';
|
|
3
4
|
import type { Store } from './types/Store.tsx';
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
import type { StoreState } from './types/StoreState.tsx';
|
|
6
|
+
export type StoreProviderProps<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = React.PropsWithChildren<{
|
|
6
7
|
instanceKey: InstanceKey;
|
|
8
|
+
store: Store<TStoreName, TStoreState, TReducerMap>;
|
|
7
9
|
}>;
|
|
8
|
-
export declare function StoreProvider(
|
|
10
|
+
export declare function StoreProvider<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>>({ children, instanceKey, store, }: StoreProviderProps<TStoreName, TStoreState, TReducerMap>): import("react/jsx-runtime").JSX.Element;
|
|
@@ -3,8 +3,8 @@ import type { InstanceKey } from './InstanceKey.tsx';
|
|
|
3
3
|
import type { ReducerMap } from './ReducerMap.tsx';
|
|
4
4
|
import type { StoreState } from './StoreState.tsx';
|
|
5
5
|
export type InternalStoreProps<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
$$init: TReducerMap[`$$init`];
|
|
7
|
+
$$reset: TReducerMap[`$$reset`];
|
|
8
8
|
context: React.Context<null | InstanceKey>;
|
|
9
9
|
initialState: TStoreState;
|
|
10
10
|
name: TStoreName;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Reducer } from './Reducer.tsx';
|
|
2
2
|
import type { StoreState } from './StoreState.tsx';
|
|
3
3
|
export type ReducerMap<TStoreState extends StoreState = StoreState> = Record<string, Reducer<TStoreState, void | any>> & {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
$$init: Reducer<TStoreState, void | any>;
|
|
5
|
+
$$reset: Reducer<TStoreState, void | any>;
|
|
6
6
|
};
|
|
@@ -6,9 +6,8 @@ import type { StateLink } from './StateLink.tsx';
|
|
|
6
6
|
import type { StoreState } from './StoreState.tsx';
|
|
7
7
|
export type Store<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
|
|
8
8
|
[INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
[`$$reset`]: void;
|
|
9
|
+
$$init: void;
|
|
10
|
+
$$reset: void;
|
|
12
11
|
} & {
|
|
13
12
|
[TKey in keyof TStoreState]: TKey extends string ? StateLink<TStoreName, TStoreState, TReducerMap, TKey> : never;
|
|
14
13
|
} & {
|
package/dist/cjs/index.cjs.js
CHANGED
|
@@ -215,10 +215,18 @@ function isEqual(prevStateAll, stateAll) {
|
|
|
215
215
|
|
|
216
216
|
function createUseStore(storeRegistry) {
|
|
217
217
|
function useStore(store, ...args) {
|
|
218
|
+
const storeRef = React.useRef({ store });
|
|
219
|
+
const storeMismatchWarnedRef = React.useRef({ warned: false });
|
|
220
|
+
if (storeRef.current.store !== store) {
|
|
221
|
+
console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
|
|
222
|
+
storeMismatchWarnedRef.current.warned = true;
|
|
223
|
+
}
|
|
218
224
|
let instanceKeys;
|
|
219
225
|
let registerInstance;
|
|
226
|
+
const internalStoreProps = storeRef.current.store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
227
|
+
const contextInstanceKey = React.useContext(internalStoreProps.context);
|
|
220
228
|
if (typeof args[0] === `function`) {
|
|
221
|
-
instanceKeys = [DEFAULT_INSTANCE_KEY];
|
|
229
|
+
instanceKeys = [contextInstanceKey ?? DEFAULT_INSTANCE_KEY];
|
|
222
230
|
registerInstance = args[0];
|
|
223
231
|
}
|
|
224
232
|
else {
|
|
@@ -227,24 +235,25 @@ function createUseStore(storeRegistry) {
|
|
|
227
235
|
}
|
|
228
236
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
229
237
|
const actualInstanceKeys = React.useMemo(() => instanceKeys, instanceKeys);
|
|
230
|
-
const storeRef = React.useRef({ store });
|
|
231
238
|
const ownerKeyRef = React.useRef({ ownerKey: Symbol(`ownerKey`) });
|
|
232
239
|
const initInstancesRef = React.useRef({ initInstances: {} });
|
|
233
|
-
const storeMismatchWarnedRef = React.useRef({ warned: false });
|
|
234
|
-
if (storeRef.current.store !== store) {
|
|
235
|
-
console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
|
|
236
|
-
storeMismatchWarnedRef.current.warned = true;
|
|
237
|
-
}
|
|
238
240
|
React.useEffect(() => {
|
|
239
|
-
const { store } = storeRef.current;
|
|
240
241
|
const { ownerKey } = ownerKeyRef.current;
|
|
241
242
|
const { initInstances } = initInstancesRef.current;
|
|
242
243
|
for (const instanceKey of actualInstanceKeys) {
|
|
244
|
+
// TODO А что по поводу альтернативного сценария?
|
|
243
245
|
if (!initInstances[instanceKey]) {
|
|
244
246
|
const initInstance = (initPayload) => {
|
|
245
|
-
const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
246
247
|
const storeController = createStoreController(storeRegistry, internalStoreProps);
|
|
247
|
-
if (
|
|
248
|
+
if (storeController.instances[instanceKey]) {
|
|
249
|
+
// TODO Множественная инициализация хранилища
|
|
250
|
+
// Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
|
|
251
|
+
// одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
|
|
252
|
+
// стоит просить разработчиков проверять инициализирована ли стора и
|
|
253
|
+
// инициализировать ее при необходимости.
|
|
254
|
+
console.warn(`Duplicate store initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
248
257
|
// TODO Убрать в логгер
|
|
249
258
|
console.log(`Store instance initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
250
259
|
// TODO Убрать в логгер
|
|
@@ -258,18 +267,14 @@ function createUseStore(storeRegistry) {
|
|
|
258
267
|
storeController.earlySubscribers[instanceKey].splice(0);
|
|
259
268
|
}
|
|
260
269
|
}
|
|
261
|
-
else {
|
|
262
|
-
// TODO Множественная инициализация хранилища
|
|
263
|
-
// Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
|
|
264
|
-
// одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
|
|
265
|
-
// стоит просить разработчиков проверять инициализирована ли стора и
|
|
266
|
-
// инициализировать ее при необходимости.
|
|
267
|
-
console.warn(`Duplicate store initialization for key "${instanceKey.toString()}"`);
|
|
268
|
-
}
|
|
269
270
|
storeController.instances[instanceKey].owners.push(ownerKey);
|
|
270
271
|
return (resetPayload) => {
|
|
271
272
|
storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
|
|
272
273
|
if (storeController.instances[instanceKey].owners.length === 0) {
|
|
274
|
+
// TODO Убрать в логгер
|
|
275
|
+
console.log(`Store instance resetting [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
276
|
+
// TODO Убрать в логгер
|
|
277
|
+
console.log({ storeRegistry });
|
|
273
278
|
// TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
|
|
274
279
|
storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps.$$reset);
|
|
275
280
|
delete storeController.instances[instanceKey];
|
|
@@ -285,18 +290,18 @@ function createUseStore(storeRegistry) {
|
|
|
285
290
|
};
|
|
286
291
|
}
|
|
287
292
|
}
|
|
288
|
-
for (const instanceKey
|
|
293
|
+
for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
|
|
289
294
|
if (!actualInstanceKeys.includes(instanceKey)) {
|
|
290
295
|
initInstances[instanceKey].resetInstance();
|
|
291
296
|
}
|
|
292
297
|
}
|
|
293
298
|
},
|
|
294
299
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
295
|
-
[actualInstanceKeys, /*registerInstance*/]);
|
|
300
|
+
[actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
|
|
296
301
|
React.useEffect(() => {
|
|
297
|
-
const initInstances = initInstancesRef.current
|
|
302
|
+
const { initInstances } = initInstancesRef.current;
|
|
298
303
|
return () => {
|
|
299
|
-
for (const instanceKey
|
|
304
|
+
for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
|
|
300
305
|
initInstances[instanceKey].resetInstance();
|
|
301
306
|
}
|
|
302
307
|
};
|
|
@@ -439,9 +444,11 @@ const requestState = {
|
|
|
439
444
|
},
|
|
440
445
|
};
|
|
441
446
|
|
|
442
|
-
function StoreProvider(
|
|
443
|
-
|
|
444
|
-
|
|
447
|
+
function StoreProvider({ children,
|
|
448
|
+
// initialState,
|
|
449
|
+
instanceKey, store, }) {
|
|
450
|
+
const Context = store[INTERNAL_STORE_PROPS_ACCESSOR].context;
|
|
451
|
+
return (jsxRuntime.jsx(Context.Provider, { value: instanceKey, children: children }));
|
|
445
452
|
}
|
|
446
453
|
|
|
447
454
|
const { useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
|
package/dist/esm/index.esm.js
CHANGED
|
@@ -213,10 +213,18 @@ function isEqual(prevStateAll, stateAll) {
|
|
|
213
213
|
|
|
214
214
|
function createUseStore(storeRegistry) {
|
|
215
215
|
function useStore(store, ...args) {
|
|
216
|
+
const storeRef = useRef({ store });
|
|
217
|
+
const storeMismatchWarnedRef = useRef({ warned: false });
|
|
218
|
+
if (storeRef.current.store !== store) {
|
|
219
|
+
console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
|
|
220
|
+
storeMismatchWarnedRef.current.warned = true;
|
|
221
|
+
}
|
|
216
222
|
let instanceKeys;
|
|
217
223
|
let registerInstance;
|
|
224
|
+
const internalStoreProps = storeRef.current.store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
225
|
+
const contextInstanceKey = useContext(internalStoreProps.context);
|
|
218
226
|
if (typeof args[0] === `function`) {
|
|
219
|
-
instanceKeys = [DEFAULT_INSTANCE_KEY];
|
|
227
|
+
instanceKeys = [contextInstanceKey ?? DEFAULT_INSTANCE_KEY];
|
|
220
228
|
registerInstance = args[0];
|
|
221
229
|
}
|
|
222
230
|
else {
|
|
@@ -225,24 +233,25 @@ function createUseStore(storeRegistry) {
|
|
|
225
233
|
}
|
|
226
234
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
227
235
|
const actualInstanceKeys = useMemo(() => instanceKeys, instanceKeys);
|
|
228
|
-
const storeRef = useRef({ store });
|
|
229
236
|
const ownerKeyRef = useRef({ ownerKey: Symbol(`ownerKey`) });
|
|
230
237
|
const initInstancesRef = useRef({ initInstances: {} });
|
|
231
|
-
const storeMismatchWarnedRef = useRef({ warned: false });
|
|
232
|
-
if (storeRef.current.store !== store) {
|
|
233
|
-
console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
|
|
234
|
-
storeMismatchWarnedRef.current.warned = true;
|
|
235
|
-
}
|
|
236
238
|
useEffect(() => {
|
|
237
|
-
const { store } = storeRef.current;
|
|
238
239
|
const { ownerKey } = ownerKeyRef.current;
|
|
239
240
|
const { initInstances } = initInstancesRef.current;
|
|
240
241
|
for (const instanceKey of actualInstanceKeys) {
|
|
242
|
+
// TODO А что по поводу альтернативного сценария?
|
|
241
243
|
if (!initInstances[instanceKey]) {
|
|
242
244
|
const initInstance = (initPayload) => {
|
|
243
|
-
const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
244
245
|
const storeController = createStoreController(storeRegistry, internalStoreProps);
|
|
245
|
-
if (
|
|
246
|
+
if (storeController.instances[instanceKey]) {
|
|
247
|
+
// TODO Множественная инициализация хранилища
|
|
248
|
+
// Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
|
|
249
|
+
// одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
|
|
250
|
+
// стоит просить разработчиков проверять инициализирована ли стора и
|
|
251
|
+
// инициализировать ее при необходимости.
|
|
252
|
+
console.warn(`Duplicate store initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
246
255
|
// TODO Убрать в логгер
|
|
247
256
|
console.log(`Store instance initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
248
257
|
// TODO Убрать в логгер
|
|
@@ -256,18 +265,14 @@ function createUseStore(storeRegistry) {
|
|
|
256
265
|
storeController.earlySubscribers[instanceKey].splice(0);
|
|
257
266
|
}
|
|
258
267
|
}
|
|
259
|
-
else {
|
|
260
|
-
// TODO Множественная инициализация хранилища
|
|
261
|
-
// Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
|
|
262
|
-
// одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
|
|
263
|
-
// стоит просить разработчиков проверять инициализирована ли стора и
|
|
264
|
-
// инициализировать ее при необходимости.
|
|
265
|
-
console.warn(`Duplicate store initialization for key "${instanceKey.toString()}"`);
|
|
266
|
-
}
|
|
267
268
|
storeController.instances[instanceKey].owners.push(ownerKey);
|
|
268
269
|
return (resetPayload) => {
|
|
269
270
|
storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
|
|
270
271
|
if (storeController.instances[instanceKey].owners.length === 0) {
|
|
272
|
+
// TODO Убрать в логгер
|
|
273
|
+
console.log(`Store instance resetting [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
274
|
+
// TODO Убрать в логгер
|
|
275
|
+
console.log({ storeRegistry });
|
|
271
276
|
// TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
|
|
272
277
|
storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps.$$reset);
|
|
273
278
|
delete storeController.instances[instanceKey];
|
|
@@ -283,18 +288,18 @@ function createUseStore(storeRegistry) {
|
|
|
283
288
|
};
|
|
284
289
|
}
|
|
285
290
|
}
|
|
286
|
-
for (const instanceKey
|
|
291
|
+
for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
|
|
287
292
|
if (!actualInstanceKeys.includes(instanceKey)) {
|
|
288
293
|
initInstances[instanceKey].resetInstance();
|
|
289
294
|
}
|
|
290
295
|
}
|
|
291
296
|
},
|
|
292
297
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
293
|
-
[actualInstanceKeys, /*registerInstance*/]);
|
|
298
|
+
[actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
|
|
294
299
|
useEffect(() => {
|
|
295
|
-
const initInstances = initInstancesRef.current
|
|
300
|
+
const { initInstances } = initInstancesRef.current;
|
|
296
301
|
return () => {
|
|
297
|
-
for (const instanceKey
|
|
302
|
+
for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
|
|
298
303
|
initInstances[instanceKey].resetInstance();
|
|
299
304
|
}
|
|
300
305
|
};
|
|
@@ -437,9 +442,11 @@ const requestState = {
|
|
|
437
442
|
},
|
|
438
443
|
};
|
|
439
444
|
|
|
440
|
-
function StoreProvider(
|
|
441
|
-
|
|
442
|
-
|
|
445
|
+
function StoreProvider({ children,
|
|
446
|
+
// initialState,
|
|
447
|
+
instanceKey, store, }) {
|
|
448
|
+
const Context = store[INTERNAL_STORE_PROPS_ACCESSOR].context;
|
|
449
|
+
return (jsx(Context.Provider, { value: instanceKey, children: children }));
|
|
443
450
|
}
|
|
444
451
|
|
|
445
452
|
const { useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
|
package/package.json
CHANGED