将可观察到的操作凝结到单链中

发布于 2025-02-06 02:27:12 字数 1035 浏览 3 评论 0原文

我有一部分代码,其中包含一些可观察到的操作,我认为当它们共享相同的最终目标时,我认为应该有可能凝结。但是,由于我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉鱼一梦 2025-02-13 02:27:14

iif用于外部条件和内部条件可观察?

尝试以下内容

linkObservable$ = iif(
  () => isCommerceOrV3,
  this.handleCommerceOrV3Notifications(parsedUrl),
  this.handleClassicNotifications(parsedUrl)
).pipe(
  switchMap((isExternalOrGoToSection: any) => 
    isExternalOrGoToSection
      ? this.linksManager.handleLink(url, sectionId)
      : EMPTY
  )
);

How about iif for outer condition and ternary operator for inner observable?

Try the following

linkObservable$ = iif(
  () => isCommerceOrV3,
  this.handleCommerceOrV3Notifications(parsedUrl),
  this.handleClassicNotifications(parsedUrl)
).pipe(
  switchMap((isExternalOrGoToSection: any) => 
    isExternalOrGoToSection
      ? this.linksManager.handleLink(url, sectionId)
      : EMPTY
  )
);
季末如歌 2025-02-13 02:27:14

确实有很多方法可以做到这一点。以下答案与Alexey的答案相同,只有一些细节。在我看来,这似乎也是最优雅的解决方案:

const notificationOperation = isCommerceOrV3
  ? this.handleCommerceOrV3Notifications
  : this.handleClassicNotifications;

linkObservable$ = notificationOperation(parsedUrl).pipe(
  switchMap(isExternalOrGoToSection => isExternalOrGoToSection
    ? this.linksManager.handleLink(url, sectionId)
    : EMPTY;
  )
);

如果不清楚,在最重要的一部分中,我们将相关方法引用到使用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:

const notificationOperation = isCommerceOrV3
  ? this.handleCommerceOrV3Notifications
  : this.handleClassicNotifications;

linkObservable$ = notificationOperation(parsedUrl).pipe(
  switchMap(isExternalOrGoToSection => isExternalOrGoToSection
    ? this.linksManager.handleLink(url, sectionId)
    : EMPTY;
  )
);

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.

空城缀染半城烟沙 2025-02-13 02:27:14
linkObservable$ = (isCommerceOrV3 ? this.handleCommerceOrV3Notifications(parsedUrl) : this.handleClassicNotifications(parsedUrl)).pipe(...)

尽管如果将三元提取为变量,则可能会更可读性:

const someNameThatMakesSenseInContext = isCommerceOrV3 ? this.handleCommerceOrV3Notifications(parsedUrl) : this.handleClassicNotifications(parsedUrl)
linkObservable$ = someNameThatMakesSenseInContext.pipe(...)
linkObservable$ = (isCommerceOrV3 ? this.handleCommerceOrV3Notifications(parsedUrl) : this.handleClassicNotifications(parsedUrl)).pipe(...)

though it's likely more readable if you extract the ternary into a variable:

const someNameThatMakesSenseInContext = isCommerceOrV3 ? this.handleCommerceOrV3Notifications(parsedUrl) : this.handleClassicNotifications(parsedUrl)
linkObservable$ = someNameThatMakesSenseInContext.pipe(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文