Fixes #1246: ws extension socketWrapper methods are undefined (#1247)

This commit is contained in:
Aral Balkan 2023-02-24 18:03:45 +00:00 committed by GitHub
parent a6701de473
commit f79fec8176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -177,11 +177,6 @@ This extension adds support for WebSockets to htmx. See /www/extensions/ws.md f
*/
function createWebsocketWrapper(socketElt, socketFunc) {
var wrapper = {
publicInterface: {
send: this.send,
sendImmediately: this.sendImmediately,
queue: this.queue
},
socket: null,
messageQueue: [],
retryCount: 0,
@ -296,6 +291,12 @@ This extension adds support for WebSockets to htmx. See /www/extensions/ws.md f
wrapper.init();
wrapper.publicInterface = {
send: wrapper.send.bind(wrapper),
sendImmediately: wrapper.sendImmediately.bind(wrapper),
queue: wrapper.messageQueue
};
return wrapper;
}

View File

@ -115,7 +115,7 @@ describe("web-sockets extension", function () {
htmx.off(div, "htmx:wsClose", handler);
})
it('raises htmx:wsConfig when sending, allows message modification', function () {
it('raises htmx:wsConfigSend when sending, allows message modification', function () {
var myEventCalled = false;
function handle(evt) {
@ -138,6 +138,35 @@ describe("web-sockets extension", function () {
htmx.off("htmx:wsConfigSend", handle)
})
it('passes socketWrapper to htmx:wsConfigSend', function () {
var socketWrapper = null;
function handle(evt) {
evt.preventDefault();
socketWrapper = evt.detail.socketWrapper;
socketWrapper.send(JSON.stringify({foo: 'bar'}), evt.detail.elt)
}
htmx.on("htmx:wsConfigSend", handle)
var div = make('<div hx-ext="ws" ws-connect="ws://localhost:8080"><div ws-send id="d1">div1</div></div>');
this.tickMock();
byId("d1").click();
this.tickMock();
socketWrapper.should.not.be.null;
socketWrapper.send.should.be.a('function');
socketWrapper.sendImmediately.should.be.a('function');
socketWrapper.queue.should.be.an('array');
this.messages.length.should.equal(1);
this.messages[0].should.contains('"foo":"bar"')
htmx.off("htmx:wsConfigSend", handle);
})
it('cancels sending when htmx:wsConfigSend is cancelled', function () {
var myEventCalled = false;