Angular NgRx-使用store.dispatch效果是否不好?
这个问题是一个延伸,但我觉得它的范围更窄,我可能能够得到一个独立的链接后的答案 背景 在链接的帖子中,我试图轮询一个服务来获取数据,我的问题是打开和关闭轮询。在我的真实世界应用程序中,通话实际上要复杂一点。我想做的是一些可选的http帖子,然后是一个GET,总是在myPOSTS'返回之后,这样我就知道服务器已经处理了发布的数据,并且将包含在我的下一个GET结果中 如果我写了一篇文章,我还想发送一个动作,这样我就可以更新我的状态,表明文章已经发生了 我已经将一些观察值转换为承诺值,因为我发现async/Wait比许多观察值切换/映射等更容易理解-我知道这可能不是很纯粹,但也许有一天我的rx/js操作员知识会有所提高,我只能寄希望于此。也许有人能给我展示更好的选择。。。但这不是主要问题 实际问题 我有下面的代码效果代码,它不会分派自己的操作,这些操作将在this.syncData中完成 然后辅助方法是Angular NgRx-使用store.dispatch效果是否不好?,angular,rxjs,ngrx,ngrx-effects,Angular,Rxjs,Ngrx,Ngrx Effects,这个问题是一个延伸,但我觉得它的范围更窄,我可能能够得到一个独立的链接后的答案 背景 在链接的帖子中,我试图轮询一个服务来获取数据,我的问题是打开和关闭轮询。在我的真实世界应用程序中,通话实际上要复杂一点。我想做的是一些可选的http帖子,然后是一个GET,总是在myPOSTS'返回之后,这样我就知道服务器已经处理了发布的数据,并且将包含在我的下一个GET结果中 如果我写了一篇文章,我还想发送一个动作,这样我就可以更新我的状态,表明文章已经发生了 我已经将一些观察值转换为承诺值,因为我发现asy
private syncData(): Observable<Action> {
const result$: Observable<Action> = Observable.create(async subscriber => {
try {
const pendingEdits = await this.store$.select(fromData.getPendingEditedData).pipe(take(1)).toPromise()
const pendingNewData = await this.store$.select(fromData.getNewData).pipe(take(1)).toPromise();
// First post any local updates. These both block, so once they finish we can get the server data knowing any posted
// data will be included
if (pendingEdits.length > 0) {
await this.dataService.postPendinEdits(pendingEdits).toPromise();
this.store$.dispatch(myActions.editSuccess());
}
if (pendingNewData.length > 0) {
await this.dataService.postPendingNewData(pendingNewData).toPromise();
this.store$.dispatch(myActions.addNewDataSuccess());
}
const dataResult$ = this.dataService.getAllData().pipe(
tap(data => {
this.previousResultsTimeUtc = data.previousResultsTimeUtc;
if (data.currentDay) {
this.store$.dispatch(myActions.getCurrentDaySuccess(data.currentDay));
this.store$.dispatch(myActions.getDataSuccess(data));
} else {
this.store$.dispatch((myActions.getDataSuccess(data));
}
}),
catchError(err => of(myActions.getDataFail(err)))
);
const subs2 = dataResult$.subscribe(ss => {
subs2.unsubscribe();
subscriber.next(ss);
});
} catch (error) {
subscriber.error(error);
}
})
return result$;
}
因此,我们可以在helper方法中看到,我获得了一些存储状态,以及分派操作。另外,在我的应用程序中,我还有另一个continuePolling效果,我也想在syncData中调用相同的代码
主要的问题是,使用这个.store$.dispatch来调度这些操作,而不是仅仅从效果中返回多个操作,这是一个坏主意吗
使用this.store$.dispatch是否会引发更多的选择,从而导致更多的UI更新?如果我从效果中返回所有选择,这将首先处理还原程序中的所有操作,然后再通过选择进行UI更新?我个人不希望syncData发送操作。 我宁愿看到它返回一个动作,让ngrx/effects处理动作的调度
性能方面应该是一样的。谢谢您的回复。我确实尝试让syncData返回一个操作列表,并使其效果返回它们,但我就是无法让它工作。我相信这是可能的,而且我对如何做到这一点非常感兴趣。这使得某些操作不总是被添加变得更加困难,因为它们的创建取决于当前状态
private syncData(): Observable<Action> {
const result$: Observable<Action> = Observable.create(async subscriber => {
try {
const pendingEdits = await this.store$.select(fromData.getPendingEditedData).pipe(take(1)).toPromise()
const pendingNewData = await this.store$.select(fromData.getNewData).pipe(take(1)).toPromise();
// First post any local updates. These both block, so once they finish we can get the server data knowing any posted
// data will be included
if (pendingEdits.length > 0) {
await this.dataService.postPendinEdits(pendingEdits).toPromise();
this.store$.dispatch(myActions.editSuccess());
}
if (pendingNewData.length > 0) {
await this.dataService.postPendingNewData(pendingNewData).toPromise();
this.store$.dispatch(myActions.addNewDataSuccess());
}
const dataResult$ = this.dataService.getAllData().pipe(
tap(data => {
this.previousResultsTimeUtc = data.previousResultsTimeUtc;
if (data.currentDay) {
this.store$.dispatch(myActions.getCurrentDaySuccess(data.currentDay));
this.store$.dispatch(myActions.getDataSuccess(data));
} else {
this.store$.dispatch((myActions.getDataSuccess(data));
}
}),
catchError(err => of(myActions.getDataFail(err)))
);
const subs2 = dataResult$.subscribe(ss => {
subs2.unsubscribe();
subscriber.next(ss);
});
} catch (error) {
subscriber.error(error);
}
})
return result$;
}