cordova - 将完成添加到永远不会完成的可观察对象
问题描述
我正在尝试使用离子包装器向cordova插件添加一个完成。所以我会在订阅者中完成。所以我得到了一些服务,它是科尔多瓦 plaugin 的离子包装器:
startListen(){
return this.someService.startListen().pipe(
switchMap((response) => {
return new Observable(subscriber => {
if (response.index !== undefined) {
subscriber.next(1);
}
if (response.errorTitle === Message.NOT_FOUND) {
subscriber.error(Message.NOT_FOUND);
}
if (response.errorTitle === Message.CANCELLED) {
console.log('blah blah'); <---### IS PRINTED
subscriber.complete();
}
});
}),
);
}
我在某些组件中调用它:
startListen(){
this.someFacade.startListen().subscribe(
(x) => console.log('next', x),
(x) => console.log('error', x),
() => console.log('complete'), <---# NOT PRINNTED
);
console.log('complete') 永远不会被触发的问题。
怎么了?
解决方案
takeWhile
操作员做你想做的事:
this.someService.startListen().pipe(
takeWhile(response => response.errorTitle !== Message.CANCELLED),
switchMap(response => response.index !== undefined
? of(response.index)
: throwError(response.errorTitle)
)
推荐阅读
- javascript - 在javascript中拼接一个数字
- python - 如何围绕另一个类方法包装装饰器?
- shell - shell 脚本 :: 两个十六进制数字相加
- oracle - 删除数据集中空格后的所有数字
- java - 将 UTF-8 与 Java 11 HttpResponse 结合使用
- excel - 在excel中自动填充日历
- terraform - 如何使用 TestCheckResourceAttr 在验收测试中检查 TypeMap?
- r - R data.table - 根据值添加行
- python - 为什么这个解码函数在 python2 中不起作用?
- arrays - 可以将 uint256[100] 内存转换为 uint256[10] 内存吗?