Skip to content

Commit

Permalink
Merge pull request #126 from gernsdorfer/bugfix/old-initial-state
Browse files Browse the repository at this point in the history
Bugfix: create Store with initial state
  • Loading branch information
gernsdorfer authored Dec 11, 2023
2 parents 0a0bd7e + 06cf356 commit 7bafd5d
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions libs/store/src/services/stores/component-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Inject, Injectable, OnDestroy, Optional } from '@angular/core';
import { ComponentStore as NgrxComponentStore } from '@ngrx/component-store';
import { Actions } from '@ngrx/effects';
import { Action, Store as NgrxStore } from '@ngrx/store';
import { asapScheduler, Observable, Subject, takeUntil } from 'rxjs';
import { Observable, Subject, asapScheduler, takeUntil } from 'rxjs';
import {
SkipLogForStore,
StateToken,
Expand Down Expand Up @@ -73,7 +73,8 @@ export class ComponentStore<STATE extends object>
: stateOrUpdaterFn;

super.setState(newState);
if (!skipLog) this.dispatchCustomAction(action, newState);
if (!skipLog)
asapScheduler.schedule(() => this.dispatchCustomAction(action, newState));
}

override patchState(
Expand All @@ -90,21 +91,20 @@ export class ComponentStore<STATE extends object>
? partialStateOrUpdaterFn(this.get())
: partialStateOrUpdaterFn;
super.patchState(newState);

this.dispatchCustomAction(action, { ...this.get(), ...newState });
asapScheduler.schedule(() =>
this.dispatchCustomAction(action, { ...this.get(), ...newState }),
);
}

protected dispatchCustomAction(action: string, state: STATE) {
if (this.skipLogForStore) {
return;
}

asapScheduler.schedule(() => {
this.ngrxStore.dispatch(
getCustomAction({ actionName: action, storeName: this.storeName })({
payload: state,
}),
);
});
this.ngrxStore.dispatch(
getCustomAction({ actionName: action, storeName: this.storeName })({
payload: state,
}),
);
}
}

0 comments on commit 7bafd5d

Please sign in to comment.