@tramvai/state 5.50.0 → 5.53.74

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.
@@ -2,13 +2,16 @@
2
2
  // well as nesting subscriptions of descendant components, so that we can ensure the
3
3
  // ancestor components re-render before descendants
4
4
  class Subscription {
5
+ stores;
6
+ unsubscribe;
5
7
  constructor(stores) {
6
- this.handleStateChange = () => {
7
- this.onStateChange && this.onStateChange();
8
- };
9
8
  this.stores = stores;
10
9
  this.unsubscribe = undefined;
11
10
  }
11
+ onStateChange;
12
+ handleStateChange = () => {
13
+ this.onStateChange && this.onStateChange();
14
+ };
12
15
  isSubscribed() {
13
16
  return Boolean(this.unsubscribe);
14
17
  }
@@ -6,13 +6,16 @@ Object.defineProperty(exports, '__esModule', { value: true });
6
6
  // well as nesting subscriptions of descendant components, so that we can ensure the
7
7
  // ancestor components re-render before descendants
8
8
  class Subscription {
9
+ stores;
10
+ unsubscribe;
9
11
  constructor(stores) {
10
- this.handleStateChange = () => {
11
- this.onStateChange && this.onStateChange();
12
- };
13
12
  this.stores = stores;
14
13
  this.unsubscribe = undefined;
15
14
  }
15
+ onStateChange;
16
+ handleStateChange = () => {
17
+ this.onStateChange && this.onStateChange();
18
+ };
16
19
  isSubscribed() {
17
20
  return Boolean(this.unsubscribe);
18
21
  }
@@ -7,10 +7,14 @@ function createReducer(nameOrOptions, initialStateArg) {
7
7
  : { name: nameOrOptions, initialState: initialStateArg };
8
8
  const reducers = {};
9
9
  class ReducerStore extends SimpleEmitter {
10
+ static storeName = name;
11
+ state;
12
+ static events = undefined;
10
13
  constructor() {
11
14
  super();
12
15
  this.state = initialState;
13
16
  }
17
+ static handlers = {};
14
18
  static createEvents(model) {
15
19
  const eventNames = Object.keys(model);
16
20
  const events = {};
@@ -58,9 +62,6 @@ function createReducer(nameOrOptions, initialStateArg) {
58
62
  }
59
63
  }
60
64
  }
61
- ReducerStore.storeName = name;
62
- ReducerStore.events = undefined;
63
- ReducerStore.handlers = {};
64
65
  if (typeof nameOrOptions === 'object' && nameOrOptions.events) {
65
66
  ReducerStore.events = ReducerStore.createEvents(nameOrOptions.events);
66
67
  }
@@ -11,10 +11,14 @@ function createReducer(nameOrOptions, initialStateArg) {
11
11
  : { name: nameOrOptions, initialState: initialStateArg };
12
12
  const reducers = {};
13
13
  class ReducerStore extends SimpleEmitter.SimpleEmitter {
14
+ static storeName = name;
15
+ state;
16
+ static events = undefined;
14
17
  constructor() {
15
18
  super();
16
19
  this.state = initialState;
17
20
  }
21
+ static handlers = {};
18
22
  static createEvents(model) {
19
23
  const eventNames = Object.keys(model);
20
24
  const events = {};
@@ -62,9 +66,6 @@ function createReducer(nameOrOptions, initialStateArg) {
62
66
  }
63
67
  }
64
68
  }
65
- ReducerStore.storeName = name;
66
- ReducerStore.events = undefined;
67
- ReducerStore.handlers = {};
68
69
  if (typeof nameOrOptions === 'object' && nameOrOptions.events) {
69
70
  ReducerStore.events = ReducerStore.createEvents(nameOrOptions.events);
70
71
  }
@@ -29,6 +29,7 @@ export declare class ChildDispatcherContext<TContext> extends DispatcherContext<
29
29
  }): InstanceType<T> | null;
30
30
  registerStore(store: Reducer<any>): void;
31
31
  unregisterStore(store: Reducer<any>): void;
32
+ hasStore(store: Reducer<any> | string): boolean;
32
33
  _getParentAllowedStore(storeName: string): InstanceType<StoreClass> | null;
33
34
  _unsubscribeFromParentAllowedStore(storeName: string): void;
34
35
  }
@@ -1,12 +1,13 @@
1
1
  import { DispatcherContext } from './dispatcherContext.es.js';
2
2
 
3
3
  class ChildDispatcherContext extends DispatcherContext {
4
+ parentDispatcherContext;
5
+ allowedParentStores = new Set();
4
6
  /**
5
7
  * @param context The context to be used for store instances
6
8
  */
7
9
  constructor({ dispatcher, context, initialState, parentDispatcherContext, middlewares, parentAllowedStores, }) {
8
10
  super(dispatcher, context, initialState, middlewares);
9
- this.allowedParentStores = new Set();
10
11
  this.parentDispatcherContext = parentDispatcherContext;
11
12
  parentAllowedStores?.forEach((store) => {
12
13
  const storeName = this.dispatcher.getStoreName(typeof store === 'object' ? store.store : store);
@@ -57,6 +58,19 @@ class ChildDispatcherContext extends DispatcherContext {
57
58
  }
58
59
  super.unregisterStore(store);
59
60
  }
61
+ hasStore(store) {
62
+ const storeName = typeof store === 'string' ? store : store.storeName;
63
+ if (this.dispatcher.stores[storeName]) {
64
+ return super.hasStore(store);
65
+ }
66
+ if (this.allowedParentStores.has(storeName)) {
67
+ return !!this.parentDispatcherContext.getStore({
68
+ store: storeName,
69
+ optional: true,
70
+ });
71
+ }
72
+ return super.hasStore(store);
73
+ }
60
74
  _getParentAllowedStore(storeName) {
61
75
  // use just storeName to prevent store initialization on the root-app side
62
76
  const storeInstance = this.parentDispatcherContext.getStore({
@@ -5,12 +5,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var dispatcherContext = require('./dispatcherContext.js');
6
6
 
7
7
  class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
8
+ parentDispatcherContext;
9
+ allowedParentStores = new Set();
8
10
  /**
9
11
  * @param context The context to be used for store instances
10
12
  */
11
13
  constructor({ dispatcher, context, initialState, parentDispatcherContext, middlewares, parentAllowedStores, }) {
12
14
  super(dispatcher, context, initialState, middlewares);
13
- this.allowedParentStores = new Set();
14
15
  this.parentDispatcherContext = parentDispatcherContext;
15
16
  parentAllowedStores?.forEach((store) => {
16
17
  const storeName = this.dispatcher.getStoreName(typeof store === 'object' ? store.store : store);
@@ -61,6 +62,19 @@ class ChildDispatcherContext extends dispatcherContext.DispatcherContext {
61
62
  }
62
63
  super.unregisterStore(store);
63
64
  }
65
+ hasStore(store) {
66
+ const storeName = typeof store === 'string' ? store : store.storeName;
67
+ if (this.dispatcher.stores[storeName]) {
68
+ return super.hasStore(store);
69
+ }
70
+ if (this.allowedParentStores.has(storeName)) {
71
+ return !!this.parentDispatcherContext.getStore({
72
+ store: storeName,
73
+ optional: true,
74
+ });
75
+ }
76
+ return super.hasStore(store);
77
+ }
64
78
  _getParentAllowedStore(storeName) {
65
79
  // use just storeName to prevent store initialization on the root-app side
66
80
  const storeInstance = this.parentDispatcherContext.getStore({
@@ -1,6 +1,7 @@
1
1
  import { DispatcherContext } from './dispatcherContext.es.js';
2
2
 
3
3
  class Dispatcher {
4
+ stores;
4
5
  /**
5
6
  * @class Dispatcher
6
7
  * @param options Dispatcher options
@@ -18,6 +19,7 @@ class Dispatcher {
18
19
  this.registerStore(store);
19
20
  });
20
21
  }
22
+ handlers;
21
23
  createContext(context, initialState, middlewares) {
22
24
  return new DispatcherContext(this, context, initialState, middlewares);
23
25
  }
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var dispatcherContext = require('./dispatcherContext.js');
6
6
 
7
7
  class Dispatcher {
8
+ stores;
8
9
  /**
9
10
  * @class Dispatcher
10
11
  * @param options Dispatcher options
@@ -22,6 +23,7 @@ class Dispatcher {
22
23
  this.registerStore(store);
23
24
  });
24
25
  }
26
+ handlers;
25
27
  createContext(context, initialState, middlewares) {
26
28
  return new dispatcherContext.DispatcherContext(this, context, initialState, middlewares);
27
29
  }
@@ -26,32 +26,40 @@ const convertAction = (actionOrNameEvent, payload) => {
26
26
  };
27
27
  // Это форкнутый вариант dispatchr
28
28
  class DispatcherContext extends SimpleEmitter {
29
- /**
30
- * @param context The context to be used for store instances
31
- */
32
- constructor(dispatcher, context, initialState, middlewares) {
33
- super();
34
- this.applyDispatch = (event) => {
35
- let eventHandlers = this.dispatcher.handlers[event.type] || [];
36
- if (!eventHandlers.length) {
37
- if (event.store) {
38
- this.registerStore(event.store);
39
- eventHandlers = this.dispatcher.handlers[event.type] || [];
40
- }
41
- else {
42
- if (process.env.NODE_ENV === 'development') {
43
- // fallback to previous versions of createEvent
44
- console.warn(`
29
+ dispatcher;
30
+ storeInstances;
31
+ storeUnsubscribeCallbacks;
32
+ dispatcherInterface;
33
+ rehydratedStoreState;
34
+ // eslint-disable-next-line react/static-property-placement
35
+ context;
36
+ fullState;
37
+ applyDispatch = (event) => {
38
+ let eventHandlers = this.dispatcher.handlers[event.type] || [];
39
+ if (!eventHandlers.length) {
40
+ if (event.store) {
41
+ this.registerStore(event.store);
42
+ eventHandlers = this.dispatcher.handlers[event.type] || [];
43
+ }
44
+ else {
45
+ if (process.env.NODE_ENV === 'development') {
46
+ // fallback to previous versions of createEvent
47
+ console.warn(`
45
48
  The event "${event.type}" has been dispatched, but no reducers for this event were registered.
46
49
  Have you forgot to register reducer or add event handler in existing reducer?
47
50
  `);
48
- }
49
- return event.payload;
50
51
  }
52
+ return event.payload;
51
53
  }
52
- this.applyHandlers(event, eventHandlers);
53
- return event.payload;
54
- };
54
+ }
55
+ this.applyHandlers(event, eventHandlers);
56
+ return event.payload;
57
+ };
58
+ /**
59
+ * @param context The context to be used for store instances
60
+ */
61
+ constructor(dispatcher, context, initialState, middlewares) {
62
+ super();
55
63
  this.dispatcher = dispatcher;
56
64
  this.storeInstances = {};
57
65
  this.storeUnsubscribeCallbacks = {};
@@ -34,32 +34,40 @@ const convertAction = (actionOrNameEvent, payload) => {
34
34
  };
35
35
  // Это форкнутый вариант dispatchr
36
36
  class DispatcherContext extends SimpleEmitter.SimpleEmitter {
37
- /**
38
- * @param context The context to be used for store instances
39
- */
40
- constructor(dispatcher, context, initialState, middlewares) {
41
- super();
42
- this.applyDispatch = (event) => {
43
- let eventHandlers = this.dispatcher.handlers[event.type] || [];
44
- if (!eventHandlers.length) {
45
- if (event.store) {
46
- this.registerStore(event.store);
47
- eventHandlers = this.dispatcher.handlers[event.type] || [];
48
- }
49
- else {
50
- if (process.env.NODE_ENV === 'development') {
51
- // fallback to previous versions of createEvent
52
- console.warn(`
37
+ dispatcher;
38
+ storeInstances;
39
+ storeUnsubscribeCallbacks;
40
+ dispatcherInterface;
41
+ rehydratedStoreState;
42
+ // eslint-disable-next-line react/static-property-placement
43
+ context;
44
+ fullState;
45
+ applyDispatch = (event) => {
46
+ let eventHandlers = this.dispatcher.handlers[event.type] || [];
47
+ if (!eventHandlers.length) {
48
+ if (event.store) {
49
+ this.registerStore(event.store);
50
+ eventHandlers = this.dispatcher.handlers[event.type] || [];
51
+ }
52
+ else {
53
+ if (process.env.NODE_ENV === 'development') {
54
+ // fallback to previous versions of createEvent
55
+ console.warn(`
53
56
  The event "${event.type}" has been dispatched, but no reducers for this event were registered.
54
57
  Have you forgot to register reducer or add event handler in existing reducer?
55
58
  `);
56
- }
57
- return event.payload;
58
59
  }
60
+ return event.payload;
59
61
  }
60
- this.applyHandlers(event, eventHandlers);
61
- return event.payload;
62
- };
62
+ }
63
+ this.applyHandlers(event, eventHandlers);
64
+ return event.payload;
65
+ };
66
+ /**
67
+ * @param context The context to be used for store instances
68
+ */
69
+ constructor(dispatcher, context, initialState, middlewares) {
70
+ super();
63
71
  this.dispatcher = dispatcher;
64
72
  this.storeInstances = {};
65
73
  this.storeUnsubscribeCallbacks = {};
@@ -5,11 +5,11 @@ import { SimpleEmitter } from './SimpleEmitter.es.js';
5
5
  * @deprecated метод устарел, в замен этого API используй createReducer, который новее и лучше маштабируется
6
6
  */
7
7
  class BaseStore extends SimpleEmitter {
8
- constructor() {
9
- super(...arguments);
10
- this.state = Object.create(null);
11
- this.hydrateKeys = Object.create(null);
12
- }
8
+ static storeName;
9
+ state = Object.create(null);
10
+ hydrateKeys = Object.create(null);
11
+ dispatcher;
12
+ static handlers;
13
13
  getState() {
14
14
  return this.state;
15
15
  }
@@ -13,11 +13,11 @@ var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);
13
13
  * @deprecated метод устарел, в замен этого API используй createReducer, который новее и лучше маштабируется
14
14
  */
15
15
  class BaseStore extends SimpleEmitter.SimpleEmitter {
16
- constructor() {
17
- super(...arguments);
18
- this.state = Object.create(null);
19
- this.hydrateKeys = Object.create(null);
20
- }
16
+ static storeName;
17
+ state = Object.create(null);
18
+ hydrateKeys = Object.create(null);
19
+ dispatcher;
20
+ static handlers;
21
21
  getState() {
22
22
  return this.state;
23
23
  }
@@ -1,7 +1,5 @@
1
1
  class SimpleEmitter {
2
- constructor() {
3
- this.listeners = new Set();
4
- }
2
+ listeners = new Set();
5
3
  emit(name) {
6
4
  this.listeners.forEach((listener) => {
7
5
  listener();
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  class SimpleEmitter {
6
- constructor() {
7
- this.listeners = new Set();
8
- }
6
+ listeners = new Set();
9
7
  emit(name) {
10
8
  this.listeners.forEach((listener) => {
11
9
  listener();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/state",
3
- "version": "5.50.0",
3
+ "version": "5.53.74",
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.50.0",
22
+ "@tramvai/types-actions-state-context": "5.53.74",
23
23
  "@types/hoist-non-react-statics": "^3.3.6",
24
24
  "invariant": "^2.2.4",
25
25
  "react-is": ">=17",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@reatom/core": "^1.1.5",
36
- "@tramvai/core": "5.50.0",
36
+ "@tramvai/core": "5.53.74",
37
37
  "@types/invariant": "^2.2.31",
38
38
  "@types/react-is": "^17.0.0",
39
39
  "@types/use-sync-external-store": "^0.0.3",