Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ws: undefined is not an object (evaluating 'api.getInternalData(elt).webSocket.close') #109

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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