import { cancel , delay , fork , put , takeEvery } from 'redux-saga/effects' ; example const addIds = ( oldIds , { payload : { ids } } ) => { const oldIdsSet = new Set ( oldIds ) ; ids . forEach ( id => oldIdsSet . add ( id ) ) ; return Array . from ( oldIdsSet ) ; } ; export const finalizeFactory = ( tasks , accumulations ) => see https function * finalize ( key , actionCreator ) { yield delay ( 50 ) ; const accumulatedValue = accumulations [ key ] ; delete accumulations [ key ] ; const action = actionCreator ( key , accumulatedValue ) ; yield put ( action ) ; delete tasks [ key ] ; } ; export const accumulateFactory = ( tasks , accumulations , finalize ) => example example function * accumulate ( action ) { const key = action . meta . accumulateKey || action . payload . resource ; if ( tasks [ key ] ) { yield cancel ( tasks [ key ] ) ; } const accumulateValues = action . meta . accumulateValues || addIds ; accumulations [ key ] = accumulateValues ( accumulations [ key ] , action ) ; tasks [ key ] = yield fork ( finalize , key , action . meta . accumulate ) ; } ; export default function * ( ) { /**
     * Example
     *
     * const accumulations = {
     *   posts: [4, 7, 345 ], // a CRUD_GET_MANY_ACCUMULATE action
     *   authors: [23, 47, 78 ], // another CRUD_GET_MANY_ACCUMULATE action
     *   '{"resource":"authors", "pagination":{"page":1,"perPage":10},"sort":{"field":"id","order":"DESC"},"filter":{}}': true, // a CRUD_GET_MATCHING_ACCUMULATE action
     *   '{"resource":"authors", "pagination":{"page":1,"perPage":10},"sort":{"field":"id","order":"DESC"},"filter":{"hasValidEmail":true}}': true, // another CRUD_GET_MATCHING_ACCUMULATE action
     * }
     */ const accumulations = { } ; const tasks = { } ; yield takeEvery ( action => action . meta && action . meta . accumulate , accumulateFactory ( tasks , accumulations , finalizeFactory ( tasks , accumulations ) ) ) ; }