From ab15dbae76abb6fb9bfb596efdfb5588abcb2586 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Fri, 31 Mar 2017 17:37:39 -0300 Subject: [PATCH] Add handler management --- kfet/static/kfet/js/kfet.js | 19 ++++++++++++++----- kfet/templates/kfet/kpsul.html | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/kfet/static/kfet/js/kfet.js b/kfet/static/kfet/js/kfet.js index b8a44244..bdf5b69b 100644 --- a/kfet/static/kfet/js/kfet.js +++ b/kfet/static/kfet/js/kfet.js @@ -33,12 +33,12 @@ $(document).ready(function() { class KfetWebsocket { - static get default_data() { - return {"relative_url": "", "default_msg": {}}; + static get defaults() { + return {"relative_url": "", "default_msg": {}, "handlers": []}; } constructor(data) { - $.extend(this, this.constructor.default_data, data); + $.extend(this, this.constructor.defaults, data); } get url() { @@ -49,13 +49,22 @@ class KfetWebsocket { return websocket_protocol+"://" + location_url + this.relative_url ; } - listen(handle_func) { + add_handler(handler) { + if (!this.socket) + this.listen(); + + this.handlers.push(handler); + } + + listen() { var that = this; this.socket = new ReconnectingWebSocket(this.url); this.socket.onmessage = function(e) { var data = $.extend({}, that.default_msg, JSON.parse(e.data)); - handle_func(data); + for (let handler of that.handlers) { + handler(data); + } } } } diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index cf558647..4ac1354c 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -1320,7 +1320,7 @@ $(document).ready(function() { // Synchronization // ----- - OperationWebSocket.listen(function(data) { + OperationWebSocket.add_handler(function(data) { for (var i=0; i