reduxjs / redux

A JS library for predictable global state management
https://redux.js.org
MIT License
60.88k stars 15.27k forks source link

Feature Request: Batch dispatch actions #2108

Closed eloytoro closed 7 years ago

eloytoro commented 7 years ago

Right now redux's listeners are synchronous, this means that for every dispatch call all the listeners will trigger, and if many happen at the same time then all the listeners will trigger that same amount of times

store.subscribe(() => console.log('hello'))

store.dispatch(...) // 'hello'
store.dispatch(...) // 'hello'

If for example there are many dispatch calls that trigger react DOM redraw it would be much better if it only happened once since it might save itself some extra computation

store.subscribe(() => console.log('hello'))

// this is completely made up, it would dispatch all these actions in a bulk
store.batch([
  (action1),
  (action2),
  ...
]);
// 'hello'

Or maybe if we stack up dispatches and clear that stack in the next tick the results might be different

store.subscribe(() => console.log('hello'))
store.lazyDispatch(...)
store.lazyDispatch(...)
// async 'hello' log in the next tick
markerikson commented 7 years ago

This is something that can be handled in "userland", outside of core Redux itself. There's a number of approaches that can be used - middleware, higher order reducers, store enhancers, etc. See http://redux.js.org/docs/faq/Actions.html#actions-multiple-actions and http://redux.js.org/docs/faq/Performance.html#performance-update-events for further info, and in particular the discussion in #1813 . My Redux addons catalog lists the various change subscription libraries out there, including an enhancer that supports dispatching arrays.