@push.rocks/smartstate 2.0.23 → 2.0.25

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.
@@ -17727,9 +17727,9 @@ var StatePart2 = class {
17727
17727
  this.webStore = new dist_ts_exports11.WebStore(this.webStoreOptions);
17728
17728
  await this.webStore.init();
17729
17729
  const storedState = await this.webStore.get(String(this.name));
17730
- if (storedState) {
17730
+ if (storedState && this.validateState(storedState)) {
17731
17731
  this.stateStore = storedState;
17732
- this.notifyChange();
17732
+ await this.notifyChange();
17733
17733
  }
17734
17734
  }
17735
17735
  }
@@ -17744,24 +17744,38 @@ var StatePart2 = class {
17744
17744
  * @param newStateArg
17745
17745
  */
17746
17746
  async setState(newStateArg) {
17747
+ if (!this.validateState(newStateArg)) {
17748
+ throw new Error(`Invalid state structure for state part '${this.name}'`);
17749
+ }
17747
17750
  this.stateStore = newStateArg;
17748
- this.notifyChange();
17751
+ await this.notifyChange();
17749
17752
  if (this.webStore) {
17750
17753
  await this.webStore.set(String(this.name), newStateArg);
17751
17754
  }
17752
17755
  return this.stateStore;
17753
17756
  }
17757
+ /**
17758
+ * Validates state structure - can be overridden for custom validation
17759
+ * @param stateArg
17760
+ */
17761
+ validateState(stateArg) {
17762
+ return stateArg !== null && stateArg !== void 0;
17763
+ }
17754
17764
  /**
17755
17765
  * notifies of a change on the state
17756
17766
  */
17757
- notifyChange() {
17758
- const createStateHash = (stateArg) => {
17759
- return dist_ts_web_exports.sha256FromString(dist_ts_exports4.stringify(stateArg));
17767
+ async notifyChange() {
17768
+ if (!this.stateStore) {
17769
+ return;
17770
+ }
17771
+ const createStateHash = async (stateArg) => {
17772
+ return await dist_ts_web_exports.sha256FromString(dist_ts_exports4.stringify(stateArg));
17760
17773
  };
17761
- if (this.stateStore && this.lastStateNotificationPayloadHash && createStateHash(this.stateStore) === this.lastStateNotificationPayloadHash) {
17774
+ const currentHash = await createStateHash(this.stateStore);
17775
+ if (this.lastStateNotificationPayloadHash && currentHash === this.lastStateNotificationPayloadHash) {
17762
17776
  return;
17763
17777
  } else {
17764
- this.lastStateNotificationPayloadHash = this.stateStore;
17778
+ this.lastStateNotificationPayloadHash = currentHash;
17765
17779
  }
17766
17780
  this.state.next(this.stateStore);
17767
17781
  }
@@ -17769,7 +17783,11 @@ var StatePart2 = class {
17769
17783
  * creates a cumulative notification by adding a change notification at the end of the call stack;
17770
17784
  */
17771
17785
  notifyChangeCumulative() {
17772
- setTimeout(() => this.state.next(this.stateStore), 0);
17786
+ setTimeout(async () => {
17787
+ if (this.stateStore) {
17788
+ await this.notifyChange();
17789
+ }
17790
+ }, 0);
17773
17791
  }
17774
17792
  /**
17775
17793
  * selects a state or a substate
@@ -17780,6 +17798,7 @@ var StatePart2 = class {
17780
17798
  }
17781
17799
  const mapped = this.state.pipe(
17782
17800
  dist_ts_exports5.rxjs.ops.startWith(this.getState()),
17801
+ dist_ts_exports5.rxjs.ops.filter((stateArg) => stateArg !== void 0),
17783
17802
  dist_ts_exports5.rxjs.ops.map((stateArg) => {
17784
17803
  try {
17785
17804
  return selectorFn(stateArg);
@@ -17837,25 +17856,33 @@ var Smartstate = class {
17837
17856
  }
17838
17857
  /**
17839
17858
  * Allows getting and initializing a new statepart
17840
- * initMode === 'soft' it will allow existing stateparts
17841
- * initMode === 'mandatory' will fail if there is an existing statepart
17842
- * initMode === 'force' will overwrite any existing statepart
17859
+ * initMode === 'soft' (default) - returns existing statepart if exists, creates new if not
17860
+ * initMode === 'mandatory' - requires statepart to not exist, fails if it does
17861
+ * initMode === 'force' - always creates new statepart, overwriting any existing
17862
+ * initMode === 'persistent' - like 'soft' but with webstore persistence
17843
17863
  * @param statePartNameArg
17844
17864
  * @param initialArg
17845
17865
  * @param initMode
17846
17866
  */
17847
- async getStatePart(statePartNameArg, initialArg, initMode) {
17848
- if (this.statePartMap[statePartNameArg]) {
17849
- if (initialArg && (!initMode || initMode !== "soft")) {
17850
- throw new Error(
17851
- `${statePartNameArg} already exists, yet you try to set an initial state again`
17852
- );
17867
+ async getStatePart(statePartNameArg, initialArg, initMode = "soft") {
17868
+ const existingStatePart = this.statePartMap[statePartNameArg];
17869
+ if (existingStatePart) {
17870
+ switch (initMode) {
17871
+ case "mandatory":
17872
+ throw new Error(
17873
+ `State part '${statePartNameArg}' already exists, but initMode is 'mandatory'`
17874
+ );
17875
+ case "force":
17876
+ return this.createStatePart(statePartNameArg, initialArg, initMode);
17877
+ case "soft":
17878
+ case "persistent":
17879
+ default:
17880
+ return existingStatePart;
17853
17881
  }
17854
- return this.statePartMap[statePartNameArg];
17855
17882
  } else {
17856
17883
  if (!initialArg) {
17857
17884
  throw new Error(
17858
- `${statePartNameArg} does not yet exist, yet you don't provide an initial state`
17885
+ `State part '${statePartNameArg}' does not exist and no initial state provided`
17859
17886
  );
17860
17887
  }
17861
17888
  return this.createStatePart(statePartNameArg, initialArg, initMode);
@@ -17865,8 +17892,9 @@ var Smartstate = class {
17865
17892
  * Creates a statepart
17866
17893
  * @param statePartName
17867
17894
  * @param initialPayloadArg
17895
+ * @param initMode
17868
17896
  */
17869
- async createStatePart(statePartName, initialPayloadArg, initMode) {
17897
+ async createStatePart(statePartName, initialPayloadArg, initMode = "soft") {
17870
17898
  const newState = new StatePart2(
17871
17899
  statePartName,
17872
17900
  initMode === "persistent" ? {
@@ -17877,8 +17905,8 @@ var Smartstate = class {
17877
17905
  await newState.init();
17878
17906
  const currentState = newState.getState();
17879
17907
  await newState.setState({
17880
- ...initialPayloadArg,
17881
- ...currentState
17908
+ ...currentState,
17909
+ ...initialPayloadArg
17882
17910
  });
17883
17911
  this.statePartMap[statePartName] = newState;
17884
17912
  return newState;