微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular – ngrx4效果中的多个动作 – > forkJoin无法按预期工作

我试图调用多个操作并等待它们完成,直到我返回SYNC_SUCCESS操作并关闭加载屏幕.但我无法让它发挥作用.

有什么建议我在这里做错了吗?

AppEffect:

@Effect()
syncMasterData$= this.actions$
    .ofType<AppActions.SyncMasterdata>(AppActions.SYNC_MASTERDATA)
    .mergeMap(() => 
    {
        return Observable.forkJoin(
            Observable.of(new BarcodeConfigActions.Sync)
        ).map(() => new AppActions.SyncingSuccess);
    })

BarcodeConfigEffect

@Effect() sync$= this.actions$
    .ofType<BarcodeConfigActions.Sync>(BarcodeConfigActions.SYNC)
    .mergeMap(() => this.apiProvider.getBarcodeConfig())
    .map((barcodeConfigs: BarcodeConfig[]) =>
    {
        let barcodeConfigState = <barcodeConfig.State>{};
        barcodeConfigState.tenant = "COOKIE";
        barcodeConfigState.barcodeConfig = barcodeConfigs;
        return new BarcodeConfigActions.SyncSuccess({ barcodeConfigs: barcodeConfigState });
    })
    .catch((error) =>
    {
        this._store.dispatch(new AppActions.UnexpectedError(error));
        return Observable.of(new BarcodeConfigActions.SyncFailed)
    })

AppAction:

export class SyncMasterdata implements Action
{
    readonly type = SYNC_MASTERDATA;
}

BarcodeConfigAction:

export class Sync implements Action
{
    readonly type = SYNC;
}

那样barcodeconfigeffect – >不会调用同步.如果我删除forkjoin并发布Observable.of(新的BarcodeConfigActions.Sync)它可以工作,但我无法分派更多的动作.

谢谢 :)

解决方法

如果我正确理解了问题,您似乎想确保在AppActions.SyncingSuccess(在AppEffect中)之前调度BarcodeConfigActions.SyncSuccess(在BarcodeConfigEffect中).这是为了确保您已收到getBarcodeConfig

您的解决方案的问题是您在forkJoining单个操作BarcodeConfigActions.Sync,然后在返回之前将该操作映射到AppActions.SyncingSuccess.这意味着BarcodeConfigActions.Sync永远不会在$observable动作上发出,这是您在BarcodeConfigEffect中使用的可观察对象.

如果你的syncMasterData $effects’唯一的逻辑是确保在AppActions.SyncingSuccess之前调用BarcodeConfigActions.SyncSuccess,或许只是在你的BarcodeConfigEffect中调度forkJoined这两个动作?

@Effect() sync$= this.actions$
.ofType<BarcodeConfigActions.Sync>(BarcodeConfigActions.SYNC)
.mergeMap(() => this.apiProvider.getBarcodeConfig())
.map((barcodeConfigs: BarcodeConfig[]) =>
{
    let barcodeConfigState = <barcodeConfig.State>{};
    barcodeConfigState.tenant = "COOKIE";
    barcodeConfigState.barcodeConfig = barcodeConfigs;
    /* NEW RETURN */
    return Observable.forkJoin(
                new AppActions.SyncingSuccess(),new BarcodeConfigActions.SyncSuccess({ barcodeConfigs: barcodeConfigState }));
})
.catch((error) =>
{
    this._store.dispatch(new AppActions.UnexpectedError(error));
    return Observable.of(new BarcodeConfigActions.SyncFailed)
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐