Add handler management

This commit is contained in:
Ludovic Stephan 2017-03-31 17:37:39 -03:00
parent 38bfccf331
commit ab15dbae76
2 changed files with 15 additions and 6 deletions

View file

@ -33,12 +33,12 @@ $(document).ready(function() {
class KfetWebsocket { class KfetWebsocket {
static get default_data() { static get defaults() {
return {"relative_url": "", "default_msg": {}}; return {"relative_url": "", "default_msg": {}, "handlers": []};
} }
constructor(data) { constructor(data) {
$.extend(this, this.constructor.default_data, data); $.extend(this, this.constructor.defaults, data);
} }
get url() { get url() {
@ -49,13 +49,22 @@ class KfetWebsocket {
return websocket_protocol+"://" + location_url + this.relative_url ; 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; var that = this;
this.socket = new ReconnectingWebSocket(this.url); this.socket = new ReconnectingWebSocket(this.url);
this.socket.onmessage = function(e) { this.socket.onmessage = function(e) {
var data = $.extend({}, that.default_msg, JSON.parse(e.data)); var data = $.extend({}, that.default_msg, JSON.parse(e.data));
handle_func(data); for (let handler of that.handlers) {
handler(data);
}
} }
} }
} }

View file

@ -1320,7 +1320,7 @@ $(document).ready(function() {
// Synchronization // Synchronization
// ----- // -----
OperationWebSocket.listen(function(data) { OperationWebSocket.add_handler(function(data) {
for (var i=0; i<data['opegroups'].length; i++) { for (var i=0; i<data['opegroups'].length; i++) {
if (data['opegroups'][i]['add']) { if (data['opegroups'][i]['add']) {
khistory.addOpeGroup(data['opegroups'][i]); khistory.addOpeGroup(data['opegroups'][i]);