ngrx/effects use delay operator with value from state

4.8k views Asked by At

in my application, I tracking for session time

  @Effect()
  session$: Observable<Action> = this.actions$
    .ofType(authAction.RESET_SESSION)
    .switchMap(_ => Observable.of({ type: authAction.LOGOUT })
                .delay(600000);

*every time that RESET_SESSION action is called, the logout action waits 10 minutes.

in my application, I get session time from the server and store it on state after login. how I can use this store value in delay? ( because in ngrx store return observable and not object of state)

something like this:

 @Effect()
  session$: Observable<Action> = this.actions$
    .ofType(authAction.RESET_SESSION)
    .switchMap(_ => Observable.of({ type: authAction.LOGOUT })
                .delay(this.store.pluck('auth','sessionTime'));
1

There are 1 answers

0
Jota.Toledo On BEST ANSWER

You would need to inject the store into the Effects through the constructor. Something like:

@Injectable()
export FooEffects {
   @Effect()
    session$: Observable<Action> = this.actions$
    .ofType(authAction.RESET_SESSION)
    .mergeMap(_ => this.store.select(getSessionTime)) //getSessionTime would be a selector
    .switchMap(delay => Observable.of({ type: authAction.LOGOUT }).delay(delay));

   ctor(
   private actions$: Actions,
   private store: Store<rootState>){}
}