Я хотел бы адаптировать этот код для проекта Angular, используя эффект NgRx для обработки обновления JWT. Но я застреваю с наблюдаемыми, так как токен обновляется только один раз, потому что timer
завершается в первый раз. Я также пытался с оператором delay
, но поскольку он не создает наблюдаемого, я не знаю, как реализовать его в этом потоке. Какие-нибудь мысли? Thanx!
// auth.effects.ts
@Effect()
renewJWT$: Observable<Action> = this.actions$.pipe(
ofType(ActionTypes.RenewJWT),
filter(() => this.authService.isLoggedIn()),
concatMap(() =>
this.authService.renewJWT().pipe(
map(() => new ScheduleJWTRenewalAction())
)
)
);
@Effect()
scheduleJWTRenewal$: Observable<Action> = this.actions$.pipe(
ofType(
ROOT_EFFECTS_INIT,
ActionTypes.SigninSuccess,
ActionTypes.ScheduleJWTRenewal
),
takeUntil(this.actions$.pipe(ofType(ActionTypes.Logout))),
map(() => Date.parse(this.authService.session.expiresAt) - Date.now()),
filter((remainingTime: number) => remainingTime > 0),
concatMap((remainingTime: number) =>
timer(Math.max(remainingTime, 1)).pipe(
map(() => new RenewJWTAction())
)
)
);