mirror of
https://github.com/bigskysoftware/htmx.git
synced 2026-01-24 20:56:15 +00:00
fix sse-swap removal handling in ext/sse.js
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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">' +
|
||||
|
||||
Reference in New Issue
Block a user