How to do http polling in ngrx effect

后端 未结 1 379
生来不讨喜
生来不讨喜 2021-01-20 10:39

I have this effect, and I\'m trying to use timer to poll for data every x seconds. But I can\'t figure out how timer is supposed to interact with the data streams. I tried a

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 11:38

    This should work (i have tested it). Please add this top of the switchMap. The key operator here is the mapTo. This operator will map the incoming value of the interval into the payload.

    switchMap((action$: appActions.GetPlotDataAction) =>
       interval(5000).pipe(mapTo(action$))
    );
    

    Update (hint): - If you want to start immediately with the polling and then each {n}ms you can use the startWith operator or the timer observable

    switchMap((action$: appActions.GetPlotDataAction) =>
      interval(5000).pipe(startWith(0), mapTo(action$))
    );
    

    or

    switchMap((action$: appActions.GetPlotDataAction) => 
      timer(0, 1000).pipe(mapTo(action$))
    );
    

    0 讨论(0)
提交回复
热议问题