我正在尝试在应用启动后从商店获取指向tokenExpiration值的值。因为这些值以异步方式填充,所以我添加了检查起诉过滤器函数以接收非空值。
在测试期间,我发现必须使用withLatestFrom来获取新值,因为CombineLatest从第一次非空数据获取发生时就返回了旧值。以下描述的解决方案有效,但似乎有点复杂,所以我想问一下如何仅使用 combineLatest 或 withLatestFrom (或其他方式)简化操作。
谢谢您的建议。
@Effect({ dispatch: false,resubscribeonError: true })
// Use combineLatest to get value from the mutliple sources & avoid to getting null values using filter option
setProperSection$ = combineLatest([
this.actions$.pipe(
ofType(AuthPatientactions.setProperSection)
),this.store.pipe(select(selectTokenExpiration),filter(t => !!t),take(1)),this.store.pipe(select(selectTenant),]).pipe(
// In order to get fresh values from store is need to get values using withLatestFrom
// because the combineLatest holds the old values in the memory (given by filter option)
withLatestFrom(this.store.pipe(select(selectTokenExpiration))),mergeMap(async a => {
await this.notificationSvc.showPreloader();
return a;
}),mergeMap(async ([a,tokenExpiration]) => {
const nowDt = new Date();
const tokenExpDt = new Date(tokenExpiration);
if (nowDt >= tokenExpDt) {
this.notificationSvc.showToast(this.translateSvc.instant("SESSION_ENDED_PLEASE_LOGIN"));
this.router.navigate(["login"],{}).catch(reason => {
console.error(reason);
});
} else {
this.router.navigate(["dashboard"],{}).catch(reason => {
this.notificationSvc.showToast(this.translateSvc.instant("CANNOT_NAVIGATE_TO_GIVEN_SECTION"));
});
}
return AuthPatientactions.doRedirect();
}
),mergeMap(async a => {
await this.notificationSvc.hidePreloader();
return a;
})
);