简体   繁体   English

抛出错误后如何避免执行concatMap?

[英]How to avoid execution concatMap after throw error?

I have the following a stream of click events:我有以下点击事件流:

this.createReestrClick
            .pipe(
                concatMap(() =>
                    this.documentsRepository.selectedDocumentsFilesIds$.pipe(
                        take(1),
                        tap((ids: number[]) => {
                            if (ids.length == 0) throw new Error('Error messsage!');
                        }),

                        indicate(this.createReestrloading$),
                        tap((ids: number[]) => (this.dialogConfig.data = { data: { ids }, width: '500px' })),
                        concatMap(() =>
                            this.dialog
                                .open(DialogDocumentCreateRegisterComponent, this.dialogConfig)
                                .afterClosed()
                                .pipe(
                                    filter(Boolean),
                                    concatMap((data: { date: Date; ids: number[] }) =>
                                        this.documentsRepository.CreateMailReestrById(data.ids, data.date),
                                    ),
                                    handleResponseMessage(this.messageService),
                                ),
                        ),
                    ),
                ),
                catchError((e) => of(e)),
            )
            .subscribe((e) => {
                if (e instanceof Error) this.messageService.showMessage('', e.message, 'warning');
            });

Firts it listens clicks, then concat with selectedDocumentsFilesIds$ .首先它会监听点击,然后与selectedDocumentsFilesIds$ In case there are no values in array it throws an exeption:如果数组中没有值,它会抛出一个异常:

if (ids.length == 0) throw new Error('Error messsage!');

First problem when in happend the inner stream selectedDocumentsFilesIds$ does not work more.发生的第一个问题是,内部流selectedDocumentsFilesIds$不再起作用。 To solve this I have moved catchError((e) => of(e)), on the line:为了解决这个问题,我将catchError((e) => of(e)),移到了行上:

tap((ids: number[]) => {
   if (ids.length == 0) throw new Error('Error messsage!');
 }),
catchError((e) => of(e))

In this case as I return an empty observer of(e) it allows to continue concatMap(() => , but should not.在这种情况下,当我返回of(e)的空观察者时of(e)它允许继续concatMap(() => ,但不应该。

How to fix it, dont allow to execute concatMap(() => if exception happens and dont stop steam?如何修复它,如果发生异常并且不停止 Steam,则不允许执行concatMap(() =>

As an abstract, you can do something like this作为一个抽象,你可以做这样的事情

let hadError=false;
observable.pipe(
  ....
  yourThrowingErrorOperator(),
  catchError(err=> hadError=true),
  takeUntil(v=>hadError),
  concatMap(...)
.....

) )

so once error will be cought in the upstream, downstream will be terminated.所以一旦上游发生错误,下游将被终止。 If you want to keep it up and running, use filter instead of takeUntil to "block" values in the stream while flag is set.如果您想让它保持正常运行,请在设置标志时使用filter而不是takeUntil来“阻止”流中的值。 Just remember to reset it somehow.只记得以某种方式重置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM