import * as signalActions from '../actions/signalActions' ; import { store } from '../store' ; import { signalNames } from '../store/factory/Signal' ; const ns = require ( '../util/ns' ) ; const getInVis = require ( '../util/immutable-utils' ) . getInVis ; function getSignal ( name ) { return getInVis ( store . getState ( ) , 'signals.' + ns ( name ) ) ; } module returns { Object } export function api ( ) { return getInVis ( store . getState ( ) , 'signals' ) . toJS ( ) ; } param { string } name param { * } val returns { Object } api . init = function ( name , val ) { store . dispatch ( signalActions . initSignal ( val , name ) ) ; } ; param { string } name returns { Object } api . reference = function ( name ) { return { signal : ns ( name ) } ; } ; function isDefault ( name ) { return signalNames . includes ( name ) ; } api . isDefault = isDefault ; param { string } name returns { * } api . get = function ( name ) { let ctrl = require ( './' ) , view = ctrl . view , signalObj = getSignal ( name ) , signalVal ; if ( view && typeof view . signal === 'function' ) { signalVal = view . signal ( ns ( name ) ) ; } return signalVal || signalObj . get ( 'value' ) ; } ; param { string } name param { * } val param { boolean } dispatch returns { Object } api . set = function ( name , val , dispatch ? ) { if ( dispatch === undefined ) { dispatch = true ; } const ctrl = require ( './' ) , view = ctrl . view ; if ( ! isDefault ( name ) && dispatch !== false ) { store . dispatch ( signalActions . setSignal ( val , name ) as any ) ; } if ( view && typeof view . signal === 'function' ) { view . signal ( ns ( name ) , val ) ; } } ; param { string } name returns { void } api . delete = function ( name ) { store . dispatch ( signalActions . unsetSignal ( null , name ) ) ; } ; param { string } name param { Object [ ] } def returns { Object } api . streams = function ( name , def ) { store . dispatch ( signalActions . setSignalStreams ( name , def ) ) ; } ; export default api ; ( window as any ) . sg = api ;