【问题标题】:Is it possible to `skip` execution while other epic is running?是否可以在其他史诗运行时“跳过”执行?
【发布时间】:2019-05-03 10:08:32
【问题描述】:

我是 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 继续。

【问题讨论】:

  • 您是否考虑过设置一些标志,例如removingUser: true在店里,什么时候收到REMOVE_USER?然后忽略 GET_DEVICE_REQUEST 如果它是真的。
  • 这实际上是我现在所做的 - 它不像有很多设备要获取那么优雅,所以我设置devicesBeingLoaded: [...ids]。我只是好奇是否有办法只使用可观察对象。

标签: rxjs redux-observable


【解决方案1】:

您可以通过 windowToggle 实现这一目标:

在你的情况下 "on"REMOVE_USER_SUCCESS"off"REMOVE_USER_REQUEST

所以我们将在REMOVE_USER_SUCCESSREMOVE_USER_REQUEST 之间收听GET_DEVICE_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”。

--

希望对你有帮助

【讨论】:

  • 啊,这很有趣!非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 2021-05-02
  • 2020-12-07
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
相关资源
最近更新 更多