RxJS publishReplay vs publishLast

I am caching HTTP results in an Angular application. From what I know, both of the following codes work, but I need to know if they do exactly the same thing, or am I missing something important?

publishLast

getPosts() {
    if( !this.posts$ ) {
      this.posts$ = this.http.get('api').publishLast().refCount();
      return this.posts$;
    }

    return this.posts$;
  }

publishReplay

getPosts() {
  if( !this.posts$ ) {
    this.posts$ = this.http.get('api').publishReplay(1).refCount();
       return this.posts$;
  }

  return this.posts$;
}
+2
source share
1 answer

publishLaststocks (as the name implies) highlights the value of last , which can only be determined when the stream completes .

publishReplay(1)divided by the last emitted value , which is executed after any outlier.


this.http.get(...) , , .

, , .

+11

Source: https://habr.com/ru/post/1649041/


All Articles