Passed
Push — develop ( 46f53c...998be7 )
by Nikolay
05:42 queued 10s
created

sites/admin-cabinet/assets/js/src/main/connection-check-worker.js   A

Complexity

Total Complexity 9
Complexity/F 1.8

Size

Lines of Code 70
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 24
dl 0
loc 70
rs 10
c 0
b 0
f 0
mnd 4
bc 4
fnc 5
bpm 0.8
cpm 1.8
noi 1
1
/*
2
 * MikoPBX - free phone system for small business
3
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along with this program.
16
 * If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
/* global globalDebugMode */
20
21
/**
22
 * The connectionCheckWorker object is responsible for checking
23
 * the connection status with backend
24
 *
25
 * @module connectionCheckWorker
26
 */
27
const connectionCheckWorker = {
28
29
    // Counter for error counts
30
    errorCounts: 0,
31
32
    /**
33
     * jQuery object for the no connection dimmer element.
34
     * @type {jQuery}
35
     */
36
    $connectionDimmer: $('#connection-dimmer'),
37
38
    /**
39
     * EventSource object for the connection check.
40
     * @type {EventSource}
41
     */
42
    eventSource: null,
43
44
    /**
45
     * Initialize the connection check worker.
46
     */
47
    initialize() {
48
        connectionCheckWorker.eventSource = new EventSource('/connection-check');
0 ignored issues
show
Bug introduced by
The variable EventSource seems to be never declared. If this is a global, consider adding a /** global: EventSource */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
49
50
        connectionCheckWorker.eventSource.addEventListener('error', function(event) {
51
            connectionCheckWorker.cbAfterResponse(false);
52
        });
53
54
        connectionCheckWorker.eventSource.addEventListener('open', function(event) {
55
            connectionCheckWorker.cbAfterResponse(true);
56
        });
57
    },
58
59
    /**
60
     * Callback function after receiving the response from the connection check.
61
     * @param {boolean} result - The result of the connection check.
62
     */
63
    cbAfterResponse(result) {
64
        if (result === true) {
65
            // If the connection is successful, hide the connection dimmer
66
            connectionCheckWorker.$connectionDimmer.dimmer('hide');
67
68
            // Reload the page if the error count exceeds a certain threshold
69
            if (connectionCheckWorker.errorCounts > 5) {
70
                window.location.reload();
71
            }
72
73
            // Reset the error count
74
            connectionCheckWorker.errorCounts = 0;
75
76
        } else if (connectionCheckWorker.errorCounts > 3) {
77
78
            // If the connection is unsuccessful and error count exceeds a threshold, show the connection dimmer
79
            connectionCheckWorker.$connectionDimmer.dimmer('show');
80
81
            // Increment the error count
82
            connectionCheckWorker.errorCounts += 1;
83
        } else {
84
85
            // Increment the error count
86
            connectionCheckWorker.errorCounts += 1;
87
        }
88
    },
89
};
90
91
// When the document is ready, initialize the connection check worker
92
$(document).ready(() => {
93
    if (!globalDebugMode) {
94
        connectionCheckWorker.initialize();
95
    }
96
});
97