({
collapsed = true,
filter = (mutation, stateBefore, stateAfter) => true,
transformer = state => state,
mutationTransformer = mut => mut,
actionFilter = (action, state) => true,
actionTransformer = act => act,
logMutations = true,
logActions = true,
logger = console
} = {})
| 3 | import { deepCopy } from '../util' |
| 4 | |
| 5 | export function createLogger ({ |
| 6 | collapsed = true, |
| 7 | filter = (mutation, stateBefore, stateAfter) => true, |
| 8 | transformer = state => state, |
| 9 | mutationTransformer = mut => mut, |
| 10 | actionFilter = (action, state) => true, |
| 11 | actionTransformer = act => act, |
| 12 | logMutations = true, |
| 13 | logActions = true, |
| 14 | logger = console |
| 15 | } = {}) { |
| 16 | return store => { |
| 17 | let prevState = deepCopy(store.state) |
| 18 | |
| 19 | if (typeof logger === 'undefined') { |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | if (logMutations) { |
| 24 | store.subscribe((mutation, state) => { |
| 25 | const nextState = deepCopy(state) |
| 26 | |
| 27 | if (filter(mutation, prevState, nextState)) { |
| 28 | const formattedTime = getFormattedTime() |
| 29 | const formattedMutation = mutationTransformer(mutation) |
| 30 | const message = `mutation ${mutation.type}${formattedTime}` |
| 31 | |
| 32 | startMessage(logger, message, collapsed) |
| 33 | logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState)) |
| 34 | logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation) |
| 35 | logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState)) |
| 36 | endMessage(logger) |
| 37 | } |
| 38 | |
| 39 | prevState = nextState |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | if (logActions) { |
| 44 | store.subscribeAction((action, state) => { |
| 45 | if (actionFilter(action, state)) { |
| 46 | const formattedTime = getFormattedTime() |
| 47 | const formattedAction = actionTransformer(action) |
| 48 | const message = `action ${action.type}${formattedTime}` |
| 49 | |
| 50 | startMessage(logger, message, collapsed) |
| 51 | logger.log('%c action', 'color: #03A9F4; font-weight: bold', formattedAction) |
| 52 | endMessage(logger) |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function startMessage (logger, message, collapsed) { |
| 60 | const startMessage = collapsed |
no test coverage detected
searching dependent graphs…