Last active
April 17, 2019 13:06
-
-
Save smashercosmo/6dc9c26ce1e2e3dafde5 to your computer and use it in GitHub Desktop.
Batch actions logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import createLogger from 'redux-logger'; | |
const actionTransformer = action => { | |
if (action.type === 'BATCHING_REDUCER.BATCH') { | |
action.payload.type = action.payload.reduce((result, next) => { | |
const prefix = result ? result + ' => ' : ''; | |
return prefix + next.type; | |
}, ''); | |
return action.payload; | |
} | |
return action; | |
}; | |
const level = 'info'; | |
const logger = {}; | |
for (const method in console) { | |
if (typeof console[method] === 'function') { | |
logger[method] = console[method].bind(console); | |
} | |
} | |
logger[level] = function levelFn(...args) { | |
const lastArg = args.pop(); | |
if (Array.isArray(lastArg)) { | |
return lastArg.forEach(item => { | |
console[level].apply(console, [...args, item]); | |
}); | |
} | |
console[level].apply(console, arguments); | |
}; | |
export default createLogger({ | |
level, | |
actionTransformer, | |
logger | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {enableBatching} from 'redux-batched-actions'; | |
import {combineReducers} from 'redux'; | |
import reducer1 from './reducer1'; | |
import reducer2 from './reducer2'; | |
import reducer3 from './reducer3'; | |
const appReducer = combineReducers({ | |
reducer1, | |
reducer2, | |
reducer3 | |
}); | |
export default enableBatching(appReducer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import reducer from './reducers'; | |
import {createStore, applyMiddleware, compose} from 'redux'; | |
const middlewares = []; | |
if (process.env.NODE_ENV === 'development') { | |
const logger = require('./logger'); | |
middlewares.push(logger); | |
} | |
const storeEnhancers = [applyMiddleware(...middlewares)]; | |
export default const store = compose(...storeEnhancers)(createStore)(reducer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This seems to be out there for a long time, but this is awesome! Thanks for sharing!