是否可以在其他史诗正在运行时“跳过”执行?

时间:2019-05-03 10:08:33

标签: rxjs redux-observable

我是RxJS的初学者,如果这没有道理,那么请抱歉。

当前时间流如下:

REMOVE_USER -----------------------> SUCCESS
---------------GET_DEVICE--->SUCCESS--------

高级目标是在删除用户时跳过获取设备。

过于简单的史诗:

const getDeviceEpic = action$ => action$.pipe(
  ofType('GET_DEVICE_REQUEST'),
  mergeMap(() => from(service...).pipe(
    mapTo({ type: 'GET_DEVICE_SUCCESS' }))
  ))

const removeUser = action$ => action$.pipe(
  ofType('REMOVE_USER_REQUEST'),
  mergeMap(() => from(service...).pipe(
    mapTo({ type: 'REMOVE_USER_SUCCESS' }))
  )
)

我将如何处理?

我不确定是否可以添加例如takeUntil(removeUserAPICall$)到设备应用。或者也许检查REMOVE_USER_REQUEST是否已被解雇,然后等待REMOVE_USER_SUCCESS继续。

1 个答案:

答案 0 :(得分:2)

可能,您可以通过 windowToggle 来实现:

windowToggle

在您的情况下,“开” REMOVE_USER_SUCCESS,而“关” REMOVE_USER_REQUEST

因此,我们将在GET_DEVICE_REQUESTREMOVE_USER_SUCCESS之间收听REMOVE_USER_REQUEST

请注意,我们必须先打开过滤器,将startWith(void 0)放在“ on”流之前。

例如:

const getDeviceEpic = action$ => action$.pipe(
  ofType('GET_DEVICE_REQUEST'),
  windowToggle(
    action$.pipe(ofType('REMOVE_USER_SUCCESS'), startWith(void 0)),
    ()=>action$.pipe(ofType('REMOVE_USER_REQUEST')
  ),
  mergeMap(() => from(service...).pipe(
    mapTo({ type: 'GET_DEVICE_SUCCESS' }))
  ))

const removeUser = action$ => action$.pipe(
  ofType('REMOVE_USER_REQUEST'),
  mergeMap(() => from(service...).pipe(
    mapTo({ type: 'REMOVE_USER_SUCCESS' }))
  )
)

* 警告:写在记事本中

但是,恕我直言,在商店上放一个标志也是可以的(也许作为状态的指示)。

在我的文章“ Pausable Observables in RxJS”中,有关暂停和静音流的更多信息。

-

希望这会有所帮助

相关问题