RxJS publishReplay vs publishLast

强颜欢笑 提交于 2019-12-07 01:35:47

问题


I am implementing caching HTTP results in Angular application. From what I know both of the following code works, but I need to know if they are doing exactly the same thing, or I am 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$;
}

回答1:


publishLast shares (as the name suggests) the last emitted value - which can only be determined when the stream completes.

publishReplay(1) shares the latest emitted value, which is done after any emission.


In the case of this.http.get(...) the behavior is the same, because the stream will complete after the result was received, thus the last and the latest value are the same thing.

You will have a different result though for streams that emit more than one value or that do not complete immediately after the emission of this value.



来源:https://stackoverflow.com/questions/44412070/rxjs-publishreplay-vs-publishlast

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!