@tramvai/state 5.9.2 → 5.14.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.
@@ -15,6 +15,21 @@ function useSelector(storesOrStore, selector, equalityFn = shallowEqual) {
15
15
  const context = useConsumerContext();
16
16
  const serverState = useContext(ServerStateContext);
17
17
  const renderIsScheduled = useRef(false);
18
+ if (process.env.NODE_ENV === 'development') {
19
+ const storeItems = toArray(storesOrStore);
20
+ storeItems.forEach((item) => {
21
+ // eslint-disable-next-line no-nested-ternary
22
+ const store = typeof item === 'string' ? item : 'store' in item ? item.store : item;
23
+ const storeName = typeof store === 'string' ? store : store.storeName;
24
+ const exists = context.hasStore(store);
25
+ if (!exists) {
26
+ console.warn(`
27
+ The store "${storeName}" has been used, but not registered via COMBINE_REDUCERS token.
28
+ Have you forgot to register it? Note, that we are registering it for you to make things just work.
29
+ `);
30
+ }
31
+ });
32
+ }
18
33
  const storesRef = useShallowEqual(storesOrStore);
19
34
  const subscription = useMemo(() => new Subscription(toArray(storesRef).map(context.getStore)), [storesRef, context]);
20
35
  const latestSubscriptionCallbackError = useRef();
@@ -25,6 +25,21 @@ function useSelector(storesOrStore, selector, equalityFn = shallowEqual__default
25
25
  const context$1 = useConsumerContext.useConsumerContext();
26
26
  const serverState = React.useContext(context.ServerStateContext);
27
27
  const renderIsScheduled = React.useRef(false);
28
+ if (process.env.NODE_ENV === 'development') {
29
+ const storeItems = toArray__default["default"](storesOrStore);
30
+ storeItems.forEach((item) => {
31
+ // eslint-disable-next-line no-nested-ternary
32
+ const store = typeof item === 'string' ? item : 'store' in item ? item.store : item;
33
+ const storeName = typeof store === 'string' ? store : store.storeName;
34
+ const exists = context$1.hasStore(store);
35
+ if (!exists) {
36
+ console.warn(`
37
+ The store "${storeName}" has been used, but not registered via COMBINE_REDUCERS token.
38
+ Have you forgot to register it? Note, that we are registering it for you to make things just work.
39
+ `);
40
+ }
41
+ });
42
+ }
28
43
  const storesRef = reactHooks.useShallowEqual(storesOrStore);
29
44
  const subscription = React.useMemo(() => new Subscription.Subscription(toArray__default["default"](storesRef).map(context$1.getStore)), [storesRef, context$1]);
30
45
  const latestSubscriptionCallbackError = React.useRef();
@@ -12,6 +12,12 @@ function useStore(reducer) {
12
12
  // регистрируем его вручную, что бы гарантировать работоспособность `context.getState(reducer)`,
13
13
  // и сохраняем в `addedReducerRef`, что бы удалить при unmount
14
14
  if (!context.hasStore(reducer)) {
15
+ if (process.env.NODE_ENV === 'development') {
16
+ console.warn(`
17
+ The store "${reducer.storeName}" has been used, but not registered via COMBINE_REDUCERS token.
18
+ Have you forgot to register it? Note, that we are registering it for you to make things just work.
19
+ `);
20
+ }
15
21
  context.registerStore(reducer);
16
22
  addedReducerRef.current = reducer.storeName;
17
23
  }
@@ -16,6 +16,12 @@ function useStore(reducer) {
16
16
  // регистрируем его вручную, что бы гарантировать работоспособность `context.getState(reducer)`,
17
17
  // и сохраняем в `addedReducerRef`, что бы удалить при unmount
18
18
  if (!context$1.hasStore(reducer)) {
19
+ if (process.env.NODE_ENV === 'development') {
20
+ console.warn(`
21
+ The store "${reducer.storeName}" has been used, but not registered via COMBINE_REDUCERS token.
22
+ Have you forgot to register it? Note, that we are registering it for you to make things just work.
23
+ `);
24
+ }
19
25
  context$1.registerStore(reducer);
20
26
  addedReducerRef.current = reducer.storeName;
21
27
  }
@@ -8,7 +8,7 @@ class ChildDispatcherContext extends DispatcherContext {
8
8
  super(dispatcher, context, initialState, middlewares);
9
9
  this.allowedParentStores = new Set();
10
10
  this.parentDispatcherContext = parentDispatcherContext;
11
- parentAllowedStores === null || parentAllowedStores === void 0 ? void 0 : parentAllowedStores.forEach((store) => {
11
+ parentAllowedStores?.forEach((store) => {
12
12
  const storeName = this.dispatcher.getStoreName(typeof store === 'object' ? store.store : store);
13
13
  this.allowedParentStores.add(storeName);
14
14
  // use just storeName to prevent store initialization on the root-app side
@@ -70,7 +70,6 @@ class ChildDispatcherContext extends DispatcherContext {
70
70
  return storeInstance;
71
71
  }
72
72
  _unsubscribeFromParentAllowedStore(storeName) {
73
- var _a, _b;
74
73
  const storeInstance = this.parentDispatcherContext.getStore({
75
74
  store: storeName,
76
75
  optional: true,
@@ -78,7 +77,7 @@ class ChildDispatcherContext extends DispatcherContext {
78
77
  if (storeInstance) {
79
78
  // in strict mode, unsubscribe callback in `useStore` is fired twice, with same `addedReducerRef.current` value,
80
79
  // and this callback already will be deleted in first call and throw error `is not a function`, so make it optional
81
- (_b = (_a = this.storeUnsubscribeCallbacks)[storeName]) === null || _b === void 0 ? void 0 : _b.call(_a);
80
+ this.storeUnsubscribeCallbacks[storeName]?.();
82
81
  delete this.storeUnsubscribeCallbacks[storeName];
83
82
  }
84
83
  }
@@ -12,7 +12,7 @@ class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
12
12
  super(dispatcher, context, initialState, middlewares);
13
13
  this.allowedParentStores = new Set();
14
14
  this.parentDispatcherContext = parentDispatcherContext;
15
- parentAllowedStores === null || parentAllowedStores === void 0 ? void 0 : parentAllowedStores.forEach((store) => {
15
+ parentAllowedStores?.forEach((store) => {
16
16
  const storeName = this.dispatcher.getStoreName(typeof store === 'object' ? store.store : store);
17
17
  this.allowedParentStores.add(storeName);
18
18
  // use just storeName to prevent store initialization on the root-app side
@@ -74,7 +74,6 @@ class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
74
74
  return storeInstance;
75
75
  }
76
76
  _unsubscribeFromParentAllowedStore(storeName) {
77
- var _a, _b;
78
77
  const storeInstance = this.parentDispatcherContext.getStore({
79
78
  store: storeName,
80
79
  optional: true,
@@ -82,7 +81,7 @@ class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
82
81
  if (storeInstance) {
83
82
  // in strict mode, unsubscribe callback in `useStore` is fired twice, with same `addedReducerRef.current` value,
84
83
  // and this callback already will be deleted in first call and throw error `is not a function`, so make it optional
85
- (_b = (_a = this.storeUnsubscribeCallbacks)[storeName]) === null || _b === void 0 ? void 0 : _b.call(_a);
84
+ this.storeUnsubscribeCallbacks[storeName]?.();
86
85
  delete this.storeUnsubscribeCallbacks[storeName];
87
86
  }
88
87
  }
@@ -77,7 +77,7 @@ export declare class DispatcherContext<TContext> extends SimpleEmitter {
77
77
  getState(): Record<string, any>;
78
78
  getState<S>(reducer: Reducer<S>): S;
79
79
  setContext(context: TContext): void;
80
- hasStore(store: Reducer<any>): boolean;
80
+ hasStore(store: Reducer<any> | string): boolean;
81
81
  registerStore(store: Reducer<any>): void;
82
82
  unregisterStore(store: Reducer<any>): void;
83
83
  }
@@ -66,7 +66,7 @@ Have you forgot to register reducer or add event handler in existing reducer?
66
66
  if (initialState) {
67
67
  this.rehydrate(initialState);
68
68
  }
69
- if (middlewares === null || middlewares === void 0 ? void 0 : middlewares.length) {
69
+ if (middlewares?.length) {
70
70
  this.applyDispatch = this.applyMiddlewares(middlewares);
71
71
  }
72
72
  // Инцииализируем уже имеющиеся сторы
@@ -268,12 +268,13 @@ Have you forgot to register reducer or add event handler in existing reducer?
268
268
  }
269
269
  return this.fullState;
270
270
  }
271
- // Для отложенной инициализации контекста, в будующем нужно удалить
271
+ // Для отложенной инициализации контекста, в будущем нужно удалить
272
272
  setContext(context) {
273
273
  this.context = context;
274
274
  }
275
275
  hasStore(store) {
276
- return store.storeName in this.storeInstances && this.dispatcher.hasStore(store);
276
+ const storeName = this.dispatcher.getStoreName(store);
277
+ return (storeName in this.storeInstances && this.dispatcher.hasStore({ storeName }));
277
278
  }
278
279
  registerStore(store) {
279
280
  this.dispatcher.registerStore(store);
@@ -74,7 +74,7 @@ Have you forgot to register reducer or add event handler in existing reducer?
74
74
  if (initialState) {
75
75
  this.rehydrate(initialState);
76
76
  }
77
- if (middlewares === null || middlewares === void 0 ? void 0 : middlewares.length) {
77
+ if (middlewares?.length) {
78
78
  this.applyDispatch = this.applyMiddlewares(middlewares);
79
79
  }
80
80
  // Инцииализируем уже имеющиеся сторы
@@ -276,12 +276,13 @@ Have you forgot to register reducer or add event handler in existing reducer?
276
276
  }
277
277
  return this.fullState;
278
278
  }
279
- // Для отложенной инициализации контекста, в будующем нужно удалить
279
+ // Для отложенной инициализации контекста, в будущем нужно удалить
280
280
  setContext(context) {
281
281
  this.context = context;
282
282
  }
283
283
  hasStore(store) {
284
- return store.storeName in this.storeInstances && this.dispatcher.hasStore(store);
284
+ const storeName = this.dispatcher.getStoreName(store);
285
+ return (storeName in this.storeInstances && this.dispatcher.hasStore({ storeName }));
285
286
  }
286
287
  registerStore(store) {
287
288
  this.dispatcher.registerStore(store);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/state",
3
- "version": "5.9.2",
3
+ "version": "5.14.9",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "dependencies": {
20
20
  "@tinkoff/react-hooks": "0.4.2",
21
21
  "@tinkoff/utils": "^2.1.2",
22
- "@tramvai/types-actions-state-context": "5.9.2",
22
+ "@tramvai/types-actions-state-context": "5.14.9",
23
23
  "@types/hoist-non-react-statics": "^3.3.1",
24
24
  "invariant": "^2.2.4",
25
25
  "react-is": ">=17",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@reatom/core": "^1.1.5",
37
- "@tramvai/core": "5.9.2",
37
+ "@tramvai/core": "5.14.9",
38
38
  "@types/invariant": "^2.2.31",
39
39
  "@types/react-is": "^17.0.0",
40
40
  "@types/use-sync-external-store": "^0.0.3",