简体   繁体   English

如何取消订阅redux observable中的内部observable?

[英]How do I unsubscribe from inner observables in redux observable?

In the following epic I am listening to an action$ stream and then also listening to an auth stream authState(app.auth()) 在下面的史诗中,我正在收听动作$ stream,然后还要监听auth流authState(app.auth())

export const fetchAuthStatus = action$ =>
      action$.pipe(
        ofType(AUTH_STATUS_CHECKED),
        switchMap(() => authState(app.auth())),
        switchMap(user =>
          user
            ? of({ type: 'SIGNED_IN', payload: user })
            : signIn(googleAuthProvider)
        ),
        catchError(error => console.log('problems signing in'))
      );

It works as expected, the only problem is that if I change the auth status by logging out then the epic fires the signIn() method since user is no longer available. 它按预期工作,唯一的问题是,如果我通过注销更改auth状态,那么epic将触发signIn()方法,因为user不再可用。

How do I stop listening to authState(app.auth()) once I have signed in. Where does the unsubscribe logic go in an epic? authState(app.auth())如何停止收听authState(app.auth()) 。取消订阅逻辑在哪里出现史诗?

Epics are supposed to stay alive as long as your app is running. 只要您的应用程序正在运行,Epics就会保持活力。

You don't complete them, you mute them . 你没有完成它们,你将它们静音

To mute a stream there are a lot of possibilities. 要使流静音,有很多可能性。 For example, windowToggle operator will let you mute your observable by another streams, say, by streams of other events. 例如, windowToggle运算符将允许您通过其他流(例如,其他事件流)使您的observable静音。

Eg you mute the epic while in-between SIGN_INSIGN_OUT sequence. 例如,您在SIGN_IN - SIGN_OUT序列之间静音史诗。 And unmute in SIGN_OUTSIGN_IN period. 并在SIGN_OUT - SIGN_IN期间取消静音。

Heres an article on different pausing strategies with rxjs . 下面是关于rxjs的不同暂停策略的文章。

Hope this helps 希望这可以帮助

暂无
暂无

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

相关问题 如果 Observable 已完成,我是否需要取消订阅 Observable? - Do I need to unsubscribe from an Observable if the Observable is finished with? 如何取消订阅RxJS 5可观察? - How to unsubscribe from an RxJS 5 observable? 取消订阅RxJS Observables - Unsubscribe from RxJS Observables RxJS 在源完成时取消订阅内部可观察对象 - RxJS unsubscribe from inner observable when the source completes 在继续观察下的下一步之前,如何取消或取消订阅? - How do you cancel or unsubscribe before continuing with next in Observables? 如何在高阶可观察量中返回外部可观察量而不是内部可观察量 - How to return outer observable and not inner in a High-order observables 如何取消订阅 dispatch 以在 useEffect 上加载新数据? - 我正在使用 Redux 工具包 - How do I unsubscribe to the dispatch to grab new data onload on useEffect? - I'm using Redux Toolkit 如何正确退订 angular 服务结果? - How do I unsubscribe correctly from angular service results? 如何使用将其映射到值为可观察值的对象的函数映射Observable? - How do I map an Observable using a function that maps it to an object where the values are observables? 如何使用 2 个 observable 发出的值作为 http 请求的参数,其中 1 个 observable 将另一个重置为默认值? - How do I use values emitted by 2 observables as parameters to an http request, with 1 observable resetting the other to a default value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM