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 user
is 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?
Epics are supposed to stay alive as long as your app is running.
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.
E.g. you mute the epic while in-between SIGN_IN
— SIGN_OUT
sequence. And unmute in SIGN_OUT
— SIGN_IN
period.
Heres an article on different pausing strategies with rxjs.
Hope this helps