2019-07-24 13:17:16 +00:00
|
|
|
from tfw.internals.networking import Scope
|
2019-05-26 16:26:33 +00:00
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from .message_storage import FrontendMessageStorage
|
2019-06-27 12:36:18 +00:00
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
|
|
|
|
class FrontendHandler:
|
2019-07-12 21:25:16 +00:00
|
|
|
keys = ['message', 'queueMessages', 'dashboard', 'console']
|
2019-05-26 16:26:33 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2019-07-12 21:25:16 +00:00
|
|
|
self.server_connector = None
|
|
|
|
self.keys = [*type(self).keys, 'recover']
|
|
|
|
self._frontend_message_storage = FrontendMessageStorage(type(self).keys)
|
2019-05-26 16:26:33 +00:00
|
|
|
|
2019-07-12 21:25:16 +00:00
|
|
|
def send_message(self, message):
|
|
|
|
self.server_connector.send_message(message, scope=Scope.WEBSOCKET)
|
|
|
|
|
|
|
|
def handle_event(self, message, _):
|
2019-05-26 16:26:33 +00:00
|
|
|
self._frontend_message_storage.save_message(message)
|
|
|
|
if message['key'] == 'recover':
|
|
|
|
self.recover_frontend()
|
2019-06-28 14:50:36 +00:00
|
|
|
self.send_message(message)
|
2019-05-26 16:26:33 +00:00
|
|
|
|
|
|
|
def recover_frontend(self):
|
|
|
|
for message in self._frontend_message_storage.messages:
|
|
|
|
self.send_message(message)
|