redux-utilities / redux-promise

FSA-compliant promise middleware for Redux.
MIT License
2.67k stars 134 forks source link

Some thoughts on optimistic updates #51

Open otakustay opened 7 years ago

otakustay commented 7 years ago

I know #12 includes some discussion about optimistic updates, but my topic is not related to FSA, so I decide to open an new issue.

I've been thinking about how optimistic UI should land in a redux architecture, as a conclusion I discovered:

  1. Optimistic updates are not always the "success part" of non-optimistic updates, they can vary from each other
  2. It's not really good to pollute hundreds of actions in order to have optimistic updates, it could be hard if we need "optimistic in some situations but without optimistic otherwise"
  3. Optimistic should be an add-on part, easy to plug in or out on demand.

As a result I developed a optimistic updates supported middleware based on redux-thunk: redux-optimistic-thunk

I think this pattern can also easily apply to promise based architecture:

  1. Just consume an array with 2 items [Promise, Action]
  2. The first is a Promise which works the same as current redux-promise
  3. The second is a plain object action which will be dispatched immediately to make optimistic updates
  4. Just remember the state before optimistic actions are dispatched, as well as all actions after a state is remembered, rollback them after Promise fulfills

I've thought about developing a redux-optimistic-promise middleware myself, but it seems to be better if redux-promise can support it directly, since an array is a brand new type of action, this will have no backward compatibility issues