Search code examples
angularmobxangular-state-managmement

mobx & mobx-angular : how to update & subscribe to updated value from store?


I have angular app v6 and I'm using latest version of mobx and mobx-angular (You can see in dependency). I'm coming from ngrx, ngxs background so it is tough to understand mobx flow because it more or less angular-service approach with something extra (with performance also).

I have few question asked in stackblitz example. I hope somebody to give guidance on them.

DEMO APP

store.ts

@Injectable()
export class Store {

    @observable counter: number = 0;

    constructor() { }

    @action count() {
        this.counter ++;
    }
}

app.component.ts

export class AppComponent  {

  _counter:number=0;

  constructor(private store:Store){}

  ngOnInit(){
    // how to subscribe to updated value of counter from service and assign it to this._counter ????

    this._counter = this.store.counter;
  }
}

app.component.html

    <div *mobxAutorun>Counter : {{store.counter}}<br /></div>

______________________________________________

<div>Counter : {{store.counter}}<br /></div>

______________________________________________


<div>how to subscribe to updated value form 'counter' variable to '_counter' local variable????</div><br />

<div> {{_counter}} </div>

<button (click)="store.count()">Count</button>

Solution

  • You can set an RxJs subscription in ngOnInit.

    ngOnInit() {
      this.store.toRx(this.store, 'counter')
        .subscribe(val => this._counter = val)
    }
    

    toRx is a convenience function which can be added to the store.
    It uses the Mobx observe() function which just activates a callback each time the specified item changes.

    import { Injectable } from '@angular/core';
    import { action, observable, observe } from 'mobx';
    import { Observable } from 'rxjs';
    
    @Injectable()
    export class Store {
      ...
      toRx(obj, prop) {
        return Observable.create(observer =>
          observe(obj, prop, (change) => observer.next(change.newValue), true)
        );
      }
    }
    

    If you have deeply nested property you want to subscribe to, e.g

    @Injectable()
    export class Store {
      ...    
      @observable counterWrapper = { counter: 0 };
    

    just change the first parameter of toRx

    this.store.toRx(this.store.counterWrapper, 'counter')
      .subscribe(val => this._counter = val)