如何更好地在RXJS中编写以下代码段?

问题描述

嗨,我有一个小的代码段。重写以下代码,使其更易读和紧凑的最佳方法是什么?我想做的是,如果标志updateParameterisedContactList为true,我将更新参数化列表,然后更新批量发送对象,重定向到url(this.router.navigate),同时异步调用Toast服务,显示一个弹出窗口,表明该对象已更新。

如果标志updateParameterisedContactList为false,我们将仅更新批量发送对象并导航至编辑屏幕(使用this.router.navigate),并显示一个弹出窗口,表明该对象已更新。

  if (updateParameterisedContactList) {

      this.parameterisedContactListService
        .updateParameterisedList({
          ...this.parameterisedContactList,...bulkSend.parameterisedContactList
        })
        .pipe(tap(() => (this.isSubmitting = true)))
        .subscribe(
          parameterisedContactList => {
            this.bulkSendService.edit({ ...bulkSend,parameterisedContactList }).pipe(
              switchMap(bulkSend => {
                this.router.navigate(['/bulk-send',bulkSend.id,'edit']);
                return this.translateService.get([titleKey,bodyKey],{
                  name: bulkSend.name
                });
              })
            ).subscribe(
              translation => {
                this.isSubmitting = false;
                this.toastService.toast({
                  type: 'success',title: translation[titleKey],body: translation[bodyKey],icon: 'comments',timeout: 10000
                });
              },() => {
                this.isSubmitting = false;
              }
            );
          },() => (this.isSubmitting = false)
        )

    } else {

      this.bulkSendService
        .edit(bulkSend)
        .pipe(
          switchMap(bulkSend => {
            this.router.navigate(['/bulk-send','edit']);
            return this.translateService.get([titleKey,{
              name: bulkSend.name
            });
          })
        )
        .subscribe(
          translation => {
            this.isSubmitting = false;
            this.toastService.toast({
              type: 'success',timeout: 10000
            });
          },() => {
            this.isSubmitting = false;
          }
        );
    }
  }

非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)