【问题标题】:Buffer until with redux-observable and rxjs使用 redux-observable 和 rxjs 缓冲直到
【发布时间】:2018-02-26 01:50:09
【问题描述】:

有没有一种模式可以制作redux-observable 史诗缓冲区,直到存储中的值为真?

const uploadsCompleteEpic = (action$, store) => {
  return action$
    .ofType(actionTypes.UPLOAD_SUCCESS)
    .bufferWhen(store => store.getState().remaining -1 === 0)
    .do(action => console.log(action))
    .ignoreElements();
};

上述方法不起作用,因为bufferWhen 中的函数不是 Observable。我也尝试将缓冲区函数包装在Observable.single 中,但没有成功。

【问题讨论】:

    标签: rxjs redux-observable


    【解决方案1】:

    您可以为此使用常规的buffer operator

    public buffer(closingNotifier: Observable<any>): Observable<T[]>

    将过去的值收集为一个数组,并仅在closingNotifier Observable 发出时发出该数组。

    const uploadsCompleteEpic = (action$, store) => {
      // just used as a notifier, so we don't actually
      // care about what the value is, just when.
      // Since the state is only ever updated when an
      // action is received we can use that time to check.
      const notification$ = action$.filter(() => {
        return store.getState().remaining - 1 === 0;
      );
    
      return action$
        .ofType(actionTypes.UPLOAD_SUCCESS)
        .buffer(notification$)
        // Use this if you want to exclude empty buffers
        // .filter(buffer => buffer.length > 0) 
        .do(action => console.log(action))
        .ignoreElements();
    };
    

    我不知道remaining 值在什么确切情况下会发生变化或为零,因此您绝对有可能需要进行额外的检查或等待,以免事情发生争执。例如排除空缓冲区,如果最终可能的话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2020-12-30
      • 1970-01-01
      相关资源
      最近更新 更多