From 32a7393c65a1c3338f3b420022e3f4ec43a38355 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 21 Jan 2024 21:35:50 +0100 Subject: [PATCH] [BasicUI] Close EventSource when leaving (#2282) Signed-off-by: Laurent Garnier --- .../org.openhab.ui.basic/web-src/smarthome.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.ui.basic/web-src/smarthome.js b/bundles/org.openhab.ui.basic/web-src/smarthome.js index 0ca0312006..ad4bb8e5dc 100644 --- a/bundles/org.openhab.ui.basic/web-src/smarthome.js +++ b/bundles/org.openhab.ui.basic/web-src/smarthome.js @@ -2677,7 +2677,7 @@ _t.navigate = function(){}; _t.source = new EventSource(subscribeLocation); - _t.source.addEventListener("event", function(payload) { + function handleEventSourceEvent(payload) { if (_t.paused) { return; } @@ -2749,10 +2749,17 @@ }; _t.updateWidget(smarthome.dataModel[data.widgetId], update); } - }); + } - _t.source.onerror = function() { + _t.source.addEventListener("event", handleEventSourceEvent); + + _t.closeConnection = function() { + _t.source.removeEventListener("event", handleEventSourceEvent); _t.source.close(); + }; + + _t.source.onerror = function() { + _t.closeConnection(); _t.connectionError(); }; } @@ -2994,6 +3001,8 @@ initSubscription(null); }; + _t.closeConnection = function(){}; + ajax({ url: _t.subscribeRequestURL, type: "POST", @@ -3056,6 +3065,7 @@ window.addEventListener("beforeunload", function() { smarthome.changeListener.suppressErrors(); + smarthome.changeListener.closeConnection(); }); }); })({