循环一定次数

Loop a certain amount of times

我有一个动作,它获得 .times 的有效负载。我想循环多次,我这样做了:

function upAsyncEpic(action$: Observable<Action>, state$: StateObservable<State>): Observable<Action> {
    return action$.pipe(
        ofType(UP_ASYNC),
        delay(1000),
        mapTo(up())
    );
}

这不会循环,只会发生一次。我希望它循环 "delay" 然后 "mapTo" action.times 次。我试过这个:

    return action$.pipe(
        ofType(UP_ASYNC),
        repeat(action => action.times),
        delay(1000),
        mapTo(up())
    );

但这没有用。超级全新的 redux-observables,正在学习。

基本上我想要的是如果有人派遣行动{ type: 'UP_ASYNC', times: 5 }应该发生的是:

            delay(1000),
            mapTo(up())
            delay(1000),
            mapTo(up())
            delay(1000),
            mapTo(up())
            delay(1000),
            mapTo(up())
            delay(1000),
            mapTo(up())

问题:repeat() 接受一个整数作为重复计数的参数。

签名:

repeat(count: number): ObservableDocumentation.

Example 来自 learnrxjs

您的示例将如下所示:

function upAsyncEpic(
  action$: Observable<Action>,
  state$: StateObservable<State>
): Observable<Action> {
  let count = 0;

  const source = action$.pipe(
    ofType(UP_ASYNC),
    map(({ times }) => {
      count = times;
    }),
    delay(1000),
    mapTo(up())
  );

  return source.pipe(repeat(count));
}