beestate 0.1.7 → 0.1.9

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,12 +1,10 @@
1
1
  import React from 'react';
2
2
  import type { InstanceKey } from './types/InstanceKey.tsx';
3
- import { INTERNAL_STORE_PROPS_ACCESSOR } from './constants.tsx';
4
- export type StoreProviderProps = React.PropsWithChildren<{
5
- store: {
6
- [INTERNAL_STORE_PROPS_ACCESSOR]: {
7
- context: React.Context<null | InstanceKey>;
8
- };
9
- };
3
+ import type { ReducerMap } from './types/ReducerMap.tsx';
4
+ import type { Store } from './types/Store.tsx';
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<{
10
7
  instanceKey: InstanceKey;
8
+ store: Store<TStoreName, TStoreState, TReducerMap>;
11
9
  }>;
12
- export declare function StoreProvider(props: StoreProviderProps): import("react/jsx-runtime").JSX.Element;
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;
@@ -65,16 +65,14 @@ class InternalStore {
65
65
  this.dispatch(instanceKey, initPayload, initReducer);
66
66
  return (resetPayload, resetReducer) => {
67
67
  this.dispatch(instanceKey, resetPayload, resetReducer);
68
- // TODO Тут место для уничтожения состояния экземпляра хранилища через `delete this.state[instanceKey];`
68
+ delete this.state[instanceKey];
69
+ delete this.listeners[instanceKey];
69
70
  };
70
71
  };
71
72
  this.subscribe = (instanceKey, listener) => {
72
73
  this.listeners[instanceKey].add(listener);
73
74
  return () => {
74
75
  this.listeners[instanceKey].delete(listener);
75
- if (this.listeners[instanceKey].size === 0) {
76
- delete this.listeners[instanceKey];
77
- }
78
76
  };
79
77
  };
80
78
  this.subscribeAll = (listener) => {
@@ -241,10 +239,19 @@ function createUseStore(storeRegistry) {
241
239
  const { ownerKey } = ownerKeyRef.current;
242
240
  const { initInstances } = initInstancesRef.current;
243
241
  for (const instanceKey of actualInstanceKeys) {
242
+ // TODO А что по поводу альтернативного сценария?
244
243
  if (!initInstances[instanceKey]) {
245
244
  const initInstance = (initPayload) => {
246
245
  const storeController = createStoreController(storeRegistry, internalStoreProps);
247
- if (!storeController.instances[instanceKey]) {
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 {
248
255
  // TODO Убрать в логгер
249
256
  console.log(`Store instance initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
250
257
  // TODO Убрать в логгер
@@ -258,18 +265,14 @@ function createUseStore(storeRegistry) {
258
265
  storeController.earlySubscribers[instanceKey].splice(0);
259
266
  }
260
267
  }
261
- else {
262
- // TODO Множественная инициализация хранилища
263
- // Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
264
- // одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
265
- // стоит просить разработчиков проверять инициализирована ли стора и
266
- // инициализировать ее при необходимости.
267
- console.warn(`Duplicate store initialization for key "${instanceKey.toString()}"`);
268
- }
269
268
  storeController.instances[instanceKey].owners.push(ownerKey);
270
269
  return (resetPayload) => {
271
270
  storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
272
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 });
273
276
  // TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
274
277
  storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps.$$reset);
275
278
  delete storeController.instances[instanceKey];
@@ -285,7 +288,7 @@ function createUseStore(storeRegistry) {
285
288
  };
286
289
  }
287
290
  }
288
- for (const instanceKey in initInstances) {
291
+ for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
289
292
  if (!actualInstanceKeys.includes(instanceKey)) {
290
293
  initInstances[instanceKey].resetInstance();
291
294
  }
@@ -294,9 +297,9 @@ function createUseStore(storeRegistry) {
294
297
  // eslint-disable-next-line react-hooks/exhaustive-deps
295
298
  [actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
296
299
  React.useEffect(() => {
297
- const initInstances = initInstancesRef.current.initInstances;
300
+ const { initInstances } = initInstancesRef.current;
298
301
  return () => {
299
- for (const instanceKey in initInstances) {
302
+ for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
300
303
  initInstances[instanceKey].resetInstance();
301
304
  }
302
305
  };
@@ -439,9 +442,11 @@ const requestState = {
439
442
  },
440
443
  };
441
444
 
442
- function StoreProvider(props) {
443
- const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
444
- return (jsxRuntime.jsx(Context.Provider, { value: props.instanceKey, children: props.children }));
445
+ function StoreProvider({ children,
446
+ // initialState,
447
+ instanceKey, store, }) {
448
+ const Context = store[INTERNAL_STORE_PROPS_ACCESSOR].context;
449
+ return (jsxRuntime.jsx(Context.Provider, { value: instanceKey, children: children }));
445
450
  }
446
451
 
447
452
  const { useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
@@ -63,16 +63,14 @@ class InternalStore {
63
63
  this.dispatch(instanceKey, initPayload, initReducer);
64
64
  return (resetPayload, resetReducer) => {
65
65
  this.dispatch(instanceKey, resetPayload, resetReducer);
66
- // TODO Тут место для уничтожения состояния экземпляра хранилища через `delete this.state[instanceKey];`
66
+ delete this.state[instanceKey];
67
+ delete this.listeners[instanceKey];
67
68
  };
68
69
  };
69
70
  this.subscribe = (instanceKey, listener) => {
70
71
  this.listeners[instanceKey].add(listener);
71
72
  return () => {
72
73
  this.listeners[instanceKey].delete(listener);
73
- if (this.listeners[instanceKey].size === 0) {
74
- delete this.listeners[instanceKey];
75
- }
76
74
  };
77
75
  };
78
76
  this.subscribeAll = (listener) => {
@@ -239,10 +237,19 @@ function createUseStore(storeRegistry) {
239
237
  const { ownerKey } = ownerKeyRef.current;
240
238
  const { initInstances } = initInstancesRef.current;
241
239
  for (const instanceKey of actualInstanceKeys) {
240
+ // TODO А что по поводу альтернативного сценария?
242
241
  if (!initInstances[instanceKey]) {
243
242
  const initInstance = (initPayload) => {
244
243
  const storeController = createStoreController(storeRegistry, internalStoreProps);
245
- if (!storeController.instances[instanceKey]) {
244
+ if (storeController.instances[instanceKey]) {
245
+ // TODO Множественная инициализация хранилища
246
+ // Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
247
+ // одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
248
+ // стоит просить разработчиков проверять инициализирована ли стора и
249
+ // инициализировать ее при необходимости.
250
+ console.warn(`Duplicate store initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
251
+ }
252
+ else {
246
253
  // TODO Убрать в логгер
247
254
  console.log(`Store instance initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
248
255
  // TODO Убрать в логгер
@@ -256,18 +263,14 @@ function createUseStore(storeRegistry) {
256
263
  storeController.earlySubscribers[instanceKey].splice(0);
257
264
  }
258
265
  }
259
- else {
260
- // TODO Множественная инициализация хранилища
261
- // Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
262
- // одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
263
- // стоит просить разработчиков проверять инициализирована ли стора и
264
- // инициализировать ее при необходимости.
265
- console.warn(`Duplicate store initialization for key "${instanceKey.toString()}"`);
266
- }
267
266
  storeController.instances[instanceKey].owners.push(ownerKey);
268
267
  return (resetPayload) => {
269
268
  storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
270
269
  if (storeController.instances[instanceKey].owners.length === 0) {
270
+ // TODO Убрать в логгер
271
+ console.log(`Store instance resetting [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
272
+ // TODO Убрать в логгер
273
+ console.log({ storeRegistry });
271
274
  // TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
272
275
  storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps.$$reset);
273
276
  delete storeController.instances[instanceKey];
@@ -283,7 +286,7 @@ function createUseStore(storeRegistry) {
283
286
  };
284
287
  }
285
288
  }
286
- for (const instanceKey in initInstances) {
289
+ for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
287
290
  if (!actualInstanceKeys.includes(instanceKey)) {
288
291
  initInstances[instanceKey].resetInstance();
289
292
  }
@@ -292,9 +295,9 @@ function createUseStore(storeRegistry) {
292
295
  // eslint-disable-next-line react-hooks/exhaustive-deps
293
296
  [actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
294
297
  useEffect(() => {
295
- const initInstances = initInstancesRef.current.initInstances;
298
+ const { initInstances } = initInstancesRef.current;
296
299
  return () => {
297
- for (const instanceKey in initInstances) {
300
+ for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
298
301
  initInstances[instanceKey].resetInstance();
299
302
  }
300
303
  };
@@ -437,9 +440,11 @@ const requestState = {
437
440
  },
438
441
  };
439
442
 
440
- function StoreProvider(props) {
441
- const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
442
- return (jsx(Context.Provider, { value: props.instanceKey, children: props.children }));
443
+ function StoreProvider({ children,
444
+ // initialState,
445
+ instanceKey, store, }) {
446
+ const Context = store[INTERNAL_STORE_PROPS_ACCESSOR].context;
447
+ return (jsx(Context.Provider, { value: instanceKey, children: children }));
443
448
  }
444
449
 
445
450
  const { useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
package/package.json CHANGED
@@ -20,5 +20,5 @@
20
20
  "prepublishOnly": "npm run build"
21
21
  },
22
22
  "types": "dist/@types",
23
- "version": "0.1.7"
23
+ "version": "0.1.9"
24
24
  }