<commit_before>/**
 * Movim Websocket
 * 
 * This file define the websocket behaviour and handle its connection
 */ 

WebSocket.prototype.unregister = function() {
    this.send(JSON.stringify({'func' : 'unregister'}));
};

WebSocket.prototype.admin = function(key) {
    this.send(JSON.stringify({'func' : 'admin', 'key' : key}));
};

/**
 * @brief Definition of the MovimWebsocket object
 * @param string error 
 */

var MovimWebsocket = {
    connection: null,
    attached: null,
    unregistered: false,
    
    launchAttached : function() {
        for(var i = 0; i < this.attached.length; i++) {
            this.attached[i]();
        }
    },

    init : function() {
        this.connection = new WebSocket('ws://' + BASE_HOST + ':8080');
        this.attached = new Array();
        
        this.connection.onopen = function(e) {
            console.log("Connection established!");
            MovimWebsocket.launchAttached();
        };

        this.connection.onmessage = function(e) {
            data = pako.ungzip(base64_decode(e.data), { to: 'string' });

            var obj = JSON.parse(data);

            if(obj != null) {
                if(obj.func == 'registered') {
                    MovimWebsocket.launchAttached();
                }

                if(obj.func == 'disconnected') {
                    movim_disconnect();
                }
            }

            MovimWebsocket.handle(data);
        };

        this.connection.onclose = function(e) {
            console.log("Connection closed by the server or session closed");
            if(e.code == 1006 || e.code == 1000) {
                if(MovimWebsocket.unregistered == false) {
                    movim_disconnect();
                } else {
                    MovimWebsocket.unregistered = false;
                    MovimWebsocket.init();
                }
            }
        };

        this.connection.onerror = function(e) {
            console.log("Connection error!");
            // We prevent the onclose launch
            this.onclose = null;
        };
    },

    send : function(widget, func, params) {
        this.connection.send(
            JSON.stringify(
                {'func' : 'message', 'body' :
                    {
                        'widget' : widget,
                        'func' : func,
                        'params' : params
                    }
                }
            )
        );
    },

    attach : function(func) {
        if(typeof(func) === "function") {
            this.attached.push(func);
        }
    },

    handle : function(json) {
        var funcalls = JSON.parse(json);
        if(funcalls != null) {
            for(h = 0; h < funcalls.length; h++) {
                var funcall = funcalls[h];
                console.log(funcall);
                if(funcall.func != null && (typeof window[funcall.func] == 'function')) {
                    try {
                        window[funcall.func].apply(null, funcall.params);
                    } catch(err) {
                        console.log("Error caught: " + err.toString() + " - " + funcall.func + ":" + JSON.stringify(funcall.params));
                    }
                } else if(funcall.func != null) {
                    var funcs  = funcall.func.split('.');
                    var called = funcs[0];
                    if(typeof window[called] == 'object') {
                        window[funcs[0]][funcs[1]].apply(null, funcall.params);
                    }
                }
            }
        }
    },

    unregister : function(reload) {
        if(reload == false) this.unregistered = true;
        this.connection.unregister();
    }
}

function remoteUnregister()
{
    MovimWebsocket.unregister(false);
}

function remoteUnregisterReload()
{
    MovimWebsocket.unregister(true);
}

window.onbeforeunload = function() {
    MovimWebsocket.connection.onclose = function () {}; // disable onclose handler first
    MovimWebsocket.connection.close()
};

// And we start it
MovimWebsocket.init();
<commit_msg>- Comment a console.log<commit_after>/**
 * Movim Websocket
 * 
 * This file define the websocket behaviour and handle its connection
 */ 

WebSocket.prototype.unregister = function() {
    this.send(JSON.stringify({'func' : 'unregister'}));
};

WebSocket.prototype.admin = function(key) {
    this.send(JSON.stringify({'func' : 'admin', 'key' : key}));
};

/**
 * @brief Definition of the MovimWebsocket object
 * @param string error 
 */

var MovimWebsocket = {
    connection: null,
    attached: null,
    unregistered: false,
    
    launchAttached : function() {
        for(var i = 0; i < this.attached.length; i++) {
            this.attached[i]();
        }
    },

    init : function() {
        this.connection = new WebSocket('ws://' + BASE_HOST + ':8080');
        this.attached = new Array();
        
        this.connection.onopen = function(e) {
            console.log("Connection established!");
            MovimWebsocket.launchAttached();
        };

        this.connection.onmessage = function(e) {
            data = pako.ungzip(base64_decode(e.data), { to: 'string' });

            var obj = JSON.parse(data);

            if(obj != null) {
                if(obj.func == 'registered') {
                    MovimWebsocket.launchAttached();
                }

                if(obj.func == 'disconnected') {
                    movim_disconnect();
                }
            }

            MovimWebsocket.handle(data);
        };

        this.connection.onclose = function(e) {
            console.log("Connection closed by the server or session closed");
            if(e.code == 1006 || e.code == 1000) {
                if(MovimWebsocket.unregistered == false) {
                    movim_disconnect();
                } else {
                    MovimWebsocket.unregistered = false;
                    MovimWebsocket.init();
                }
            }
        };

        this.connection.onerror = function(e) {
            console.log("Connection error!");
            // We prevent the onclose launch
            this.onclose = null;
        };
    },

    send : function(widget, func, params) {
        this.connection.send(
            JSON.stringify(
                {'func' : 'message', 'body' :
                    {
                        'widget' : widget,
                        'func' : func,
                        'params' : params
                    }
                }
            )
        );
    },

    attach : function(func) {
        if(typeof(func) === "function") {
            this.attached.push(func);
        }
    },

    handle : function(json) {
        var funcalls = JSON.parse(json);
        if(funcalls != null) {
            for(h = 0; h < funcalls.length; h++) {
                var funcall = funcalls[h];
                //console.log(funcall);
                if(funcall.func != null && (typeof window[funcall.func] == 'function')) {
                    try {
                        window[funcall.func].apply(null, funcall.params);
                    } catch(err) {
                        console.log("Error caught: " + err.toString() + " - " + funcall.func + ":" + JSON.stringify(funcall.params));
                    }
                } else if(funcall.func != null) {
                    var funcs  = funcall.func.split('.');
                    var called = funcs[0];
                    if(typeof window[called] == 'object') {
                        window[funcs[0]][funcs[1]].apply(null, funcall.params);
                    }
                }
            }
        }
    },

    unregister : function(reload) {
        if(reload == false) this.unregistered = true;
        this.connection.unregister();
    }
}

function remoteUnregister()
{
    MovimWebsocket.unregister(false);
}

function remoteUnregisterReload()
{
    MovimWebsocket.unregister(true);
}

window.onbeforeunload = function() {
    MovimWebsocket.connection.onclose = function () {}; // disable onclose handler first
    MovimWebsocket.connection.close()
};

// And we start it
MovimWebsocket.init();

{'fraction_non_alphanumeric': 0.13195470211718366, 'fraction_numerical': 0.004677498769079271, 'mean_word_length': 1.5154798761609907, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991911', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2223, 'n_words': 413}