Created
June 13, 2019 12:10
-
-
Save sergey-shpak/29f1f5733715fedb265662d2f327ee8c to your computer and use it in GitHub Desktop.
Hyperapp#2 dispatch 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 { h, app } from 'hyperapp' | |
import { logger } from 'helpers/logger' | |
const init = () => ({ initalized: true }) | |
app({ init }, logger(process.env.NODE_ENV==='development')) |
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
const isArray = Array.isArray | |
const isFunction = param => typeof param === 'function' | |
export const logger = (env, output = console.debug) => | |
env && (dispatch => (action, props, obj) => { | |
if(isFunction(action)) | |
output('Action', action.name, props, obj) | |
else if(isArray(action)){ | |
// 'tuple action' logged with next dispatch iteration | |
if(isArray(action[1])) | |
output('Effect', action[1][0].name, action[1][1]) | |
} else output('State', action) | |
return dispatch(action, props, obj) | |
}) | |
export default logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment