<commit_before>const Header              = require('./header');
const BinarySocket        = require('./socket');
const BinarySocketGeneral = require('./socket_general');
const getElementById      = require('../../_common/common_functions').getElementById;
const localize            = require('../../_common/localize').localize;

/*
 * Monitors the network status and initialises the WebSocket connection
 * 1. online : check the WS status (init/send: blink after timeout, open/message: online)
 * 2. offline: it is offline
 */
const NetworkMonitor = (() => {
    const status_config = {
        online  : { class: 'online',  tooltip: localize('Online') },
        offline : { class: 'offline', tooltip: localize('Offline') },
        blinking: { class: 'pulser',  tooltip: localize('Connecting') },
    };
    const pendings = {};
    const pending_keys = {
        ws_init   : 'ws_init',
        ws_request: 'ws_request',
    };
    const pending_timeouts = {
        [pending_keys.ws_init]   : 5000,
        [pending_keys.ws_request]: 10000,
    };

    let ws_config,
        el_status,
        network_status;

    const init = () => {
        ws_config = $.extend({ wsEvent }, BinarySocketGeneral.initOptions());
        el_status = getElementById('network_status');

        if ('onLine' in navigator) {
            window.addEventListener('online',  setStatus);
            window.addEventListener('offline', setStatus);
        } else { // if not supported, default to online and fallback to WS checks
            navigator.onLine = true;
        }

        if (isOnline()) {
            BinarySocket.init(ws_config);
        }

        setStatus(isOnline() ? 'online' : 'offline');
    };

    const isOnline = () => navigator.onLine;

    const wsReconnect = () => {
        if (isOnline() && BinarySocket.hasReadyState(2, 3)) { // CLOSING or CLOSED
            BinarySocket.init(ws_config);
            clearPendings();
        } else {
            BinarySocket.send({ ping: 1 }); // trigger a request to get stable status sooner
        }
    };

    const setStatus = (status) => {
        if (!isOnline() || status === pending_keys.ws_init) {
            network_status = 'offline';
            Header.displayNotification(localize('Connection error: Please check your internet connection.'), true, 'CONNECTION_ERROR');
        } else if (status === pending_keys.ws_request || network_status === 'offline') {
            network_status = 'blinking';<commit_msg>Consider all pendings as blinking<commit_after>const Header              = require('./header');
const BinarySocket        = require('./socket');
const BinarySocketGeneral = require('./socket_general');
const getElementById      = require('../../_common/common_functions').getElementById;
const localize            = require('../../_common/localize').localize;

/*
 * Monitors the network status and initialises the WebSocket connection
 * 1. online : check the WS status (init/send: blink after timeout, open/message: online)
 * 2. offline: it is offline
 */
const NetworkMonitor = (() => {
    const status_config = {
        online  : { class: 'online',  tooltip: localize('Online') },
        offline : { class: 'offline', tooltip: localize('Offline') },
        blinking: { class: 'pulser',  tooltip: localize('Connecting') },
    };
    const pendings = {};
    const pending_keys = {
        ws_init   : 'ws_init',
        ws_request: 'ws_request',
    };
    const pending_timeouts = {
        [pending_keys.ws_init]   : 5000,
        [pending_keys.ws_request]: 10000,
    };

    let ws_config,
        el_status,
        network_status;

    const init = () => {
        ws_config = $.extend({ wsEvent }, BinarySocketGeneral.initOptions());
        el_status = getElementById('network_status');

        if ('onLine' in navigator) {
            window.addEventListener('online',  setStatus);
            window.addEventListener('offline', setStatus);
        } else { // if not supported, default to online and fallback to WS checks
            navigator.onLine = true;
        }

        if (isOnline()) {
            BinarySocket.init(ws_config);
        }

        setStatus(isOnline() ? 'online' : 'offline');
    };

    const isOnline = () => navigator.onLine;

    const wsReconnect = () => {
        if (isOnline() && BinarySocket.hasReadyState(2, 3)) { // CLOSING or CLOSED
            BinarySocket.init(ws_config);
            clearPendings();
        } else {
            BinarySocket.send({ ping: 1 }); // trigger a request to get stable status sooner
        }
    };

    const setStatus = (status) => {
        if (!isOnline()) {
            network_status = 'offline';
            Header.displayNotification(localize('Connection error: Please check your internet connection.'), true, 'CONNECTION_ERROR');
        } else if (pending_keys[status] || network_status === 'offline') {
            network_status = 'blinking';
{'fraction_non_alphanumeric': 0.13376702581825575, 'fraction_numerical': 0.005692213864606627, 'mean_word_length': 2.486888731396173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5314059', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1328, 'n_words': 332}