Skip to content

Commit

Permalink
Add reconnect() to socketWrapper public interface (#130)
Browse files Browse the repository at this point in the history
* Add `reconnect()` to socketWrapper public interface

Implements functionality suggested in #129

* Add test for reconnect()

* QA: More precise test description
  • Loading branch information
thekid authored Jan 9, 2025
1 parent b0f44b2 commit 09f61d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/ws/test/ext/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,32 @@ describe('web-sockets extension', function() {
this.tickMock()
})

it('re-establishes closed connections using reconnect()', function() {
var handledEventTypes = []
var handler = function(evt) { handledEventTypes.push(evt.detail.event.type) }
var reconnect = function(evt) { setTimeout(() => evt.detail.socketWrapper.reconnect()) }

htmx.on('htmx:wsConnecting', handler)

var div = make('<div hx-get="/test" hx-swap="outerHTML" hx-ext="ws" ws-connect="ws://localhost:8080">')

htmx.on(div, 'htmx:wsClose', handler)
htmx.on(div, 'htmx:wsClose', reconnect)

this.tickMock()
this.socketServer.close()

this.tickMock()
handledEventTypes.should.eql(['connecting', 'close', 'connecting'])

this.tickMock()
this.socketServer.close()

htmx.off('htmx:wsConnecting', handler)
htmx.off(div, 'htmx:wsClose', handler)
htmx.off(div, 'htmx:wsClose', reconnect)
})

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
1 change: 1 addition & 0 deletions src/ws/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ This extension adds support for WebSockets to htmx. See /www/extensions/ws.md f
wrapper.publicInterface = {
send: wrapper.send.bind(wrapper),
sendImmediately: wrapper.sendImmediately.bind(wrapper),
reconnect: wrapper.init.bind(wrapper),
queue: wrapper.messageQueue
}

Expand Down

0 comments on commit 09f61d9

Please sign in to comment.