Skip to content

Some thoughts on optimistic updates #51

Open
@otakustay

Description

@otakustay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions