【发布时间】:2025-11-23 16:55:02
【问题描述】:
我们正在为我们的 Angular 应用程序采用 Nrwl.io 的 Nx 框架。
作为其中的一部分,我们试图了解数据持久性模块中的optimisticUpdate 和pessimisticUpdate 方法之间的底层实现差异。
根据文档,pessimisticUpdate 会先于客户端更新服务器,而使用optimisticUpdate 时,会先更新客户端。
不过这两种方法在github上的源码如下
export function pessimisticUpdate<T, A extends Action>(
opts: PessimisticUpdateOpts<T, A>
) {
return (source: ActionStateStream<T, A>): Observable<Action> => {
return source.pipe(
mapActionAndState(),
concatMap(runWithErrorHandling(opts.run, opts.onError))
);
};
}
export function optimisticUpdate<T, A extends Action>(
opts: OptimisticUpdateOpts<T, A>
) {
return (source: ActionStateStream<T, A>): Observable<Action> => {
return source.pipe(
mapActionAndState(),
concatMap(runWithErrorHandling(opts.run, opts.undoAction))
);
};
}
从表面上看,将更新分为乐观和悲观似乎非常有用,但从本质上讲,这两种方法的实现似乎是相同的,我们正在努力理解这两种方法的含义。
此外,当 run 方法成功完成时,调用optimisticUpdate 方法的示例代码不会调度操作。我的理解是,这将结束流 - 没有迹象表明后端调用应该返回什么。
class TodoEffects {
@Effect() updateTodo = this.s.optimisticUpdate('UPDATE_TODO', {
// provides an action and the current state of the store
run: (a: UpdateTodo, state: TodosState) => {
return this.backend(state.user, a.payload);
},
undoAction: (a: UpdateTodo, e: any) => {
// dispatch an undo action to undo the changes in the client state
return ({
type: 'UNDO_UPDATE_TODO',
payload: a
});
}
});
任何一直在使用 Nx 的人都可以阐明其中的区别以及我们需要在我们的服务中实施哪些操作以实现乐观更新。
【问题讨论】:
标签: angular ngrx ngrx-effects nrwl nrwl-nx