BBaysinger
BBaysinger

Reputation: 6987

RxJS observable. How to make a re-emit once I have a reference

I have a reference to an observable, and I'm trying to trigger it to emit again after the first time it emits (or completes?). Is it possible to make this reference I've stored emit one more time?

  this._dashboardsObservable = this._http.get(PGDashService.MODEL_URL)

    .map((response: Response) => {

      this._model = this.parse(response.json());

      return this._model;

    })
    .catch((error: any) => Observable.throw(error || 'Server error'));

Upvotes: 4

Views: 3668

Answers (2)

Olaf Horstmann
Olaf Horstmann

Reputation: 16892

You could use the publishReplay(n)-operator:

this._dashboardsObservable = this._http.get(PGDashService.MODEL_URL)
    .map((response: Response) => this.parse(response.json()))
    .catch((error: any) => Observable.throw(error || 'Server error'))
    .publishReplay(1) // replay the last n (1) emissions
    .refCount();      // and share it between any subscriber

Upvotes: 1

snorkpete
snorkpete

Reputation: 14564

To be specific, no, a consumer of an observable can't force the observable to re-emit. The observable is responsible for producing its own values, and the observer merely gets values pushed to it from the observable.

However, i am sure there are other ways to arrive at what you're trying to do. For example, the use of a BehaviorSubject can allow you to cache the last value emitted so the next observer to subscribe will automatically get the last value sent to it.

You'll have to be more specific about what you're actually trying to accomplish to be guided any futher.

Upvotes: 4

Related Questions