initial_state = dict( instrument_list={}, connection_list={}, ) class ActionTypes(object): # Initiate update of instrument list. For every instrument, ADD_INSTRUMENT is triggered UPDATE_LIST = 'instrument_manager/UPDATE_LIST' # Add an instrument to the instrument list. ADD_INSTRUMENT = 'instrument_manager/ADD_INSTRUMENT' # Try to connect an instrument. On success, trigger ADD_CONNECTION. CONNECT = 'instrument_manager/CONNECT' # Add an instrument from the instrument list to the connection list. ADD_CONNECTION = 'instrument_manager/ADD_CONNECTION' # Try to disconnect an instrument. On success, trigger REMOVE_CONNECTION. DISCONNECT = 'instrument_manager/DISCONNECT' # Remove a disconnected instrument from the connection list. REMOVE_CONNECTION = 'instrument_manager/REMOVE_CONNECTION' class ActionCreators(object): @staticmethod def update_list(): return dict( type=ActionTypes.UPDATE_LIST ) @staticmethod def add_instrument(instrument): return dict( type=ActionTypes.ADD_INSTRUMENT, instrument=instrument ) @staticmethod def connect(instrument): return dict( type=ActionTypes.CONNECT, id=instrument ) @staticmethod def add_connection(instrument): return dict( type=ActionTypes.ADD_CONNECTION, instrument=instrument ) @staticmethod def disconnect(instrument): return dict( type=ActionTypes.DISCONNECT, id=instrument ) @staticmethod def remove_connection(instrument): return dict( type=ActionTypes.REMOVE_CONNECTION, id=instrument ) def reducer(state=None, action=None): if state is None: state = initial_state if not (isinstance(action, dict) and 'type' in action): return state # if action['type'] == ActionTypes.UPDATE_LIST: # state = action['value'] # elif action['type'] == ActionTypes.CONNECT: # state += 1 # elif action['type'] == ActionTypes.DISCONNECT: # state -= 1 return state if __name__ == '__main__': import unittest unittest.main()