@tramvai/state 1.35.1 → 1.35.10

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/lib/index.es.js CHANGED
@@ -633,48 +633,45 @@ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffec
633
633
 
634
634
  function useStore(reducer) {
635
635
  const context = useConsumerContext();
636
- const stateRef = useRef();
637
636
  const reducerRef = useRef(reducer);
638
637
  const addedReducerRef = useRef(null);
639
638
  const unsubscribeRef = useRef(noop);
640
639
  const [, forceRender] = useReducer((s) => s + 1, 0);
641
- useMemo(() => {
642
- // отписываемся от обновлений текущего редьюсера
643
- unsubscribeRef.current();
644
- // если мы получили новый редьюсер
645
- if (reducer.storeName !== reducerRef.current.storeName) {
640
+ // если текущий редьюсер не зарегистрирован в диспетчере,
641
+ // регистрируем его вручную, что бы гарантировать работоспособность `context.getState(reducer)`,
642
+ // и сохраняем в `addedReducerRef`, что бы удалить при unmount
643
+ if (!context.hasStore(reducer)) {
644
+ context.registerStore(reducer);
645
+ addedReducerRef.current = reducer.storeName;
646
+ }
647
+ const stateRef = useRef(context.getState(reducer));
648
+ useIsomorphicLayoutEffect(() => {
649
+ const subscribe = (updatedState) => {
650
+ // если состояние текущего редьюсера изменилось,
651
+ // обновляем локальное состояние и ререндерим компонент
652
+ if (stateRef.current !== updatedState) {
653
+ stateRef.current = updatedState;
654
+ forceRender();
655
+ }
656
+ };
657
+ // сразу обновляем состояние
658
+ subscribe(context.getState(reducer));
659
+ // и подписываемся на обновления редьюсера
660
+ unsubscribeRef.current = context.subscribe(reducer, subscribe);
661
+ // заменяем текущий редьюсер
662
+ reducerRef.current = reducer;
663
+ return () => {
664
+ // гарантируем отписку от обновлений текущего редьюсера,
665
+ // при анмаунте компонента
666
+ unsubscribeRef.current();
646
667
  // если текущий редьюсер был зарегистрирован в диспетчере в этом хуке,
647
668
  // удаляем его из диспетчера
648
- if (addedReducerRef.current === reducerRef.current.storeName) {
669
+ if (addedReducerRef.current) {
649
670
  context.unregisterStore(reducerRef.current);
650
671
  addedReducerRef.current = null;
651
672
  }
652
- // заменяем текущий редьюсер
653
- reducerRef.current = reducer;
654
- }
655
- // если текущий редьюсер не зарегистрирован в диспетчере,
656
- // регистрируем его вручную
657
- if (!context.hasStore(reducerRef.current)) {
658
- context.registerStore(reducerRef.current);
659
- addedReducerRef.current = reducerRef.current.storeName;
660
- }
661
- // подписываемся на обновления текущего редьюсера,
662
- // если состояние текущего редьюсера изменилось,
663
- // ререндерим компонент
664
- unsubscribeRef.current = context.subscribe(reducerRef.current, (nextState) => {
665
- stateRef.current = nextState;
666
- forceRender();
667
- });
668
- // сохраняем состояние текущего редьюсера
669
- stateRef.current = context.getState(reducerRef.current);
670
- }, [reducer, context]);
671
- // гарантируем отписку от обновлений текущего редьюсера,
672
- // при анмаунте компонента
673
- useIsomorphicLayoutEffect(() => {
674
- return () => {
675
- unsubscribeRef.current();
676
673
  };
677
- }, []);
674
+ }, [reducer, context]);
678
675
  return stateRef.current;
679
676
  }
680
677
 
package/lib/index.js CHANGED
@@ -656,48 +656,45 @@ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayou
656
656
 
657
657
  function useStore(reducer) {
658
658
  const context = useConsumerContext();
659
- const stateRef = React.useRef();
660
659
  const reducerRef = React.useRef(reducer);
661
660
  const addedReducerRef = React.useRef(null);
662
661
  const unsubscribeRef = React.useRef(noop__default["default"]);
663
662
  const [, forceRender] = React.useReducer((s) => s + 1, 0);
664
- React.useMemo(() => {
665
- // отписываемся от обновлений текущего редьюсера
666
- unsubscribeRef.current();
667
- // если мы получили новый редьюсер
668
- if (reducer.storeName !== reducerRef.current.storeName) {
663
+ // если текущий редьюсер не зарегистрирован в диспетчере,
664
+ // регистрируем его вручную, что бы гарантировать работоспособность `context.getState(reducer)`,
665
+ // и сохраняем в `addedReducerRef`, что бы удалить при unmount
666
+ if (!context.hasStore(reducer)) {
667
+ context.registerStore(reducer);
668
+ addedReducerRef.current = reducer.storeName;
669
+ }
670
+ const stateRef = React.useRef(context.getState(reducer));
671
+ useIsomorphicLayoutEffect(() => {
672
+ const subscribe = (updatedState) => {
673
+ // если состояние текущего редьюсера изменилось,
674
+ // обновляем локальное состояние и ререндерим компонент
675
+ if (stateRef.current !== updatedState) {
676
+ stateRef.current = updatedState;
677
+ forceRender();
678
+ }
679
+ };
680
+ // сразу обновляем состояние
681
+ subscribe(context.getState(reducer));
682
+ // и подписываемся на обновления редьюсера
683
+ unsubscribeRef.current = context.subscribe(reducer, subscribe);
684
+ // заменяем текущий редьюсер
685
+ reducerRef.current = reducer;
686
+ return () => {
687
+ // гарантируем отписку от обновлений текущего редьюсера,
688
+ // при анмаунте компонента
689
+ unsubscribeRef.current();
669
690
  // если текущий редьюсер был зарегистрирован в диспетчере в этом хуке,
670
691
  // удаляем его из диспетчера
671
- if (addedReducerRef.current === reducerRef.current.storeName) {
692
+ if (addedReducerRef.current) {
672
693
  context.unregisterStore(reducerRef.current);
673
694
  addedReducerRef.current = null;
674
695
  }
675
- // заменяем текущий редьюсер
676
- reducerRef.current = reducer;
677
- }
678
- // если текущий редьюсер не зарегистрирован в диспетчере,
679
- // регистрируем его вручную
680
- if (!context.hasStore(reducerRef.current)) {
681
- context.registerStore(reducerRef.current);
682
- addedReducerRef.current = reducerRef.current.storeName;
683
- }
684
- // подписываемся на обновления текущего редьюсера,
685
- // если состояние текущего редьюсера изменилось,
686
- // ререндерим компонент
687
- unsubscribeRef.current = context.subscribe(reducerRef.current, (nextState) => {
688
- stateRef.current = nextState;
689
- forceRender();
690
- });
691
- // сохраняем состояние текущего редьюсера
692
- stateRef.current = context.getState(reducerRef.current);
693
- }, [reducer, context]);
694
- // гарантируем отписку от обновлений текущего редьюсера,
695
- // при анмаунте компонента
696
- useIsomorphicLayoutEffect(() => {
697
- return () => {
698
- unsubscribeRef.current();
699
696
  };
700
- }, []);
697
+ }, [reducer, context]);
701
698
  return stateRef.current;
702
699
  }
703
700
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/state",
3
- "version": "1.35.1",
3
+ "version": "1.35.10",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "dependencies": {
21
21
  "@tinkoff/react-hooks": "0.0.23",
22
22
  "@tinkoff/utils": "^2.1.2",
23
- "@tramvai/types-actions-state-context": "1.35.1",
23
+ "@tramvai/types-actions-state-context": "1.35.10",
24
24
  "@types/hoist-non-react-statics": "^3.3.1",
25
25
  "invariant": "^2.2.4",
26
26
  "react-is": "^17.0.1",