Todos - Optimistic
What if we wanted to optimistically add/toggle our todos before the server API call returns?
ConanJs allows you to add optimistic updates to your state. In order to achieve this, it provides State monitor and asyncMerge. As you will see, the monitor also allows us to cancel ongoing async calls, and update our local state accordingly.
We need to slightly modify our Todo definition to make it optimistically updatable. It just needs to hold a status per todo item:
export enum OptimisticStatus {
SETTLED = 'SETTLED',
IN_PROCESS = 'IN_PROCESS',
}
export interface OptimisticData<T> {
data: T;
status: OptimisticStatus;
cancelCb: ICallback;
}
export interface OptimisticTodoListData {
todos: OptimisticData<ToDo>[];
appliedFilter: VisibilityFilters;
}