将可观察到的操作凝结到单链中
我有一部分代码,其中包含一些可观察到的操作,我认为当它们共享相同的最终目标时,我认为应该有可能凝结。但是,由于我是RXJ的新手,所以我无法弄清楚该怎么做。有什么建议吗?谢谢。
if (isCommerceOrV3) {
linkObservable$ = this.handleCommerceOrV3Notifications(parsedUrl).pipe(
switchMap(isExternalOrGoToSection => {
if (isExternalOrGoToSection) {
return this.linksManager.handleLink(url, sectionId);
}
return EMPTY;
}
));
} else {
linkObservable$ = this.handleClassicNotifications(parsedUrl).pipe(
switchMap(isExternalOrGoToSection => {
if (isExternalOrGoToSection) {
return this.linksManager.handleLink(url, sectionId);
}
return EMPTY;
}
));
}
}
I have a segment of code containing some observable operations which I think should be possible to condense as they share the same end goal. However as I am fairly new to RxJs I can't quite figure out how to do it. Any suggestions? Thanks.
if (isCommerceOrV3) {
linkObservable$ = this.handleCommerceOrV3Notifications(parsedUrl).pipe(
switchMap(isExternalOrGoToSection => {
if (isExternalOrGoToSection) {
return this.linksManager.handleLink(url, sectionId);
}
return EMPTY;
}
));
} else {
linkObservable$ = this.handleClassicNotifications(parsedUrl).pipe(
switchMap(isExternalOrGoToSection => {
if (isExternalOrGoToSection) {
return this.linksManager.handleLink(url, sectionId);
}
return EMPTY;
}
));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iif
用于外部条件和内部条件可观察?
尝试以下内容
How about
iif
for outer condition and ternary operator for inner observable?Try the following
确实有很多方法可以做到这一点。以下答案与Alexey的答案相同,只有一些细节。在我看来,这似乎也是最优雅的解决方案:
如果不清楚,在最重要的一部分中,我们将相关方法引用到使用ISCommerceorv3 boolean上的三元运算符(?)上的函数变量。
在下一部分中,我们通过
NotificationOperation
参考变量调用相应的方法,并使用通用管道序列。在
switchmap
中,我们还使用无代码块的箭头功能速记,返回另一个三元语句和相关的可观察。There really is quite a few ways to do this. The following answer is the same as Alexey's, just with some more detail. To me this also seems to be the most elegant solution:
If it's not clear, in the top part we reference the relevant method to a function variable using a ternary operator on the isCommerceOrV3 boolean(?).
In the next part we invoke the respective method via the
notificationOperation
reference variable and use the common piping sequence.In the
switchMap
we also use the arrow function shorthand without a code block, returning another ternary statement and the relevant observable.尽管如果将三元提取为变量,则可能会更可读性:
though it's likely more readable if you extract the ternary into a variable: