Skip to content

Commit

Permalink
[BasicUI] Close EventSource when leaving (openhab#2282)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored Jan 21, 2024
1 parent 3a31e85 commit 32a7393
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions bundles/org.openhab.ui.basic/web-src/smarthome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
};
}
Expand Down Expand Up @@ -2994,6 +3001,8 @@
initSubscription(null);
};

_t.closeConnection = function(){};

ajax({
url: _t.subscribeRequestURL,
type: "POST",
Expand Down Expand Up @@ -3056,6 +3065,7 @@

window.addEventListener("beforeunload", function() {
smarthome.changeListener.suppressErrors();
smarthome.changeListener.closeConnection();
});
});
})({
Expand Down

0 comments on commit 32a7393

Please sign in to comment.