github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-magicmirror/web/src/ducks/connection.js (about) 1 import { List, Map } from 'immutable'; 2 import { defineAction } from 'redux-define'; 3 4 const c = defineAction( 5 'connection', 6 ['CONNECTED', 'DISCONNECTED', 'ERROR'], 7 ); 8 9 const defaultState = Map({ 10 connected: null, 11 error: null, 12 }); 13 14 // Actions 15 export function connected() { 16 return { type: c.CONNECTED }; 17 } 18 19 export function disconnected() { 20 return (dispatch, getState) => { 21 if (getState().getIn(['connection', 'connected']) !== false) { 22 dispatch({ type: c.DISCONNECTED }); 23 } 24 }; 25 } 26 27 export function error(err) { 28 return { type: c.ERROR, error: err }; 29 } 30 31 // Reducer 32 let idCount = 0; 33 export default function reducer(state = defaultState, action) { 34 switch (action.type) { 35 case c.CONNECTED: { 36 return state 37 .set('error', null) 38 .set('connected', true); 39 } 40 case c.DISCONNECTED: { 41 return state 42 .set('connected', false); 43 } 44 case c.ERROR: { 45 return state 46 .set('error', action.error); 47 } 48 default: return state; 49 } 50 }