ForbesLindesay / redux-optimist

Optimistically apply actions that can be later commited or reverted.
MIT License
776 stars 39 forks source link

REVERT after multiple BEGIN #19

Closed aaronbeall closed 7 years ago

aaronbeall commented 7 years ago

I have a series of actions that happen as a set of changes that eventually all get committed or reverted. Something like this:

dispatch({type: DRAG, optimist: { type: BEGIN, id: 0 }});
dispatch({type: DRAG, optimist: { type: BEGIN, id: 0 }});
dispatch({type: DRAG, optimist: { type: BEGIN, id: 0 }});

dispatch({type: DROP, optimist: { type: BEGIN, id: 0 }});

// ... after server round-trop
dispatch({type: DROP_SUCCESS, optimist: { type: COMMIT, id: 0 }});
// or
dispatch({type: DROP_REJECTED, optimist: { type: REVERT, id: 0 }});

As is, the REVERT doesn't work. All the optimist state keys get removed, but my state doesn't revert to its state before the first DRAG action. I need to revert all the actions, not just the last or the first. Is this possible without storing and reverting unique ids for every step?

ForbesLindesay commented 7 years ago

Only the start of the transaction should have the type: BEGIN bit, so it should look like:

dispatch({type: DRAG, optimist: { type: BEGIN, id: 0 }});
dispatch({type: DRAG, optimist: { id: 0 }});
dispatch({type: DRAG, optimist: { id: 0 }});

dispatch({type: DROP, optimist: { id: 0 }});

// ... after server round-trop
dispatch({type: DROP_SUCCESS, optimist: { type: COMMIT, id: 0 }});
// or
dispatch({type: DROP_REJECTED, optimist: { type: REVERT, id: 0 }});
aaronbeall commented 7 years ago

@ForbesLindesay Thanks for the follow up! I knew multiple BEGIN was not right but I did not know you can update by passing no type at all. Is this example in the documentation? Maybe a type UPDATE or something would be more intuitive. Cheers.

ForbesLindesay commented 7 years ago

I would be very happy to accept a pull request to update the README and make this clearer :)