Merging the states in a better way

In the preceding code, we used an Object.assign() to merge the old state with the new state. We can do this even better by using the scan() operator on our dispatcher member, like so:

// NGRX-light/storeV.jsconst Rx = require('rxjs');class Store extends Rx.Subject {  constructor() {    super();    this.dispatcher = new Rx.Subject();    this.dispatcher      .scan((acc, curr) => ({ ...acc, ...curr }))      .subscribe(data => this.next(data));  }  dispatch(newState) {    this.dispatcher.next(newState);  }}const store = new Store();store.subscribe(data => console.log('store', data));store.dispatch({ name: 'chris' });store.dispatch({ address: 'London' });

An important thing to note in the preceding code is that we removed the state member ...

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.