Skip to content

Commit

Permalink
fix ws: `undefined is not an object (evaluating 'api.getInternalData(…
Browse files Browse the repository at this point in the history
…elt).webSocket.close')` (#109)

* websocket presence check

* with tests
  • Loading branch information
enigma authored Nov 23, 2024
1 parent 457da4b commit e87e1c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/ws/test/ext/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ describe('web-sockets extension', function() {
this.messages[1].should.contains('"action":"B"')
})

it('maybeClose does not raise when there is no socket', function() {
var div = make('<div hx-ext="ws" ws-connect="ws://localhost:8080"><div id="d1">div1</div></div>')
this.tickMock()
var internalData = div['htmx-internal-data']
should.exist(internalData.webSocket)
delete internalData.webSocket
should.not.exist(internalData.webSocket)
div.parentNode.removeChild(div)
this.tickMock()
})

describe('Send immediately', function() {
function checkCallForWsBeforeSend(spy, wrapper, message, target) {
// Utility function to always check the same for htmx:wsBeforeSend caught by a spy
Expand Down
8 changes: 6 additions & 2 deletions src/ws/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,12 @@ This extension adds support for WebSockets to htmx. See /www/extensions/ws.md f
*/
function maybeCloseWebSocketSource(elt) {
if (!api.bodyContains(elt)) {
api.getInternalData(elt).webSocket.close()
return true
var internalData = api.getInternalData(elt)
if (internalData.webSocket) {
internalData.webSocket.close()
return true
}
return false
}
return false
}
Expand Down

0 comments on commit e87e1c3

Please sign in to comment.