Skip to content

Instantly share code, notes, and snippets.

@guimello
Created July 6, 2017 16:13
Show Gist options
  • Save guimello/2120ff301a367491ecf9fb0304b80bde to your computer and use it in GitHub Desktop.
Save guimello/2120ff301a367491ecf9fb0304b80bde to your computer and use it in GitHub Desktop.
Throttle with batch execution of actions - Redux Saga
import { buffers, delay } from 'redux-saga'
import { actionChannel, call, fork, take, flush } from 'redux-saga/effects'
import { flatten } from "ramda";
yield fork(function* () {
const leChannel = yield actionChannel("LOL", buffers.expanding(20));
while (true) {
const currentAction = yield take(leChannel);
yield call(delay, 1000);
const actions = yield flush(leChannel);
yield fork(function* (actions) {
console.log("le actions", actions);
}, flatten([currentAction, actions]))
}
});
// and then play around
(() => {
let a = 0;
const interval = setInterval(() => {
store.dispatch({type: "LOL", payload: a++})
if (a === 40) clearInterval(interval);
}, 400);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment