some cleanup
This commit is contained in:
parent
c7f4781137
commit
8cdc625133
5 changed files with 15 additions and 48 deletions
|
|
@ -10,8 +10,7 @@ func IndexPage(ctx *h.RequestContext) *h.Page {
|
|||
return h.NewPage(
|
||||
RootPage(
|
||||
h.Div(
|
||||
h.JoinAttributes(
|
||||
", ",
|
||||
h.JoinExtensions(
|
||||
h.TriggerChildren(),
|
||||
h.HxExtension("ws"),
|
||||
),
|
||||
|
|
@ -36,7 +35,6 @@ func MessageInput() *h.Element {
|
|||
h.Class("p-4 rounded-md border border-slate-200"),
|
||||
h.Name("message"),
|
||||
h.Placeholder("Message"),
|
||||
h.OnEvent("htmx:wsBeforeMessage", js.EvalJs("console.log('got message input')")),
|
||||
h.HxBeforeWsSend(
|
||||
js.SetValue(""),
|
||||
),
|
||||
|
|
|
|||
4
framework/assets/dist/htmgo.js
vendored
4
framework/assets/dist/htmgo.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -3,9 +3,6 @@ import htmx from "htmx.org";
|
|||
htmx.defineExtension("debug", {
|
||||
// @ts-ignore
|
||||
onEvent: function (name, evt) {
|
||||
if(name != 'htmx:wsBeforeMessage') {
|
||||
return
|
||||
}
|
||||
if (console.debug) {
|
||||
console.debug(name, evt);
|
||||
} else if (console) {
|
||||
|
|
|
|||
|
|
@ -2,24 +2,6 @@ import htmx from 'htmx.org'
|
|||
import {removeAssociatedScripts} from "./htmgo";
|
||||
|
||||
|
||||
declare module 'htmx.org' {
|
||||
interface Htmx {
|
||||
defineExtension(name: string, extension: HtmxExtension): void;
|
||||
createWebSocket?: (url: string) => WebSocket;
|
||||
config: {
|
||||
wsReconnectDelay?: 'full-jitter' | ((retryCount: number) => number);
|
||||
wsBinaryType?: string;
|
||||
[key: string]: any
|
||||
};
|
||||
[key: string]: any;
|
||||
}
|
||||
}
|
||||
|
||||
interface HtmxExtension {
|
||||
init: (apiRef: HtmxInternalApi) => void;
|
||||
onEvent: (name: string, evt: Event) => void;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface HtmxInternalApi {
|
||||
getInternalData(elt: Element): any;
|
||||
|
|
@ -113,7 +95,7 @@ function ensureWebSocket(socketElt: HTMLElement): void {
|
|||
}
|
||||
}
|
||||
|
||||
const socketWrapper = createWebsocketWrapper(socketElt, () => htmx.createWebSocket!(wssSource));
|
||||
const socketWrapper = createWebsocketWrapper(socketElt, () => createWebSocket(wssSource));
|
||||
|
||||
socketWrapper.addEventListener('message', (event) => {
|
||||
if (maybeCloseWebSocketSource(socketElt)) {
|
||||
|
|
@ -288,7 +270,9 @@ function ensureWebSocketSend(elt: HTMLElement): void {
|
|||
return;
|
||||
}
|
||||
|
||||
const webSocketParent = api.getClosestMatch(elt, hasWebSocket);
|
||||
const webSocketParent = api.getClosestMatch(elt, (node) => {
|
||||
return hasWebSocket(node as HTMLElement);
|
||||
});
|
||||
if (webSocketParent) {
|
||||
processWebSocketSend(webSocketParent as HTMLElement, elt);
|
||||
}
|
||||
|
|
@ -355,17 +339,10 @@ function processWebSocketSend(socketElt: HTMLElement, sendElt: HTMLElement): voi
|
|||
}
|
||||
|
||||
function getWebSocketReconnectDelay(retryCount: number): number {
|
||||
const delay = htmx.config.wsReconnectDelay;
|
||||
if (typeof delay === 'function') {
|
||||
return delay(retryCount);
|
||||
}
|
||||
if (delay === 'full-jitter') {
|
||||
const exp = Math.min(retryCount, 6);
|
||||
const maxDelay = 1000 * Math.pow(2, exp);
|
||||
return maxDelay * Math.random();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function maybeCloseWebSocketSource(elt: HTMLElement): boolean {
|
||||
if (!api.bodyContains(elt)) {
|
||||
|
|
@ -407,18 +384,9 @@ htmx.defineExtension('ws', {
|
|||
init: (apiRef: HtmxInternalApi) => {
|
||||
// Store reference to internal API
|
||||
api = apiRef;
|
||||
|
||||
// Default function for creating new WebSocket objects
|
||||
if (!htmx.createWebSocket) {
|
||||
htmx.createWebSocket = createWebSocket;
|
||||
}
|
||||
|
||||
// Default setting for reconnect delay
|
||||
if (!htmx.config.wsReconnectDelay) {
|
||||
htmx.config.wsReconnectDelay = 'full-jitter';
|
||||
}
|
||||
},
|
||||
|
||||
// @ts-ignore
|
||||
onEvent: (name: string, evt: Event) => {
|
||||
const parent: Element = evt.target as Element || (evt as CustomEvent).detail.elt;
|
||||
|
||||
|
|
|
|||
|
|
@ -141,6 +141,10 @@ func HxExtensions(value ...string) Ren {
|
|||
return Attribute(hx.ExtAttr, strings.Join(value, ","))
|
||||
}
|
||||
|
||||
func JoinExtensions(attrs ...*AttributeR) Ren {
|
||||
return JoinAttributes(", ", attrs...)
|
||||
}
|
||||
|
||||
func JoinAttributes(sep string, attrs ...*AttributeR) *AttributeR {
|
||||
values := make([]string, 0, len(attrs))
|
||||
for _, a := range attrs {
|
||||
|
|
|
|||
Loading…
Reference in a new issue