此RxJS史诗流有更好的解决方案吗?

问题描述 投票:3回答:2

我使用redux-observable的史诗具有redux状态。

我需要解决在用户删除一个或多个对象后显示消息的问题。

有两种方法删除对象:

  1. 通过动作deleteObject(id: string)调用deleteObjectFulfilled动作
  2. 通过动作deleteObjects(ids: Array<string>)调用N * deleteObject(id: string)动作

我想在每次成功的“删除操作”之后显示已删除邮件的仅一个邮件,计数]。

我对这部史诗的最终解决方案是:

export const showDeleteInformationEpic = action$ =>
  combineLatest(
    action$.pipe(ofType(DELETE_OBJECT_FULFILLED)),
    action$.pipe(
      ofType(DELETE_OBJECTS),
      switchMap(({ meta: { ids } }) =>
        action$.pipe(
          ofType(DELETE_OBJECT_FULFILLED),
          skip(ids.length - 1),
          map(() => ids.length),
          startWith('BATCH_IN_PROGRESS'),
          take(2),
        ),
      ),
      startWith(1),
    ),
  ).pipe(
    startWith([null, null]),
    pairwise(),
    map(([[, previousCount], [, currentCount]]) => 
      (previousCount === 'BATCH_IN_PROGRESS') 
        ? currentCount 
        : isNumber(currentCount) ? 1 : currentCount),
    filter(isNumber),
    map((count) => throwInfo('objectDeleted', { count })),
  );

您能看到更好的解决方案吗?

我使用redux-observable的史诗具有redux状态。我需要解决在用户删除一个或多个对象后显示一条消息的问题。有两种删除对象的方法:通过操作...

javascript redux rxjs redux-observable
2个回答
1
投票

如果两种情况下我都只使用deleteObjects(Array<string>),则有一个更简单的解决方案。


0
投票

代替触发多个动作,您可以创建和调度单个动作DELETE_MULTIPLE,并在有效负载中传递所有id

© www.soinside.com 2019 - 2024. All rights reserved.