@ngxs/store 3.8.1-dev.master-3e21e23 → 3.8.1-dev.master-94c82c4

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.
@@ -5,6 +5,7 @@ import { memoize, INITIAL_STATE_TOKEN, NgxsBootstrapper, ɵNGXS_STATE_CONTEXT_FA
5
5
  import { isPlatformServer } from '@angular/common';
6
6
  import { Observable, Subject, BehaviorSubject, of, forkJoin, throwError, EMPTY, from, isObservable } from 'rxjs';
7
7
  import { filter, map, share, shareReplay, take, exhaustMap, mergeMap, defaultIfEmpty, catchError, takeUntil, distinctUntilChanged, tap, startWith, pairwise } from 'rxjs/operators';
8
+ import { isStateOperator } from '@ngxs/store/operators';
8
9
 
9
10
  /**
10
11
  * Returns the type from an action instance/class.
@@ -1137,43 +1138,21 @@ class StateContextFactory {
1137
1138
  */
1138
1139
  createStateContext(mappedStore) {
1139
1140
  const root = this._internalStateOperations.getRootStateOperations();
1140
- function getState(currentAppState) {
1141
- return getValue(currentAppState, mappedStore.path);
1142
- }
1143
- function setStateValue(currentAppState, newValue) {
1144
- const newAppState = setValue(currentAppState, mappedStore.path, newValue);
1145
- root.setState(newAppState);
1146
- return newAppState;
1147
- // In doing this refactoring I noticed that there is a 'bug' where the
1148
- // application state is returned instead of this state slice.
1149
- // This has worked this way since the beginning see:
1150
- // https://github.com/ngxs/store/blame/324c667b4b7debd8eb979006c67ca0ae347d88cd/src/state-factory.ts
1151
- // This needs to be fixed, but is a 'breaking' change.
1152
- // I will do this fix in a subsequent PR and we can decide how to handle it.
1153
- }
1154
- function setStateFromOperator(currentAppState, stateOperator) {
1155
- const local = getState(currentAppState);
1156
- const newValue = stateOperator(local);
1157
- return setStateValue(currentAppState, newValue);
1158
- }
1159
- function isStateOperator(value) {
1160
- return typeof value === 'function';
1161
- }
1162
1141
  return {
1163
1142
  getState() {
1164
1143
  const currentAppState = root.getState();
1165
- return getState(currentAppState);
1144
+ return getState(currentAppState, mappedStore.path);
1166
1145
  },
1167
1146
  patchState(val) {
1168
1147
  const currentAppState = root.getState();
1169
1148
  const patchOperator = simplePatch(val);
1170
- return setStateFromOperator(currentAppState, patchOperator);
1149
+ return setStateFromOperator(root, currentAppState, patchOperator, mappedStore.path);
1171
1150
  },
1172
1151
  setState(val) {
1173
1152
  const currentAppState = root.getState();
1174
1153
  return isStateOperator(val)
1175
- ? setStateFromOperator(currentAppState, val)
1176
- : setStateValue(currentAppState, val);
1154
+ ? setStateFromOperator(root, currentAppState, val, mappedStore.path)
1155
+ : setStateValue(root, currentAppState, val, mappedStore.path);
1177
1156
  },
1178
1157
  dispatch(actions) {
1179
1158
  return root.dispatch(actions);
@@ -1187,31 +1166,48 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.17", ngImpo
1187
1166
  type: Injectable,
1188
1167
  args: [{ providedIn: 'root' }]
1189
1168
  }], ctorParameters: function () { return [{ type: InternalStateOperations }]; } });
1169
+ function setStateValue(root, currentAppState, newValue, path) {
1170
+ const newAppState = setValue(currentAppState, path, newValue);
1171
+ root.setState(newAppState);
1172
+ return newAppState;
1173
+ // In doing this refactoring I noticed that there is a 'bug' where the
1174
+ // application state is returned instead of this state slice.
1175
+ // This has worked this way since the beginning see:
1176
+ // https://github.com/ngxs/store/blame/324c667b4b7debd8eb979006c67ca0ae347d88cd/src/state-factory.ts
1177
+ // This needs to be fixed, but is a 'breaking' change.
1178
+ // I will do this fix in a subsequent PR and we can decide how to handle it.
1179
+ }
1180
+ function setStateFromOperator(root, currentAppState, stateOperator, path) {
1181
+ const local = getState(currentAppState, path);
1182
+ const newValue = stateOperator(local);
1183
+ return setStateValue(root, currentAppState, newValue, path);
1184
+ }
1185
+ function getState(currentAppState, path) {
1186
+ return getValue(currentAppState, path);
1187
+ }
1190
1188
 
1191
- class StoreValidators {
1192
- static checkThatStateIsNamedCorrectly(name) {
1193
- if (!name) {
1194
- throwStateNamePropertyError();
1195
- }
1196
- else if (!this.stateNameRegex.test(name)) {
1197
- throwStateNameError(name);
1198
- }
1189
+ const stateNameRegex = new RegExp('^[a-zA-Z0-9_]+$');
1190
+ function ensureStateNameIsValid(name) {
1191
+ if (!name) {
1192
+ throwStateNamePropertyError();
1199
1193
  }
1200
- static checkThatStateNameIsUnique(stateName, state, statesByName) {
1201
- const existingState = statesByName[stateName];
1202
- if (existingState && existingState !== state) {
1203
- throwStateUniqueError(stateName, state.name, existingState.name);
1204
- }
1194
+ else if (!stateNameRegex.test(name)) {
1195
+ throwStateNameError(name);
1205
1196
  }
1206
- static checkThatStateClassesHaveBeenDecorated(stateClasses) {
1207
- stateClasses.forEach((stateClass) => {
1208
- if (!getStoreMetadata$1(stateClass)) {
1209
- throwStateDecoratorError(stateClass.name);
1210
- }
1211
- });
1197
+ }
1198
+ function ensureStateNameIsUnique(stateName, state, statesByName) {
1199
+ const existingState = statesByName[stateName];
1200
+ if (existingState && existingState !== state) {
1201
+ throwStateUniqueError(stateName, state.name, existingState.name);
1212
1202
  }
1213
1203
  }
1214
- StoreValidators.stateNameRegex = new RegExp('^[a-zA-Z0-9_]+$');
1204
+ function ensureStatesAreDecorated(stateClasses) {
1205
+ stateClasses.forEach((stateClass) => {
1206
+ if (!getStoreMetadata$1(stateClass)) {
1207
+ throwStateDecoratorError(stateClass.name);
1208
+ }
1209
+ });
1210
+ }
1215
1211
 
1216
1212
  /**
1217
1213
  * All provided or injected tokens must have `@Injectable` decorator
@@ -1372,8 +1368,8 @@ class StateFactory {
1372
1368
  get statePaths() {
1373
1369
  return this._parentFactory ? this._parentFactory.statePaths : this._statePaths;
1374
1370
  }
1375
- static cloneDefaults(defaults) {
1376
- let value = {};
1371
+ static _cloneDefaults(defaults) {
1372
+ let value = defaults;
1377
1373
  if (Array.isArray(defaults)) {
1378
1374
  value = defaults.slice();
1379
1375
  }
@@ -1383,9 +1379,6 @@ class StateFactory {
1383
1379
  else if (defaults === undefined) {
1384
1380
  value = {};
1385
1381
  }
1386
- else {
1387
- value = defaults;
1388
- }
1389
1382
  return value;
1390
1383
  }
1391
1384
  ngOnDestroy() {
@@ -1397,7 +1390,7 @@ class StateFactory {
1397
1390
  */
1398
1391
  add(stateClasses) {
1399
1392
  if (NG_DEV_MODE) {
1400
- StoreValidators.checkThatStateClassesHaveBeenDecorated(stateClasses);
1393
+ ensureStatesAreDecorated(stateClasses);
1401
1394
  }
1402
1395
  const { newStates } = this.addToStatesMap(stateClasses);
1403
1396
  if (!newStates.length)
@@ -1416,7 +1409,7 @@ class StateFactory {
1416
1409
  // `State` decorator. This check is moved here because the `ɵprov` property
1417
1410
  // will not exist on the class in JIT mode (because it's set asynchronously
1418
1411
  // during JIT compilation through `Object.defineProperty`).
1419
- if (typeof ngDevMode === 'undefined' || ngDevMode) {
1412
+ if (NG_DEV_MODE) {
1420
1413
  ensureStateClassIsInjectable(stateClass);
1421
1414
  }
1422
1415
  const stateMap = {
@@ -1425,7 +1418,7 @@ class StateFactory {
1425
1418
  isInitialised: false,
1426
1419
  actions: meta.actions,
1427
1420
  instance: this._injector.get(stateClass),
1428
- defaults: StateFactory.cloneDefaults(meta.defaults)
1421
+ defaults: StateFactory._cloneDefaults(meta.defaults)
1429
1422
  };
1430
1423
  // ensure our store hasn't already been added
1431
1424
  // but don't throw since it could be lazy
@@ -1538,7 +1531,7 @@ class StateFactory {
1538
1531
  for (const stateClass of stateClasses) {
1539
1532
  const stateName = getStoreMetadata$1(stateClass).name;
1540
1533
  if (NG_DEV_MODE) {
1541
- StoreValidators.checkThatStateNameIsUnique(stateName, stateClass, statesMap);
1534
+ ensureStateNameIsUnique(stateName, stateClass, statesMap);
1542
1535
  }
1543
1536
  const unmountedState = !statesMap[stateName];
1544
1537
  if (unmountedState) {
@@ -1998,36 +1991,34 @@ function Action(actions, options) {
1998
1991
  * Decorates a class with ngxs state information.
1999
1992
  */
2000
1993
  function State(options) {
2001
- function getStateOptions(inheritedStateClass) {
2002
- const inheritanceOptions = inheritedStateClass[META_OPTIONS_KEY] || {};
2003
- return Object.assign(Object.assign({}, inheritanceOptions), options);
2004
- }
2005
- function mutateMetaData(params) {
2006
- const { meta, inheritedStateClass, optionsWithInheritance } = params;
2007
- const { children, defaults, name } = optionsWithInheritance;
2008
- const stateName = typeof name === 'string' ? name : (name && name.getName()) || null;
2009
- // Caretaker note: we have still left the `typeof` condition in order to avoid
2010
- // creating a breaking change for projects that still use the View Engine.
2011
- if (typeof ngDevMode === 'undefined' || ngDevMode) {
2012
- StoreValidators.checkThatStateIsNamedCorrectly(stateName);
2013
- }
2014
- if (inheritedStateClass.hasOwnProperty(META_KEY)) {
2015
- const inheritedMeta = inheritedStateClass[META_KEY] || {};
2016
- meta.actions = Object.assign(Object.assign({}, meta.actions), inheritedMeta.actions);
2017
- }
2018
- meta.children = children;
2019
- meta.defaults = defaults;
2020
- meta.name = stateName;
2021
- }
2022
1994
  return (target) => {
2023
1995
  const stateClass = target;
2024
1996
  const meta = ensureStoreMetadata$1(stateClass);
2025
1997
  const inheritedStateClass = Object.getPrototypeOf(stateClass);
2026
- const optionsWithInheritance = getStateOptions(inheritedStateClass);
1998
+ const optionsWithInheritance = getStateOptions(inheritedStateClass, options);
2027
1999
  mutateMetaData({ meta, inheritedStateClass, optionsWithInheritance });
2028
2000
  stateClass[META_OPTIONS_KEY] = optionsWithInheritance;
2029
2001
  };
2030
2002
  }
2003
+ function getStateOptions(inheritedStateClass, options) {
2004
+ const inheritanceOptions = inheritedStateClass[META_OPTIONS_KEY] || {};
2005
+ return Object.assign(Object.assign({}, inheritanceOptions), options);
2006
+ }
2007
+ function mutateMetaData(params) {
2008
+ const { meta, inheritedStateClass, optionsWithInheritance } = params;
2009
+ const { children, defaults, name } = optionsWithInheritance;
2010
+ const stateName = typeof name === 'string' ? name : (name && name.getName()) || null;
2011
+ if (typeof ngDevMode === 'undefined' || ngDevMode) {
2012
+ ensureStateNameIsValid(stateName);
2013
+ }
2014
+ if (inheritedStateClass.hasOwnProperty(META_KEY)) {
2015
+ const inheritedMeta = inheritedStateClass[META_KEY] || {};
2016
+ meta.actions = Object.assign(Object.assign({}, meta.actions), inheritedMeta.actions);
2017
+ }
2018
+ meta.children = children;
2019
+ meta.defaults = defaults;
2020
+ meta.name = stateName;
2021
+ }
2031
2022
 
2032
2023
  const DOLLAR_CHAR_CODE = 36;
2033
2024
  function createSelectObservable(selector) {