@ngxs/store 18.1.5-dev.master-cd94934 → 18.1.5-dev.master-a4f3a7f

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.
@@ -258,19 +258,26 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImpor
258
258
  }], ctorParameters: () => [] });
259
259
 
260
260
  const ɵɵunhandledRxjsErrorCallbacks = new WeakMap();
261
- const existingHandler = config.onUnhandledError;
262
- config.onUnhandledError = function (error) {
263
- const unhandledErrorCallback = ɵɵunhandledRxjsErrorCallbacks.get(error);
264
- if (unhandledErrorCallback) {
265
- unhandledErrorCallback();
266
- }
267
- else if (existingHandler) {
268
- existingHandler.call(this, error);
269
- }
270
- else {
271
- throw error;
261
+ let installed = false;
262
+ function installOnUnhandhedErrorHandler() {
263
+ if (installed) {
264
+ return;
272
265
  }
273
- };
266
+ const existingHandler = config.onUnhandledError;
267
+ config.onUnhandledError = function (error) {
268
+ const unhandledErrorCallback = ɵɵunhandledRxjsErrorCallbacks.get(error);
269
+ if (unhandledErrorCallback) {
270
+ unhandledErrorCallback();
271
+ }
272
+ else if (existingHandler) {
273
+ existingHandler.call(this, error);
274
+ }
275
+ else {
276
+ throw error;
277
+ }
278
+ };
279
+ installed = true;
280
+ }
274
281
  function executeUnhandledCallback(error) {
275
282
  const unhandledErrorCallback = ɵɵunhandledRxjsErrorCallbacks.get(error);
276
283
  if (unhandledErrorCallback) {
@@ -1241,20 +1248,17 @@ function cloneDefaults(defaults) {
1241
1248
  * The `StateFactory` class adds root and feature states to the graph.
1242
1249
  * This extracts state names from state classes, checks if they already
1243
1250
  * exist in the global graph, throws errors if their names are invalid, etc.
1244
- * See its constructor, state factories inject state factories that are
1245
- * parent-level providers. This is required to get feature states from the
1246
- * injector on the same level.
1247
1251
  *
1248
- * The `NgxsModule.forFeature(...)` returns `providers: [StateFactory, ...states]`.
1249
- * The `StateFactory` is initialized on the feature level and goes through `...states`
1250
- * to get them from the injector through `injector.get(state)`.
1252
+ * Root and feature initializers call `addAndReturnDefaults()` to add those states
1253
+ * to the global graph. Since `addAndReturnDefaults` runs within the injection
1254
+ * context (which might be the root injector or a feature injector), we can
1255
+ * retrieve an instance of the state class using `inject(StateClass)`.
1251
1256
  * @ignore
1252
1257
  */
1253
1258
  class StateFactory {
1254
1259
  constructor() {
1255
1260
  this._injector = inject(Injector);
1256
1261
  this._config = inject(NgxsConfig);
1257
- this._parentFactory = inject(StateFactory, { optional: true, skipSelf: true });
1258
1262
  this._stateContextFactory = inject(StateContextFactory);
1259
1263
  this._actions = inject(InternalActions);
1260
1264
  this._actionResults = inject(InternalDispatchedActionResults);
@@ -1271,47 +1275,36 @@ class StateFactory {
1271
1275
  const stateFactory = this;
1272
1276
  const propGetter = stateFactory._propGetter;
1273
1277
  function resolveGetter(key) {
1274
- const path = stateFactory.statePaths[key];
1278
+ const path = stateFactory._statePaths[key];
1275
1279
  return path ? propGetter(path.split('.')) : null;
1276
1280
  }
1277
- const context = this._parentFactory
1278
- ? this._parentFactory.getRuntimeSelectorContext()
1279
- : {
1280
- getStateGetter(key) {
1281
- // Use `@__INLINE__` annotation to forcely inline `resolveGetter`.
1282
- // This is a Terser annotation, which will function only in the production mode.
1283
- let getter = /*@__INLINE__*/ resolveGetter(key);
1284
- if (getter) {
1285
- return getter;
1286
- }
1287
- return (...args) => {
1288
- // Late loaded getter
1289
- if (!getter) {
1290
- getter = /*@__INLINE__*/ resolveGetter(key);
1291
- }
1292
- return getter ? getter(...args) : undefined;
1293
- };
1294
- },
1295
- getSelectorOptions(localOptions) {
1296
- const globalSelectorOptions = stateFactory._config.selectorOptions;
1297
- return {
1298
- ...globalSelectorOptions,
1299
- ...(localOptions || {})
1300
- };
1281
+ const context = {
1282
+ getStateGetter(key) {
1283
+ // Use `@__INLINE__` annotation to forcely inline `resolveGetter`.
1284
+ // This is a Terser annotation, which will function only in the production mode.
1285
+ let getter = /*@__INLINE__*/ resolveGetter(key);
1286
+ if (getter) {
1287
+ return getter;
1301
1288
  }
1302
- };
1289
+ return (...args) => {
1290
+ // Late loaded getter
1291
+ if (!getter) {
1292
+ getter = /*@__INLINE__*/ resolveGetter(key);
1293
+ }
1294
+ return getter ? getter(...args) : undefined;
1295
+ };
1296
+ },
1297
+ getSelectorOptions(localOptions) {
1298
+ const globalSelectorOptions = stateFactory._config.selectorOptions;
1299
+ return {
1300
+ ...globalSelectorOptions,
1301
+ ...(localOptions || {})
1302
+ };
1303
+ }
1304
+ };
1303
1305
  return context;
1304
1306
  });
1305
1307
  }
1306
- get states() {
1307
- return this._parentFactory ? this._parentFactory.states : this._states;
1308
- }
1309
- get statesByName() {
1310
- return this._parentFactory ? this._parentFactory.statesByName : this._statesByName;
1311
- }
1312
- get statePaths() {
1313
- return this._parentFactory ? this._parentFactory.statePaths : this._statePaths;
1314
- }
1315
1308
  ngOnDestroy() {
1316
1309
  this._actionsSubscription?.unsubscribe();
1317
1310
  }
@@ -1347,7 +1340,7 @@ class StateFactory {
1347
1340
  path,
1348
1341
  isInitialised: false,
1349
1342
  actions: meta.actions,
1350
- instance: this._injector.get(stateClass),
1343
+ instance: inject(stateClass),
1351
1344
  defaults: cloneDefaults(meta.defaults)
1352
1345
  };
1353
1346
  // ensure our store hasn't already been added
@@ -1356,7 +1349,7 @@ class StateFactory {
1356
1349
  if (!this.hasBeenMountedAndBootstrapped(name, path)) {
1357
1350
  bootstrappedStores.push(stateMap);
1358
1351
  }
1359
- this.states.push(stateMap);
1352
+ this._states.push(stateMap);
1360
1353
  this.hydrateActionMetasMap(stateMap);
1361
1354
  }
1362
1355
  return bootstrappedStores;
@@ -1371,12 +1364,6 @@ class StateFactory {
1371
1364
  return { defaults, states: mappedStores };
1372
1365
  }
1373
1366
  connectActionHandlers() {
1374
- // Note: We have to connect actions only once when the `StateFactory`
1375
- // is being created for the first time. This checks if we're in
1376
- // a child state factory and the parent state factory already exists.
1377
- if (this._parentFactory || this._actionsSubscription !== null) {
1378
- return;
1379
- }
1380
1367
  this._actionsSubscription = this._actions
1381
1368
  .pipe(filter((ctx) => ctx.status === "DISPATCHED" /* ActionStatus.Dispatched */), mergeMap$1(ctx => {
1382
1369
  const action = ctx.action;
@@ -1432,7 +1419,7 @@ class StateFactory {
1432
1419
  }
1433
1420
  addToStatesMap(stateClasses) {
1434
1421
  const newStates = [];
1435
- const statesMap = this.statesByName;
1422
+ const statesMap = this._statesByName;
1436
1423
  for (const stateClass of stateClasses) {
1437
1424
  const stateName = _getStoreMetadata(stateClass).name;
1438
1425
  if (typeof ngDevMode !== 'undefined' && ngDevMode) {
@@ -1447,7 +1434,7 @@ class StateFactory {
1447
1434
  return { newStates };
1448
1435
  }
1449
1436
  addRuntimeInfoToMeta(meta, path) {
1450
- this.statePaths[meta.name] = path;
1437
+ this._statePaths[meta.name] = path;
1451
1438
  // TODO: versions after v3 - we plan to get rid of the `path` property because it is non-deterministic
1452
1439
  // we can do this when we get rid of the incorrectly exposed getStoreMetadata
1453
1440
  // We will need to come up with an alternative to what was exposed in v3 because this is used by many plugins
@@ -1457,7 +1444,7 @@ class StateFactory {
1457
1444
  const valueIsBootstrappedInInitialState = getValue(this._initialState, path) !== undefined;
1458
1445
  // This checks whether a state has been already added to the global graph and
1459
1446
  // its lifecycle is in 'bootstrapped' state.
1460
- return this.statesByName[name] && valueIsBootstrappedInInitialState;
1447
+ return this._statesByName[name] && valueIsBootstrappedInInitialState;
1461
1448
  }
1462
1449
  hydrateActionMetasMap({ path, actions, instance }) {
1463
1450
  const { dispatched$ } = this._actions;
@@ -1528,10 +1515,11 @@ class StateFactory {
1528
1515
  }
1529
1516
  }
1530
1517
  /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: StateFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1531
- /** @nocollapse */ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: StateFactory }); }
1518
+ /** @nocollapse */ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: StateFactory, providedIn: 'root' }); }
1532
1519
  }
1533
1520
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: StateFactory, decorators: [{
1534
- type: Injectable
1521
+ type: Injectable,
1522
+ args: [{ providedIn: 'root' }]
1535
1523
  }] });
1536
1524
  // This is used to replace `setState` and `patchState` once the action
1537
1525
  // handler has been unsubscribed or completed, to prevent writing
@@ -1774,6 +1762,11 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImpor
1774
1762
  * same initialization functionality.
1775
1763
  */
1776
1764
  function rootStoreInitializer() {
1765
+ // Override the RxJS `config.onUnhandledError` within the root store initializer,
1766
+ // but only after other code has already executed.
1767
+ // If users have a custom `config.onUnhandledError`, we might overwrite it too
1768
+ // early and capture the original `config.onUnhandledError` before it is properly set.
1769
+ installOnUnhandhedErrorHandler();
1777
1770
  const prebootFns = inject(NGXS_PREBOOT_FNS, { optional: true }) || [];
1778
1771
  prebootFns.forEach(prebootFn => prebootFn());
1779
1772
  const factory = inject(StateFactory);
@@ -1883,7 +1876,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImpor
1883
1876
  */
1884
1877
  function getRootProviders(states, options) {
1885
1878
  return [
1886
- StateFactory,
1887
1879
  ...states,
1888
1880
  {
1889
1881
  provide: ROOT_STATE_TOKEN,
@@ -1914,7 +1906,6 @@ function getRootProviders(states, options) {
1914
1906
  */
1915
1907
  function getFeatureProviders(states) {
1916
1908
  return [
1917
- StateFactory,
1918
1909
  PluginManager,
1919
1910
  ...states,
1920
1911
  {