asynchronous - 基于异步事件管理订阅
问题描述
我正在寻找一种方法来简化这一点并避免在我的管道内管理订阅。
关于我的代码的总体思路:
this.start$ubject // waiting for users call to start
.pipe(
mergeMap(() => from(this.thirdPartyService.start())), // now i need to wait for smth to start
tap(() => {
// only after thirdPartyService started i can subscribe because prior to that they are undefined
this.subscriptions.push(
this.thirdPartyService.alfa$.subscribe(this.first$ubject),
this.thirdPartyService.beta$.subscribe(this.second$ubject),
);
}),
);
有什么办法可以处理吗?类似的东西,takeWhile
但订阅?
解决方案
试试这样:
// waits for start$ubject, then waits for thirdPartyService, then starts.
this.subscriptions.push(
this.start$ubject.pipe(
switchMap(() => from(this.thirdPartyService.start())), // waiting start
switchMap(() => merge( // listening on both of the streams
this.thirdPartyService.alfa$.pipe(
tap(this.first$ubject),
),
this.thirdPartyService.beta$.pipe(
tap(this.second$ubject),
),
)),
).subscribe(),
);
// waits for start$ubject or for thirdPartyService, then starts.
this.subscriptions.push(
merge(this.start$ubject, from(this.thirdPartyService.start()).pipe(
switchMap(() => merge( // listening on both of the streams
this.thirdPartyService.alfa$.pipe(
tap(this.first$ubject),
),
this.thirdPartyService.beta$.pipe(
tap(this.second$ubject),
),
)),
).subscribe(),
);
推荐阅读
- python - 错误UnicodeDecodeError:'utf-8'编解码器无法解码位置2的字节0xbe:无效的起始字节
- sql - ORA-04044: 此处不允许过程、函数、包或类型
- javascript - React.js datepicker 在时区之间转换
- python - os.write() 追加文件而不是覆盖,但不使用 O_APPEND
- cs50 - CS50 Pset 4 模糊 | 二进制表达式的无效操作数
- python - 如何将压缩的 MNIST 数据集提取到测试、训练集中?
- flutter - 升级颤振后 Xcode 构建的颤振错误输出
- javascript - 无法写入 Firebase 数据库
- django - Apache + mod_wsgi + django:为积压的请求发送默认响应
- python - 如何从python中的文本文件制作两个列表?