Refactor: simplify the part handling EventSource
authorRaphaël Jakse <raphael.jakse@univ-grenoble-alpes.fr>
Sun, 5 Apr 2020 12:59:28 +0000 (14:59 +0200)
committerRaphaël Jakse <raphael.jakse@univ-grenoble-alpes.fr>
Sun, 5 Apr 2020 12:59:28 +0000 (14:59 +0200)
trivabble.js

index 860b64c44aa74396535a6cc49131702bcffe17a6..1cfd57c805c29180a37e42c66b06d95bc8ab98c3 100644 (file)
             }
 
             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();
         xhr.send(JSON.stringify(data));
 
         xhr.onreadystatechange = function () {
-            // TODO error handling
-
             if (xhr.readyState === 2 && thisRequestIsPolling) {
                 connectionReady();
             } else if (xhr.readyState === 3 && thisRequestIsPolling) {