kenwheeler / mcfly

Flux architecture made easy
BSD 3-Clause "New" or "Revised" License
762 stars 46 forks source link

Implementing optimistic updates with McFly #21

Closed mking closed 9 years ago

mking commented 9 years ago

I was trying to work through how to implement optimistic updates with McFly (Fluxxor has a very good example of why we would need this). The main problem is that McFly actions dispatch a single payload, while optimistic updates require actions to dispatch multiple payloads (example: addTodo dispatching ADD_TODO, ADD_TODO_SUCCESS, and ADD_TODO_FAILURE).

One possible approach is to implement those extra payloads (ADD_TODO_SUCCESS, ADD_TODO_FAILURE) as separate McFly actions. Then, in the top-level action (addTodo) you can (a) immediately return the optimistic payload (ADD_TODO) and (b) fire off an AJAX request that dispatches the extra payloads.

What do you think? Should we add documentation to the project about this? I was thinking maybe a link in the README to a wiki page, or we can add it directly in the README.

bigblind commented 9 years ago

I think that implementing optimistic updates using separate actions is perfectly fine.

An other option would be to add a dispatch(action) method to the actions object. This way, an action method could call this.dispatch(action) whenever they want to dispatch something.