beestate 0.1.7 → 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,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;
@@ -241,10 +241,19 @@ function createUseStore(storeRegistry) {
241
241
  const { ownerKey } = ownerKeyRef.current;
242
242
  const { initInstances } = initInstancesRef.current;
243
243
  for (const instanceKey of actualInstanceKeys) {
244
+ // TODO А что по поводу альтернативного сценария?
244
245
  if (!initInstances[instanceKey]) {
245
246
  const initInstance = (initPayload) => {
246
247
  const storeController = createStoreController(storeRegistry, internalStoreProps);
247
- if (!storeController.instances[instanceKey]) {
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,7 +290,7 @@ function createUseStore(storeRegistry) {
285
290
  };
286
291
  }
287
292
  }
288
- for (const instanceKey in initInstances) {
293
+ for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
289
294
  if (!actualInstanceKeys.includes(instanceKey)) {
290
295
  initInstances[instanceKey].resetInstance();
291
296
  }
@@ -294,9 +299,9 @@ function createUseStore(storeRegistry) {
294
299
  // eslint-disable-next-line react-hooks/exhaustive-deps
295
300
  [actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
296
301
  React.useEffect(() => {
297
- const initInstances = initInstancesRef.current.initInstances;
302
+ const { initInstances } = initInstancesRef.current;
298
303
  return () => {
299
- for (const instanceKey in initInstances) {
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(props) {
443
- const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
444
- return (jsxRuntime.jsx(Context.Provider, { value: props.instanceKey, children: props.children }));
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();
@@ -239,10 +239,19 @@ function createUseStore(storeRegistry) {
239
239
  const { ownerKey } = ownerKeyRef.current;
240
240
  const { initInstances } = initInstancesRef.current;
241
241
  for (const instanceKey of actualInstanceKeys) {
242
+ // TODO А что по поводу альтернативного сценария?
242
243
  if (!initInstances[instanceKey]) {
243
244
  const initInstance = (initPayload) => {
244
245
  const storeController = createStoreController(storeRegistry, internalStoreProps);
245
- 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 {
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,7 +288,7 @@ function createUseStore(storeRegistry) {
283
288
  };
284
289
  }
285
290
  }
286
- for (const instanceKey in initInstances) {
291
+ for (const instanceKey of [...Object.keys(initInstances), ...Object.getOwnPropertySymbols(initInstances)]) {
287
292
  if (!actualInstanceKeys.includes(instanceKey)) {
288
293
  initInstances[instanceKey].resetInstance();
289
294
  }
@@ -292,9 +297,9 @@ function createUseStore(storeRegistry) {
292
297
  // eslint-disable-next-line react-hooks/exhaustive-deps
293
298
  [actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
294
299
  useEffect(() => {
295
- const initInstances = initInstancesRef.current.initInstances;
300
+ const { initInstances } = initInstancesRef.current;
296
301
  return () => {
297
- for (const instanceKey in initInstances) {
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(props) {
441
- const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
442
- return (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 (jsx(Context.Provider, { value: instanceKey, children: children }));
443
450
  }
444
451
 
445
452
  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.8"
24
24
  }