admin管理员组文章数量:1245117
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$;
}
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$;
}
Share
Improve this question
edited Jun 7, 2017 at 12:12
Olaf Horstmann
16.9k6 gold badges60 silver badges65 bronze badges
asked Jun 7, 2017 at 12:00
undefinedundefined
6,87413 gold badges53 silver badges101 bronze badges
1 Answer
Reset to default 18publishLast
shares (as the name suggests) the last emitted value - which can only be determined when the stream pletes.
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 plete 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 plete immediately after the emission of this value.
本文标签: javascriptRxJS publishReplay vs publishLastStack Overflow
版权声明:本文标题:javascript - RxJS publishReplay vs publishLast - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740150887a2232634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论