<commit_before>"use strict";

var Push = {

    MAX_RETRIES: 1,
    actualRetries: 0,

    ad: "localhost:8080",
    ad_ws: null,
    ad_http: null,

    ws: {
        connection: null,
        ready: false,
    },

    token: null,

    /**
     * This initializes the app
     */
    init: function() {
        this.ad_ws = "ws://" + this.ad;
        this.ad_http = "http://" + this.ad;
        // TODO: get from mozSettings the "token"
        /*savedToken = getToken();
        if (savedToken !== null) {
            this.token = savedToken;
        }
        */
        // Assign UI elements to variables
        this.registerDeviceButton = document.getElementById("buttonRegisterDevice");
        this.unregisterDeviceButton = document.getElementById("buttonUnregisterDevice");
        this.registerAppButton = document.getElementById("buttonRegisterApp");
        this.logArea = document.getElementById("logarea");
        this.clearButton = document.getElementById("buttonClear");
        this.registerDeviceButton.disabled = false;
        this.registerDeviceButton.addEventListener('click', this.registerDevice.bind(this));
        this.unregisterDeviceButton.addEventListener('click', this.unregisterDevice.bind(this));
        this.clearButton.addEventListener('click', this.onclear.bind(this));
    },

    onclear: function() {
        this.logArea.innerHTML = "";
    },

    /**
     * Function called to register the device to the notification server
     */
    registerDevice: function() {
        if (this.token === null) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4){
                    if(xmlhttp.status == 200) {
                        Push.token = xmlhttp.responseText;
                        Push.onReceivedToken();
                    } else {
                        Push.logMessage("[TOK] The notification server is not working")
                    }
                }
            };
            xmlhttp.open("GET", this.ad_http + "/token",true);
            xmlhttp.send(null);
        }
    },

    unregisterDevice: function() {
        this.onDeleteToken();
    },

    onReceivedToken: function() {
        if(this.token === null) {
            return;
        }
        this.logMessage("[TOK] Token from the notification server (or saved)" + this.token);
        this.ws.connection = new WebSocket(this.ad_ws, "push-notification");
        this.logMessage("[WS] Opening websocket to " + this.ad_ws);
        this.ws.connection.onopen = this.onOpenWebsocket.bind(this);
        this.ws.connection.onclose = this.onCloseWebsocket.bind(this);
        this.ws.connection.onerror = this.onCloseWebsocket.bind(this);
        this.registerDeviceButton.disabled=true;
        this.unregisterDeviceButton.disabled=false;
    },

    onDeleteToken: function() {
        this.logMessage("[TOK] We are deleting our token!");
        this.token = <PASSWORD>;
        this.ws.connection.close();
        this.logMessage("[TOK] Token deleted")
        this.registerDeviceButton.disabled=false;
        this.unregisterDeviceButton.disabled=true;
    },

    onOpenWebsocket: function() {
        this.logMessage("[WS] Opened connection to " + this.ad);
        this.ws.ready = true;
        this.logMessage("[REG] Started registration to the notification server");
        this.ws.connection.send('{"data": {"token":"' + this.token + '"}, "command":"register/node"}');
        this.ws.connection.addEventListener('onmessage', function(e) {
            Push.logMessage("[MSG], message received --- " + e.data);
        });
    },

    onErrorWebsocket: function(e) {
        this.logMessage("[WS] Error in websocket in " + this.ad + " with error " + e.error);
    },

    onCloseWebsocket: function(e) {
        this.logMessage("[WS] Closed connection to " + this.ad + " with code " + e.code + " and reason " + e.reason);
        this.ws.ready = false;
        //HACK to reconnect
        if (this.MAX_RETRIES>++this.actualRetries) {
            this.onReceivedToken();
        } else {
            this.logMessage("[WS] MAX_RETRIES reached. Stopping")
        }
    },

    /**
     * Logs every message from the websocket
     */
     logMessage: function(message) {<commit_msg>Fixed event registration<commit_after>"use strict";

var Push = {

    MAX_RETRIES: 1,
    actualRetries: 0,

    ad: "localhost:8080",
    ad_ws: null,
    ad_http: null,

    ws: {
        connection: null,
        ready: false,
    },

    token: null,

    /**
     * This initializes the app
     */
    init: function() {
        this.ad_ws = "ws://" + this.ad;
        this.ad_http = "http://" + this.ad;
        // TODO: get from mozSettings the "token"
        /*savedToken = getToken();
        if (savedToken !== null) {
            this.token = savedToken;
        }
        */
        // Assign UI elements to variables
        this.registerDeviceButton = document.getElementById("buttonRegisterDevice");
        this.unregisterDeviceButton = document.getElementById("buttonUnregisterDevice");
        this.registerAppButton = document.getElementById("buttonRegisterApp");
        this.logArea = document.getElementById("logarea");
        this.clearButton = document.getElementById("buttonClear");
        this.registerDeviceButton.disabled = false;
        this.registerDeviceButton.addEventListener('click', this.registerDevice.bind(this));
        this.unregisterDeviceButton.addEventListener('click', this.unregisterDevice.bind(this));
        this.clearButton.addEventListener('click', this.onclear.bind(this));
    },

    onclear: function() {
        this.logArea.innerHTML = "";
    },

    /**
     * Function called to register the device to the notification server
     */
    registerDevice: function() {
        if (this.token === null) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4){
                    if(xmlhttp.status == 200) {
                        Push.token = xmlhttp.responseText;
                        Push.onReceivedToken();
                    } else {
                        Push.logMessage("[TOK] The notification server is not working")
                    }
                }
            };
            xmlhttp.open("GET", this.ad_http + "/token",true);
            xmlhttp.send(null);
        }
    },

    unregisterDevice: function() {
        this.onDeleteToken();
    },

    onReceivedToken: function() {
        if(this.token === null) {
            return;
        }
        this.logMessage("[TOK] Token from the notification server (or saved)" + this.token);
        this.ws.connection = new WebSocket(this.ad_ws, "push-notification");
        this.logMessage("[WS] Opening websocket to " + this.ad_ws);
        this.ws.connection.onopen = this.onOpenWebsocket.bind(this);
        this.ws.connection.onclose = this.onCloseWebsocket.bind(this);
        this.ws.connection.onerror = this.onCloseWebsocket.bind(this);
        this.registerDeviceButton.disabled=true;
        this.unregisterDeviceButton.disabled=false;
    },

    onDeleteToken: function() {
        this.logMessage("[TOK] We are deleting our token!");
        this.token = null;
        this.ws.connection.close();
        this.logMessage("[TOK] Token deleted")
        this.registerDeviceButton.disabled=false;
        this.unregisterDeviceButton.disabled=true;
    },

    onOpenWebsocket: function() {
        this.logMessage("[WS] Opened connection to " + this.ad);
        this.ws.ready = true;
        this.logMessage("[REG] Started registration to the notification server");
        this.ws.connection.send('{"data": {"token":"' + this.token + '"}, "command":"register/node"}');
        this.ws.connection.onmessage = function(e) {
            Push.logMessage("[MSG] message received --- " + e.data);
        };
    },

    onErrorWebsocket: function(e) {
        this.logMessage("[WS] Error in websocket in " + this.ad + " with error " + e.error);
    },

    onCloseWebsocket: function(e) {
        this.logMessage("[WS] Closed connection to " + this.ad + " with code " + e.code + " and reason " + e.reason);
        this.ws.ready = false;
        //HACK to reconnect
        if (this.MAX_RETRIES>++this.actualRetries) {
            this.onReceivedToken();
        } else {
            this.logMessage("[WS] MAX_RETRIES reached. Stopping")
        }
    },

    /**
     * Logs every message from the websocket
     */
     logMessage: function(message) {
{'fraction_non_alphanumeric': 0.13095099522756373, 'fraction_numerical': 0.0023280176929344663, 'mean_word_length': 2.198808637379002, 'pattern_counts': {'":': 6, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '2929216', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2370, 'n_words': 468}