Last active
February 13, 2019 05:08
-
-
Save outbreak/915c05da976492057330f18f5b900f5b to your computer and use it in GitHub Desktop.
Debug effects and events for effector (https://github.com/zerobias/effector)
This file contains hidden or 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 { | |
createDomain | |
} from 'effector' | |
import useDomainLogger from './useDomainLogger.js' | |
const DEBUG = true | |
const app = createDomain('app') | |
DEBUG && useDomainLogger(app) | |
const doEvent = app.event('event1') | |
const doEffect = app.effect('effect1').use((params) => { | |
return Boolean(params) ? Promise.resolve(params) : Promise.reject(params) | |
}) | |
doEvent(true) | |
doEffect(1) | |
doEffect(0) |
This file contains hidden or 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 logEvent = (name, type, payload, isError = false) => { | |
const log = isError ? console.error : console.debug | |
console.groupCollapsed(name) | |
log('type', type) | |
log('payload', payload) | |
console.groupEnd() | |
} | |
const useDomainLogger = domain => { | |
const domainName = `[${domain.getType()}]` | |
domain.onCreateEvent(event => { | |
event.watch((payload, type) => { | |
logEvent(`${domainName} event ${type}`, type, payload) | |
}) | |
}) | |
domain.onCreateEffect(effect => { | |
effect.watch((payload, type) => { | |
logEvent(`${domainName} effect ${type}`, type, payload) | |
}) | |
effect.done.watch((payload, type) => { | |
logEvent(`${domainName} effect ${type}`, type, payload) | |
}) | |
effect.fail.watch((payload, type) => { | |
logEvent(`${domainName} effect ${type}`, type, payload, true) | |
}) | |
}) | |
domain.onCreateDomain(useDomainLogger) | |
} | |
export default useDomainLogger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment