简体   繁体   English

如何在 redux-observable 史诗中链接 RxJS observable?

[英]How can I chain RxJS observable in a redux-observable epic?

I've just started learning redux-observables and RxJS for work recently.我最近刚刚开始学习 redux-observables 和 RxJS 以用于工作。 We have alerts globally set in Redux.我们在 Redux 中全局设置了警报。 I want to be able to set an alert, then after a set period, close that same alert.我希望能够设置警报,然后在设定的时间段后关闭相同的警报。 There can also be multiple alerts at any time, and the user could manually close one alert before it automatically closes itself.也可以随时有多个警报,用户可以在它自动关闭之前手动关闭一个警报。 I have added to id here, so I can close the correct alert.我在这里添加了 id,所以我可以关闭正确的警报。 I have attempted to use delay after the initial map than another map to achieve this so far.到目前为止,我已经尝试在初始地图之后使用延迟而不是另一个地图来实现这一目标。 However, this skips the first map.但是,这会跳过第一张地图。

export const addAlertEpic: Epic<Action, Action, RootState> = (
  action$,
  state$
) =>
  action$.pipe(
    ofType(slice.actions.addAlert),
    map((values: any) =>
      slice.actions.addAlertSuccess({ id: uuid(), ...values.payload })
    )
  );

Thank you for help!谢谢你的帮助!

Add another epic for addAlertSucess, include a delay to dispatch the remove action.为 addAlertSucess 添加另一个史诗,包括调度删除操作的延迟。

export const addAlertSuccessEpic: Epic<Action, Action, RootState> = (
  action$,
  state$
) =>
  action$.pipe(
    ofType(slice.actions.addAlertSuccess),
    delay(myTimeToWait),
    map(foo =>
      slice.actions.removeAlert({ id: foo.id })
    )
  );

set an alert, then after a set period, close that same alert [...] he user could manually close one alert before it automatically closes itself设置警报,然后在设定的时间段后关闭相同的警报 [...] 在警报自动关闭之前,用户可以手动关闭警报

With that in mind, here would be my approach:考虑到这一点,这是我的方法:

actions$.pipe(
  ofType(slice.actions.addAlert),

  // `There can also be multiple alerts at any time`
  mergeMap(action => {
    const id = uuid();

    // using `NEVER` is important, it's essentially the same as new Observable(() => {})
    // it is needed because we don't want the inner observable to complete unless `takeUntil` decides to
    return NEVER
      .pipe(

        // start with opening the alert
        startWith(slice.actions.addAlertSuccess({ id, ...action.payload }))
        
        // close the alert either
        takeUntil(merge(
          
          // when `TIME` expires
          timer(TIME),
          
          // or when the user decides to
          userClosedActions.pipe(
            filter(action => action.id === id)
          )
        )),

        // at the end(when the stream completed due to `takeUntil`), close the alert
        endWith(slice.actions.closeAlertSuccess(...))
      )
  })
)

userClosedActions can be a Subject that sends next notifications as a result of user action. userClosedActions可以是作为用户操作的结果发送next通知的Subject For example:例如:

onClick (closedId) {
 userClosedActions.next({ id: closedId })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM