Angular 角度:具有NgRx效应的JWT更新

Angular 角度:具有NgRx效应的JWT更新,angular,jwt,ngrx,ngrx-effects,Angular,Jwt,Ngrx,Ngrx Effects,我想适应一个角度项目,使用NgRx效果处理JWT更新。但我被可观察性所困扰,因为令牌只更新一次,因为timer第一次完成。我还尝试了delay操作符,但由于它不创建可观察对象,我不知道如何在这个流中实现它。有什么想法吗?塔克斯 // auth.effects.ts @Effect() renewJWT$: Observable<Action> = this.actions$.pipe( ofType(ActionTypes.RenewJWT), filter(() =>

我想适应一个角度项目,使用NgRx效果处理JWT更新。但我被可观察性所困扰,因为令牌只更新一次,因为
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())
    )
  )
);
//auth.effects.ts
@效果()
renewJWT$:Observable=此.actions$.pipe(
ofType(ActionTypes.RenewJWT),
筛选器(()=>this.authService.isLoggedIn()),
concatMap(()=>
此.authService.renewJWT()管道(
映射(()=>new ScheduleJWTRenewalAction())
)
)
);
@效果()
scheduleJWTRenewal$:可观=此.actions$.pipe(
类型(
根效应,
ActionTypes.SignInsAccess,
ActionTypes.ScheduleJWTRenewal
),
takeUntil(this.actions$.pipe(of type(ActionTypes.Logout)),
map(()=>Date.parse(this.authService.session.expiresAt)-Date.now()),
过滤器((remainingTime:number)=>remainingTime>0),
concatMap((剩余时间:数字)=>
计时器(数学最大值(剩余时间,1))管道(
映射(()=>new RenewJWTAction())
)
)
);
定时器

签名:定时器(初始延迟:数字|日期,周期:数字, 调度程序:调度程序):可观察

在给定的持续时间之后,每隔指定的时间按顺序发出数字 持续时间

如果未指定
period
计时器将发出一次并完成

takeUntil(this.actions$.pipe(ofType(ActionTypes.Logout))),
用户注销后,流将完成。用户是否可以再次登录?)


最好将
过滤器上的
takeUntil
替换为
过滤器

为什么不保留使用setTimeout的原则,setTimeout可以发送特定操作来续订(或检查到期)令牌。