【发布时间】:2018-09-16 16:29:03
【问题描述】:
我想将this code 改编为 Angular 项目,使用 NgRx 效果来处理 JWT 更新。但是我被 observables 卡住了,因为令牌只更新一次,因为timer 是第一次完成。我也尝试使用 delay 运算符,但由于它不创建可观察对象,我不知道如何在此流中实现它。有什么想法吗?谢谢!
// 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())
)
)
);
【问题讨论】:
-
为什么不保持使用 setTimeout 的原则,它可以调度特定的操作来更新(或检查过期)令牌。
标签: angular jwt ngrx ngrx-effects