import { Observable } from 'rxjs'; import { Store } from '@ngrx/store'; import { Data } from '@angular/router'; import { filter } from 'rxjs/operators'; export abstract class ReduxModel { private model: T; constructor( private readonly st: Store, private readonly selector: string, private readonly action: any, ) { this.st.select(`${ selector }`) .pipe(filter((state: T | Data) => !!Object.keys(state).length)) .subscribe((state: T) => this.model = state); } public get data() { return this.model; } public set data(model: T) { this.model = model; this.st.dispatch(this.action(model)); } public getState(): Observable { return this.st.select(`${ this.selector }`); } }