fix sse-swap removal handling in ext/sse.js

This commit is contained in:
Denis Palashevskii
2024-01-26 20:02:51 +04:00
parent 5b4fb817d6
commit a895597a53
2 changed files with 12 additions and 0 deletions

View File

@@ -145,6 +145,7 @@ This extension adds support for Server Sent Events to htmx. See /www/extensions
// If the body no longer contains the element, remove the listener
if (!api.bodyContains(child)) {
source.removeEventListener(sseEventName, listener);
return;
}
// swap the response into the DOM and trigger a notification

View File

@@ -5,6 +5,7 @@ describe("sse extension", function() {
var wasClosed = false;
var url;
var mockEventSource = {
_listeners: listeners,
removeEventListener: function(name, l) {
listeners[name] = listeners[name].filter(function(elt, idx, arr) {
if (arr[idx] === l) {
@@ -149,6 +150,16 @@ describe("sse extension", function() {
this.eventSource.wasClosed().should.equal(true)
})
it('is not listening for events after hx-swap element removed', function() {
var div = make('<div hx-ext="sse" sse-connect="/foo">' +
'<div id="d1" hx-swap="outerHTML" sse-swap="e1">div1</div>' +
'</div>');
this.eventSource._listeners["e1"].should.be.lengthOf(1)
div.removeChild(byId("d1"));
this.eventSource.sendEvent("e1", "Test")
this.eventSource._listeners["e1"].should.be.empty
})
// sse and hx-trigger handlers are distinct
it('is closed after removal with no close and activity, sse-swap', function() {
var div = make('<div hx-get="/test" hx-swap="outerHTML" hx-ext="sse" sse-connect="/foo">' +