@tramvai/state 4.19.5 → 4.20.7

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.
@@ -28,7 +28,9 @@ export declare class ChildDispatcherContext<TContext> extends DispatcherContext<
28
28
  optional: true;
29
29
  }): InstanceType<T> | null;
30
30
  registerStore(store: Reducer<any>): void;
31
+ unregisterStore(store: Reducer<any>): void;
31
32
  _getParentAllowedStore(storeName: string): InstanceType<StoreClass> | null;
33
+ _unsubscribeFromParentAllowedStore(storeName: string): void;
32
34
  }
33
35
  export {};
34
36
  //# sourceMappingURL=childDispatcherContext.d.ts.map
@@ -45,6 +45,18 @@ class ChildDispatcherContext extends DispatcherContext {
45
45
  }
46
46
  super.registerStore(store);
47
47
  }
48
+ unregisterStore(store) {
49
+ const { storeName } = store;
50
+ if (this.dispatcher.stores[storeName]) {
51
+ super.unregisterStore(store);
52
+ return;
53
+ }
54
+ if (this.allowedParentStores.has(storeName)) {
55
+ this._unsubscribeFromParentAllowedStore(storeName);
56
+ return;
57
+ }
58
+ super.unregisterStore(store);
59
+ }
48
60
  _getParentAllowedStore(storeName) {
49
61
  // use just storeName to prevent store initialization on the root-app side
50
62
  const storeInstance = this.parentDispatcherContext.getStore({
@@ -57,6 +69,19 @@ class ChildDispatcherContext extends DispatcherContext {
57
69
  this.storeSubscribe(storeName, storeInstance);
58
70
  return storeInstance;
59
71
  }
72
+ _unsubscribeFromParentAllowedStore(storeName) {
73
+ var _a, _b;
74
+ const storeInstance = this.parentDispatcherContext.getStore({
75
+ store: storeName,
76
+ optional: true,
77
+ });
78
+ if (storeInstance) {
79
+ // in strict mode, unsubscribe callback in `useStore` is fired twice, with same `addedReducerRef.current` value,
80
+ // 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);
82
+ delete this.storeUnsubscribeCallbacks[storeName];
83
+ }
84
+ }
60
85
  }
61
86
 
62
87
  export { ChildDispatcherContext };
@@ -49,6 +49,18 @@ class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
49
49
  }
50
50
  super.registerStore(store);
51
51
  }
52
+ unregisterStore(store) {
53
+ const { storeName } = store;
54
+ if (this.dispatcher.stores[storeName]) {
55
+ super.unregisterStore(store);
56
+ return;
57
+ }
58
+ if (this.allowedParentStores.has(storeName)) {
59
+ this._unsubscribeFromParentAllowedStore(storeName);
60
+ return;
61
+ }
62
+ super.unregisterStore(store);
63
+ }
52
64
  _getParentAllowedStore(storeName) {
53
65
  // use just storeName to prevent store initialization on the root-app side
54
66
  const storeInstance = this.parentDispatcherContext.getStore({
@@ -61,6 +73,19 @@ class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
61
73
  this.storeSubscribe(storeName, storeInstance);
62
74
  return storeInstance;
63
75
  }
76
+ _unsubscribeFromParentAllowedStore(storeName) {
77
+ var _a, _b;
78
+ const storeInstance = this.parentDispatcherContext.getStore({
79
+ store: storeName,
80
+ optional: true,
81
+ });
82
+ if (storeInstance) {
83
+ // in strict mode, unsubscribe callback in `useStore` is fired twice, with same `addedReducerRef.current` value,
84
+ // 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);
86
+ delete this.storeUnsubscribeCallbacks[storeName];
87
+ }
88
+ }
64
89
  }
65
90
 
66
91
  exports.ChildDispatcherContext = ChildDispatcherContext;
@@ -87,6 +87,11 @@ Have you forgot to register reducer or add event handler in existing reducer?
87
87
  return dispatch;
88
88
  }
89
89
  storeSubscribe(storeName, storeInstance) {
90
+ // prevent multiple subscriptions in internal reducer event emitter,
91
+ // because only last will be saved here in dispatcher and can be unsubscribed
92
+ if (this.storeUnsubscribeCallbacks[storeName]) {
93
+ return;
94
+ }
90
95
  const subscribeHandler = () => {
91
96
  const newState = storeInstance.getState();
92
97
  if (newState !== this.fullState[storeName]) {
@@ -95,6 +95,11 @@ Have you forgot to register reducer or add event handler in existing reducer?
95
95
  return dispatch;
96
96
  }
97
97
  storeSubscribe(storeName, storeInstance) {
98
+ // prevent multiple subscriptions in internal reducer event emitter,
99
+ // because only last will be saved here in dispatcher and can be unsubscribed
100
+ if (this.storeUnsubscribeCallbacks[storeName]) {
101
+ return;
102
+ }
98
103
  const subscribeHandler = () => {
99
104
  const newState = storeInstance.getState();
100
105
  if (newState !== this.fullState[storeName]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/state",
3
- "version": "4.19.5",
3
+ "version": "4.20.7",
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.3.1",
21
21
  "@tinkoff/utils": "^2.1.2",
22
- "@tramvai/types-actions-state-context": "4.19.5",
22
+ "@tramvai/types-actions-state-context": "4.20.7",
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": "4.19.5",
37
+ "@tramvai/core": "4.20.7",
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",