From: Raphaƫl Jakse Date: Sun, 5 Apr 2020 12:59:28 +0000 (+0200) Subject: Refactor: simplify the part handling EventSource X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=a3e6bd245ff24d24bc20fb0f0de0a8a8589bc3da;p=trivabble.git Refactor: simplify the part handling EventSource --- diff --git a/trivabble.js b/trivabble.js index 860b64c..1cfd57c 100644 --- a/trivabble.js +++ b/trivabble.js @@ -818,34 +818,33 @@ } pollingServer = true; - thisRequestIsPolling = true; data.version = VERSION; - } - if (!thisRequestIsPolling) { - blockMove++; - } + if (window.EventSource) { + eventSource = new EventSource('/:trivabble/sse/' + JSON.stringify(data)); - if (thisRequestIsPolling && window.EventSource) { - eventSource = new EventSource('/:trivabble/sse/' + JSON.stringify(data)); + eventSource.onopen = connectionReady; - eventSource.onopen = connectionReady; + eventSource.onmessage = function (e) { + connectionReady(); + parseAndHandleReceivedData(e.data); + }; - eventSource.onmessage = function (e) { - connectionReady(); - parseAndHandleReceivedData(e.data); - }; + eventSource.onerror = function () { + pollingReady = false; + if (needsRestart) { + eventSource.close(); + } else if (!blockEventSourceAutoRetry) { + retryPolling(); + } + }; - eventSource.onerror = function () { - pollingReady = false; - if (needsRestart) { - eventSource.close(); - } else if (!blockEventSourceAutoRetry) { - retryPolling(); - } - }; + return; + } - return; + thisRequestIsPolling = true; + } else { + blockMove++; } var xhr = new XMLHttpRequest(); @@ -855,8 +854,6 @@ xhr.send(JSON.stringify(data)); xhr.onreadystatechange = function () { - // TODO error handling - if (xhr.readyState === 2 && thisRequestIsPolling) { connectionReady(); } else if (xhr.readyState === 3 && thisRequestIsPolling) {