RxJs/NgRx - is there a way to “Cancel” a stream after the delay operator

别来无恙 提交于 2020-03-20 19:39:32

问题


I am using a polling scheme in my Angular application using NgRx.

To simplify things, I have something like the following...

    public stopPolling$ = createEffect(() => this.actions$.pipe(
        ofType(actions.stopPolling),
        tap(_ => this.isPollingActive = false),    
        map(_ => actions.stopPolling())
      ), { dispatch: false });

     public continuePolling$ = createEffect(() => this.actions$.pipe(
        ofType(actions.getData),
        tap(_ => this.logger.debug('continue polling')),    
        delay(8000),    
        switchMap(_ => this.pollData())
      ), { dispatch: false });


    private pollData() {
       if (!this.isPollingActive)
         return;
    }

In my "StopPolling" I set a flag, but if it is restarted (ie isPollingActive is et back to true) while I am in the delay(8000), the delay will exit and I will end up with the getData being called multiple times.

So, my question is, is there a way to step the switchMap(_ => this.pollData()) being called after the delay - ie is there someway to "force the delay to exit" before the timeout period?

Almost (if you know C#/.net). like a manualResetEvent.WaitOne(8000) which can be cancelled by calling Set() on the manualResetEvent object.

I hope I have described this clearly?

Thanks in advance


回答1:


You can create an observable that emits after a delay using timer and unsubscribe with takeUntil to exit early:

this.actions$.pipe(
  ofType(actions.getData),
  tap(_ => this.logger.debug('continue polling')),   
  switchMap(_ =>
    timer(8000).pipe(
      takeUntil(this.actions$.pipe(ofType(actions.stopPolling))),
      concatMap(() => this.pollData())
    )
)

This might also allow you to remove the side-effect this.isPollingActive = false and ensure that the control flow remains within the observable.



来源:https://stackoverflow.com/questions/60220897/rxjs-ngrx-is-there-a-way-to-cancel-a-stream-after-the-delay-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!