[RxJS] Subject asObservable() method
You can create your own state store, not using any state management libraray.
You might have seen the partten: People create a Subject, then use abObservable() method.
export class State { private prevState: INEO[]; private neoStoreSubject: BehaviorSubject<INEO[]>; neoStore$: Observable<INEO[]>; protected constructor() { this.neoStoreSubject = new BehaviorSubject([]); this.neoStore$ = this.neoStoreSubject.asObservable(); } ... }
Main reason for that is, we want to keep:
this.neoStoreSubject = new BehaviorSubject([]);
as private, we don't want any component can call .next() method to update store. The job for updating store should only happen in 'State' class.
// State class
setNeoStore(neoList: INEO[]) { this.setPrevState(); this.neoStoreSubject.next(neoList); this.dismissError(); }
For component, we can subscribe this.neoStore$. it can only receive the data, but not update the store directly.
// From Component biggerFasterNeo$ = this.data.neoStore$.pipe( filter(neoList => !!neoList === true), map(neoList => neoList.filter(neo => { if (neo.estimated_diameter > 0.5 || neo.relative_velocity > 50000) { return neo; } })) );
For component