@ngxs/store 3.8.1-dev.master-5942e46 → 3.8.1-dev.master-9b1dc3e

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.
@@ -48,6 +48,18 @@ function getZoneWarningMessage() {
48
48
  function getUndecoratedStateInIvyWarningMessage(name) {
49
49
  return `'${name}' class should be decorated with @Injectable() right after the @State() decorator`;
50
50
  }
51
+ function getInvalidInitializationOrderMessage(addedStates) {
52
+ let message = 'You have an invalid state initialization order. This typically occurs when `NgxsModule.forFeature`\n' +
53
+ 'or `provideStates` is called before `NgxsModule.forRoot` or `provideStore`.\n' +
54
+ 'One example is when `NgxsRouterPluginModule.forRoot` is called before `NgxsModule.forRoot`.';
55
+ if (addedStates) {
56
+ const stateNames = Object.keys(addedStates).map(stateName => `"${stateName}"`);
57
+ message +=
58
+ '\nFeature states added before the store initialization is complete: ' +
59
+ `${stateNames.join(', ')}.`;
60
+ }
61
+ return message;
62
+ }
51
63
  function throwSelectFactoryNotConnectedError() {
52
64
  throw new Error('You have forgotten to import the NGXS module!');
53
65
  }
@@ -111,18 +123,18 @@ function verifyZoneIsNotNooped(ngZone) {
111
123
  console.warn(getZoneWarningMessage());
112
124
  }
113
125
 
114
- const NG_DEV_MODE$3 = typeof ngDevMode === 'undefined' || ngDevMode;
126
+ const NG_DEV_MODE$4 = typeof ngDevMode === 'undefined' || ngDevMode;
115
127
  /**
116
128
  * Consumers have the option to utilize the execution strategy provided by
117
129
  * `NgxsModule.forRoot({executionStrategy})` or `provideStore([], {executionStrategy})`.
118
130
  */
119
- const CUSTOM_NGXS_EXECUTION_STRATEGY = new InjectionToken(NG_DEV_MODE$3 ? 'CUSTOM_NGXS_EXECUTION_STRATEGY' : '');
131
+ const CUSTOM_NGXS_EXECUTION_STRATEGY = new InjectionToken(NG_DEV_MODE$4 ? 'CUSTOM_NGXS_EXECUTION_STRATEGY' : '');
120
132
  /**
121
133
  * The injection token is used internally to resolve an instance of the execution
122
134
  * strategy. It checks whether consumers have provided their own `executionStrategy`
123
135
  * and also verifies if we are operating in a zone-aware environment.
124
136
  */
125
- const NGXS_EXECUTION_STRATEGY = new InjectionToken(NG_DEV_MODE$3 ? 'NGXS_EXECUTION_STRATEGY' : '', {
137
+ const NGXS_EXECUTION_STRATEGY = new InjectionToken(NG_DEV_MODE$4 ? 'NGXS_EXECUTION_STRATEGY' : '', {
126
138
  providedIn: 'root',
127
139
  factory: () => {
128
140
  const injector = inject(INJECTOR);
@@ -515,21 +527,21 @@ const mergeDeep = (base, ...sources) => {
515
527
  return mergeDeep(base, ...sources);
516
528
  };
517
529
 
518
- const NG_DEV_MODE$2 = typeof ngDevMode === 'undefined' || ngDevMode;
530
+ const NG_DEV_MODE$3 = typeof ngDevMode === 'undefined' || ngDevMode;
519
531
  // The injection token is used to resolve a list of states provided at
520
532
  // the root level through either `NgxsModule.forRoot` or `provideStore`.
521
- const ROOT_STATE_TOKEN = new InjectionToken(NG_DEV_MODE$2 ? 'ROOT_STATE_TOKEN' : '');
533
+ const ROOT_STATE_TOKEN = new InjectionToken(NG_DEV_MODE$3 ? 'ROOT_STATE_TOKEN' : '');
522
534
  // The injection token is used to resolve a list of states provided at
523
535
  // the feature level through either `NgxsModule.forFeature` or `provideStates`.
524
536
  // The Array<Array> is used to overload the resolved value of the token because
525
537
  // it is a multi-provider token.
526
- const FEATURE_STATE_TOKEN = new InjectionToken(NG_DEV_MODE$2 ? 'FEATURE_STATE_TOKEN' : '');
538
+ const FEATURE_STATE_TOKEN = new InjectionToken(NG_DEV_MODE$3 ? 'FEATURE_STATE_TOKEN' : '');
527
539
  // The injection token is used to resolve to custom NGXS plugins provided
528
540
  // at the root level through either `{provide}` scheme or `withNgxsPlugin`.
529
- const NGXS_PLUGINS = new InjectionToken(NG_DEV_MODE$2 ? 'NGXS_PLUGINS' : '');
541
+ const NGXS_PLUGINS = new InjectionToken(NG_DEV_MODE$3 ? 'NGXS_PLUGINS' : '');
530
542
  // The injection token is used to resolve to options provided at the root
531
543
  // level through either `NgxsModule.forRoot` or `provideStore`.
532
- const NGXS_OPTIONS = new InjectionToken(NG_DEV_MODE$2 ? 'NGXS_OPTIONS' : '');
544
+ const NGXS_OPTIONS = new InjectionToken(NG_DEV_MODE$3 ? 'NGXS_OPTIONS' : '');
533
545
  /**
534
546
  * The NGXS config settings.
535
547
  */
@@ -1380,7 +1392,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImpor
1380
1392
  args: [NGXS_DEVELOPMENT_OPTIONS]
1381
1393
  }] }]; } });
1382
1394
 
1383
- const NG_DEV_MODE$1 = typeof ngDevMode === 'undefined' || ngDevMode;
1395
+ const NG_DEV_MODE$2 = typeof ngDevMode === 'undefined' || ngDevMode;
1384
1396
  /**
1385
1397
  * The `StateFactory` class adds root and feature states to the graph.
1386
1398
  * This extracts state names from state classes, checks if they already
@@ -1472,7 +1484,7 @@ class StateFactory {
1472
1484
  * Add a new state to the global defs.
1473
1485
  */
1474
1486
  add(stateClasses) {
1475
- if (NG_DEV_MODE$1) {
1487
+ if (NG_DEV_MODE$2) {
1476
1488
  ensureStatesAreDecorated(stateClasses);
1477
1489
  }
1478
1490
  const { newStates } = this.addToStatesMap(stateClasses);
@@ -1492,7 +1504,7 @@ class StateFactory {
1492
1504
  // `State` decorator. This check is moved here because the `ɵprov` property
1493
1505
  // will not exist on the class in JIT mode (because it's set asynchronously
1494
1506
  // during JIT compilation through `Object.defineProperty`).
1495
- if (NG_DEV_MODE$1) {
1507
+ if (NG_DEV_MODE$2) {
1496
1508
  ensureStateClassIsInjectable(stateClass);
1497
1509
  }
1498
1510
  const stateMap = {
@@ -1594,7 +1606,7 @@ class StateFactory {
1594
1606
  }
1595
1607
  // The `NgxsUnhandledActionsLogger` is a tree-shakable class which functions
1596
1608
  // only during development.
1597
- if (NG_DEV_MODE$1 && !actionHasBeenHandled) {
1609
+ if (NG_DEV_MODE$2 && !actionHasBeenHandled) {
1598
1610
  const unhandledActionsLogger = this._injector.get(NgxsUnhandledActionsLogger, null);
1599
1611
  // The `NgxsUnhandledActionsLogger` will not be resolved by the injector if the
1600
1612
  // `NgxsDevelopmentModule` is not provided. It's enough to check whether the `injector.get`
@@ -1613,7 +1625,7 @@ class StateFactory {
1613
1625
  const statesMap = this.statesByName;
1614
1626
  for (const stateClass of stateClasses) {
1615
1627
  const stateName = getStoreMetadata$1(stateClass).name;
1616
- if (NG_DEV_MODE$1) {
1628
+ if (NG_DEV_MODE$2) {
1617
1629
  ensureStateNameIsUnique(stateName, stateClass, statesMap);
1618
1630
  }
1619
1631
  const unmountedState = !statesMap[stateName];
@@ -1767,6 +1779,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImpor
1767
1779
  args: [{ providedIn: 'root' }]
1768
1780
  }], ctorParameters: function () { return [{ type: Store }, { type: NgxsConfig }]; } });
1769
1781
 
1782
+ const NG_DEV_MODE$1 = typeof ngDevMode === 'undefined' || ngDevMode;
1770
1783
  class LifecycleStateManager {
1771
1784
  constructor(_store, _internalErrorReporter, _internalStateOperations, _stateContextFactory, _bootstrapper) {
1772
1785
  this._store = _store;
@@ -1780,6 +1793,21 @@ class LifecycleStateManager {
1780
1793
  this._destroy$.next();
1781
1794
  }
1782
1795
  ngxsBootstrap(action, results) {
1796
+ if (NG_DEV_MODE$1) {
1797
+ if (action instanceof InitState) {
1798
+ this._initStateHasBeenDispatched = true;
1799
+ }
1800
+ else if (
1801
+ // This is a dev mode-only check that ensures the correct order of
1802
+ // state initialization. The `NgxsModule.forRoot` or `provideStore` should
1803
+ // always come first, followed by `forFeature` and `provideStates`. If the
1804
+ // `UpdateState` is dispatched before the `InitState` is dispatched, it indicates
1805
+ // that modules or providers are in an invalid order.
1806
+ action instanceof UpdateState &&
1807
+ !this._initStateHasBeenDispatched) {
1808
+ console.error(getInvalidInitializationOrderMessage(action.addedStates));
1809
+ }
1810
+ }
1783
1811
  this._internalStateOperations
1784
1812
  .getRootStateOperations()
1785
1813
  .dispatch(action)