如何解决可观察到的“完成”后退订
我正在通过以下查询通过RxJS从Firebase接收数据:
getAppliedLocalTags() {
this.tagSub = this.pageService
.getTagsOnPage(this.workspaceId,this.currentPage.id)
.pipe(
switchMap((references) => from(references)),mergeMap((ref) => this.afs.doc(ref).snapshotChanges()),map((actions) => actions.payload.data()),scan((allTagData,tagData) => [...allTagData,tagData],[])
)
.subscribe((data) => {
this.appliedTags = data;
});
// STOP THE SUB ONCE WE GET THE TAGS
setTimeout(() => {
console.log("un-subbed from tags");
this.tagSub.unsubscribe();
},2000);
}
订阅时,this.appliedTags
会填充来自数据库的所有“标签”。
在这种情况下,我只需要一次值。获得所有值后,我将退订,但是目前执行此操作的最佳方法是使用setTimeout(() => {},2000),
,这并不理想,因为时间会根据网络,标签数量等而变化。
不幸的是,我还没有找到一种解决方案,它在可观察项“完成”之后取消订阅数据流。
有没有办法在可观察到的“完成”之后退订,或停止发出值?
谢谢!
解决方法
这可以通过两种方式解决:
承诺方式:
getAppliedLocalTags() {
this.tagSub = this.pageService
.getTagsOnPage(this.workspaceId,this.currentPage.id)
.pipe(
switchMap((references) => from(references)),mergeMap((ref) => this.afs.doc(ref).snapshotChanges()),map((actions) => actions.payload.data()),scan((allTagData,tagData) => [...allTagData,tagData],[])
)
.toPromise().then((data) => {
this.appliedTags = data;
}).catch(err => {
// handle error
});
}
另一种方法是使用 take 运算符:
getAppliedLocalTags() {
this.tagSub = this.pageService
.getTagsOnPage(this.workspaceId,this.currentPage.id)
.pipe(
switchMap((references) => from(references)),[]),take(1) // This will emit only once then..
)
.subscribe((data) => {
this.appliedTags = data;
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。