import { Map } from 'immutable' ; import { ActionType , getType } from 'typesafe-actions' ; import { SignalValue } from 'vega-typings/types' ; import * as guideActions from '../actions/guideActions' ; import * as markActions from '../actions/markActions' ; import * as signalActions from '../actions/signalActions' ; import { Signal , SignalState } from '../store/factory/Signal' ; import { propSg } from '../util/prop-signal' ; const ns = require ( '../util/ns' ) ; function signalInit ( state , signal , value ) { return state . set ( signal , Signal ( { name : ns ( signal ) , value : value , _idx : state . size } ) ) ; } function setStreams ( state , signal , value ) { return state . setIn ( [ signal , 'on' ] , value ) ; } function initSignalsForMark ( state , action , props , propName ? ) { if ( ! props ) { return state ; } return Object . keys ( props ) . reduce ( function ( accState , key ) { if ( typeof props [ key ] === 'undefined' || typeof props [ key ] . value === 'undefined' ) { return accState ; } if ( action . type === getType ( guideActions . baseAddGuide ) ) { const type = 'guide' ; const name = propName + '_' + key ; const signalName = propSg ( action . meta , type , name ) ; const intermediateState = signalInit ( accState , signalName , props [ key ] . value ) ; return intermediateState ; } if ( action . type === getType ( markActions . baseAddMark ) ) { const type = action . payload . props . type ; const name = key ; const signalName = propSg ( action . meta , type , name ) ; const streams = action . payload . streams ; const intermediateState = signalInit ( accState , signalName , props [ key ] . value ) ; return streams && streams [ signalName ] ? setStreams ( intermediateState , signalName , streams [ signalName ] ) : intermediateState ; } } , state ) ; } function deleteSignalsForMark ( state , action ) { const markType = action . type === getType ( markActions . baseDeleteMark ) ? action . payload : 'guide' ; const markSignalRegex = new RegExp ( '^' + ns ( markType + '_' + action . meta ) ) ; return state . filter ( function ( value , key ) { return ! markSignalRegex . test ( key ) ; } ) ; } export function signalsReducer ( state , action ) { if ( typeof state === 'undefined' ) { return Map ( ) ; } if ( action . type === getType ( signalActions . initSignal ) ) { return signalInit ( state , action . meta , action . payload ) ; } if ( action . type === getType ( signalActions . baseSetSignal ) ) { return state . setIn ( [ action . meta , 'value' ] , action . payload ) ; } if ( action . type === getType ( signalActions . setSignalStreams ) ) { return setStreams ( state , action . meta , action . payload ) ; } if ( action . type === getType ( signalActions . unsetSignal ) ) { return state . remove ( action . meta ) ; } if ( action . type === getType ( markActions . baseAddMark ) ) { return initSignalsForMark ( state , action , action . payload . props . encode && action . payload . props . encode . update ) ; } if ( action . type === getType ( guideActions . baseAddGuide ) ) { const props = action . payload . encode ; Object . keys ( props ) . forEach ( function ( key ) { state = initSignalsForMark ( state , action , props [ key ] , key ) ; } ) ; return state ; } if ( action . type === getType ( markActions . baseDeleteMark ) || action . type === getType ( guideActions . deleteGuide ) ) { return deleteSignalsForMark ( state , action ) ; } return state ; }