Refactor ZMQWSProxy.__init__

This commit is contained in:
Kristóf Tóth 2018-07-12 15:55:26 +02:00
parent da98576260
commit f6cc7ae495

View File

@ -16,8 +16,14 @@ class ZMQWebSocketProxy(WebSocketHandler):
def initialize(self, **kwargs): # pylint: disable=arguments-differ def initialize(self, **kwargs): # pylint: disable=arguments-differ
self._event_handler_connector = kwargs['event_handler_connector'] self._event_handler_connector = kwargs['event_handler_connector']
self._message_handlers = kwargs.get('message_handlers', [])
self._proxy_filters = kwargs.get('proxy_filters', []) self.init_callback_lists_from(
kwargs,
[
'message_handlers',
'proxy_filters'
]
)
self.proxy_eventhandler_to_websocket = TFWProxy( self.proxy_eventhandler_to_websocket = TFWProxy(
self.send_eventhandler_message, self.send_eventhandler_message,
@ -28,7 +34,21 @@ class ZMQWebSocketProxy(WebSocketHandler):
self.send_eventhandler_message self.send_eventhandler_message
) )
proxies = (self.proxy_eventhandler_to_websocket, self.proxy_websocket_to_eventhandler) self.subscribe_proxy_callbacks()
def init_callback_lists_from(self, init_kwargs, callback_lists):
for callback_list in callback_lists:
setattr(
self,
f'_{callback_list}',
init_kwargs.get(callback_list, [])
)
def subscribe_proxy_callbacks(self):
proxies = (
self.proxy_eventhandler_to_websocket,
self.proxy_websocket_to_eventhandler
)
for proxy in proxies: for proxy in proxies:
proxy.proxy_filters.subscribe_callbacks(*self._proxy_filters) proxy.proxy_filters.subscribe_callbacks(*self._proxy_filters)
proxy.proxy_callbacks.subscribe_callbacks(*self._message_handlers) proxy.proxy_callbacks.subscribe_callbacks(*self._message_handlers)