Redux + Angular:如何简化并用最新和最新版本?

我正在尝试在应用启动后从商店获取指向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;
        })
    );
z1583393 回答:Redux + Angular:如何简化并用最新和最新版本?

我认为不必从流中取出1并用take(1)将其关闭。试试这个:

    setProperSection$ = combineLatest([
        this.actions$.pipe(
            ofType(AuthPatientActions.setProperSection)
        ),this.store.pipe(select(selectTokenExpiration),filter(t => !!t)),this.store.pipe(select(selectTenant),])
,
mergeMap(async a => {
            await this.notificationSvc.showPreloader();
            return a;
        }),

除了您的实际问题之外,您还应该在上面考虑以下问题:

delayWhen(async a => await this.notificationSvc.showPreloader()),

应该可以的所有工作

@Effect({ dispatch: false,resubscribeOnError: true })
setProperSection$ = this.actions$.pipe(
    ofType<AddUser>(AddUserActionTypes.AddUser),delayWhen(async action => await this.notificationSvc.showPreloader()),withLatestFrom(this.store.pipe(select(selectTokenExpiration))),map(([,tokenExpiration]) => tokenExpiration),// remove the action param
    mergeMap(async 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();
        }
    ),delayWhen(async r => await this.notificationSvc.hidePreloader()),}
本文链接:https://www.f2er.com/3013191.html

大家都在问