首页 > 解决方案 > 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())

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 useris no longer available.

How do I stop listening to authState(app.auth()) once I have signed in. Where does the unsubscribe logic go in an epic?

标签: javascriptrxjsrxjs6redux-observableunsubscribe

解决方案


只要您的应用程序正在运行,史诗就应该保持活力。

您没有完成它们,而是将它们静音

要使流静音,有很多可能性。例如,windowToggleoperator 将允许您通过其他流(例如,通过其他事件的流)使您的 observable 静音。

例如,您在中间时将史诗静音SIGN_IN-SIGN_OUT序列。SIGN_OUT并在-SIGN_IN期间取消静音。

这是一篇关于rxjs 的不同暂停策略的文章。

希望这可以帮助


推荐阅读