all files / src/components/ nocaster.js

33.33% Statements 6/18
0% Branches 0/6
100% Functions 0/0
35.29% Lines 6/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                                      
import gaze from 'gaze'
import { modifiedFile } from '../helpers/formatted-file.js'
import {
    SET_WATCHER_PREPARATIONS,
    CLEAR_WATCHER_PREPARATIONS
} from '../constants/actionTypes.js'
import { setModifiedFile, handleError } from '../actions/creators.js'
 
const Watcher = ({ dispatch, getState }) => {
    const state = getState()
    const watch = getStateLeaf(state, 'WATCH')
    const watching = getStateLeaf(state, 'WATCHING')
    if (watch && !watching) {
        dispatch({ type: SET_WATCHER_PREPARATIONS })
        gaze.on('all', (event, filepath) => {
            return dispatch(setModifiedFile(modifiedFile(event, filepath)))
        })
        gaze.on('ready', () => dispatch({ type: CLEAR_WATCHER_PREPARATIONS }))
        gaze.on('error', error => dispatch(handleError(error)))
    }
}
 
const getStateLeaf = (state, leaf) => {
    switch(leaf) {
        case 'WATCH':
            return state.watcher.watch
        case 'WATCHING':
            return state.watcher.watching
    }
}
 
export default Watcher