I'm trying to learn a bit about effect lifecycle hooks introduced in Ngrx 7 and I'm not really understanding something that's happening. I've got an Angular app and I have the following in an effects class, however, the init$ observable is emitting values infinitely. I would have expected it to fire once and complete. I am somewhat new to observables as well. The documentation doesn't really help me out as there aren't really many examples. I could add a take(1), but I would like to understand why it continues to emit forever.
@Injectable()
export class AuthEffects implements OnInitEffects{
constructor(private actions$: Actions) {}
@Effect({dispatch: false})
login$ = this.actions$.pipe(
ofType<LoginAction>(AuthActionTypes.LoginAction),
tap(console.log)
);
@Effect({dispatch: false})
logout$ = this.actions$.pipe(
ofType<LogoutAction>(AuthActionTypes.LogoutAction),
tap(console.log)
);
@Effect()
init$ = this.actions$.pipe(
ofType<Action>('[Auth] Effects Init'),
tap(console.log)
);
ngrxOnInitEffects(): Action {
console.log('AuthEffects init\'d');
return { type: '[Auth] Effects Init'};
}
}
This is the expected behaviour - effects' primary usage is to act on some third party side effects. And you set ofType<Action>
so it emits whenever any Action happens, while for ex:
@Effect({dispatch: false})
login$ = this.actions$.pipe(
ofType<LoginAction>(AuthActionTypes.LoginAction),
tap(console.log)
);
emits whenever LoginAction happens.