working again
This commit is contained in:
parent
a23e1fd78f
commit
ef54e274f5
9 changed files with 44 additions and 13 deletions
|
|
@ -5,9 +5,12 @@ import (
|
|||
"github.com/maddalax/htmgo/framework/h"
|
||||
"github.com/maddalax/htmgo/framework/service"
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
"runtime"
|
||||
"sse-with-state/internal"
|
||||
"sse-with-state/sse"
|
||||
"sse-with-state/state"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type HandlerData struct {
|
||||
|
|
@ -174,7 +177,30 @@ var Map = xsync.NewMapOf[state.SessionId, *Events]()
|
|||
var socketMessageListener = make(chan sse.SocketEvent, 100)
|
||||
var serverSideMessageListener = make(chan ServerSideEvent, 100)
|
||||
|
||||
func AddServerSideHandler(ctx *h.RequestContext, id string, event string, handler Handler) {
|
||||
func getCallerHash() string {
|
||||
now := time.Now()
|
||||
pc := make([]uintptr, 1000) // Adjust the size if you need a deeper stack trace
|
||||
n := runtime.Callers(2, pc) // Skip 2: runtime.Callers() and printCallers()
|
||||
|
||||
frames := runtime.CallersFrames(pc[:n])
|
||||
calls := make([]string, 0)
|
||||
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
calls = append(calls, fmt.Sprintf("%s:%d", frame.Function, frame.Line))
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
took := time.Since(now)
|
||||
fmt.Printf("took: %dms\n", took.Milliseconds())
|
||||
return strings.Join(calls, ",")
|
||||
}
|
||||
|
||||
func AddServerSideHandler(ctx *h.RequestContext, event string, handler Handler) {
|
||||
hash := getCallerHash()
|
||||
fmt.Printf("adding server side handler %s\n", hash)
|
||||
sessionId := state.GetSessionId(ctx)
|
||||
events, ok := Map.Load(sessionId)
|
||||
|
||||
|
|
@ -183,7 +209,7 @@ func AddServerSideHandler(ctx *h.RequestContext, id string, event string, handle
|
|||
Map.Store(sessionId, events)
|
||||
}
|
||||
|
||||
events.AddServerSideHandler(event, id, handler)
|
||||
events.AddServerSideHandler(event, "test", handler)
|
||||
}
|
||||
|
||||
func AddHandler(ctx *h.RequestContext, event string, handler Handler) *h.AttributeMapOrdered {
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ package pages
|
|||
|
||||
import (
|
||||
"github.com/maddalax/htmgo/framework/h"
|
||||
"starter-template/state"
|
||||
"sse-with-state/state"
|
||||
)
|
||||
|
||||
func RootPage(ctx *h.RequestContext, children ...h.Ren) h.Ren {
|
||||
s := state.NewState(ctx)
|
||||
return h.Html(
|
||||
h.Attribute("data-session-id", s.SessionId),
|
||||
h.Attribute("data-session-id", string(s.SessionId)),
|
||||
h.HxExtension(h.BaseExtensions()),
|
||||
h.Head(
|
||||
h.Link("/public/main.css", "stylesheet"),
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package partials
|
|||
import (
|
||||
"github.com/maddalax/htmgo/framework/h"
|
||||
"sse-with-state/event"
|
||||
"sse-with-state/internal"
|
||||
)
|
||||
|
||||
func OnClick(ctx *h.RequestContext, handler event.Handler) *h.AttributeMapOrdered {
|
||||
|
|
@ -11,9 +10,8 @@ func OnClick(ctx *h.RequestContext, handler event.Handler) *h.AttributeMapOrdere
|
|||
}
|
||||
|
||||
func OnServerSideEvent(ctx *h.RequestContext, eventName string, handler event.Handler) h.Ren {
|
||||
id := internal.RandSeq(30)
|
||||
event.AddServerSideHandler(ctx, id, eventName, handler)
|
||||
return h.Attribute("data-handler-id", id)
|
||||
event.AddServerSideHandler(ctx, eventName, handler)
|
||||
return h.Attribute("data-handler-id", "")
|
||||
}
|
||||
|
||||
func OnMouseOver(ctx *h.RequestContext, handler event.Handler) *h.AttributeMapOrdered {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ type CounterProps struct {
|
|||
|
||||
func CounterForm(ctx *h.RequestContext, props CounterProps) *h.Element {
|
||||
if props.Id == "" {
|
||||
props.Id = h.GenId()
|
||||
props.Id = h.GenId(6)
|
||||
}
|
||||
counter := UseCounter(state.GetSessionId(ctx), props.Id)
|
||||
return h.Div(
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ func repeaterItem(ctx *h.RequestContext, item *h.Element, index int, props *Repe
|
|||
|
||||
func Repeater(ctx *h.RequestContext, props RepeaterProps) *h.Element {
|
||||
if props.Id == "" {
|
||||
props.Id = h.GenId()
|
||||
props.Id = h.GenId(6)
|
||||
}
|
||||
return h.Div(
|
||||
h.Class("flex flex-col gap-2"),
|
||||
|
|
|
|||
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
|
|
@ -7,6 +7,8 @@ import "./htmxextensions/mutation-error";
|
|||
import "./htmxextensions/livereload"
|
||||
import "./htmxextensions/htmgo";
|
||||
import "./htmxextensions/sse"
|
||||
import "./htmxextensions/ws"
|
||||
import "./htmxextensions/ws-event-handler"
|
||||
|
||||
// @ts-ignore
|
||||
window.htmx = htmx;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/maddalax/htmgo/framework/hx"
|
||||
"github.com/maddalax/htmgo/framework/internal/datastructure"
|
||||
"github.com/maddalax/htmgo/framework/internal/util"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -337,3 +338,7 @@ func AriaHidden(value bool) *AttributeR {
|
|||
func TabIndex(value int) *AttributeR {
|
||||
return Attribute("tabindex", fmt.Sprintf("%d", value))
|
||||
}
|
||||
|
||||
func GenId(len int) string {
|
||||
return util.RandSeq(len)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package h
|
|||
import "strings"
|
||||
|
||||
func BaseExtensions() string {
|
||||
extensions := []string{"path-deps", "response-targets", "mutation-error", "htmgo", "sse"}
|
||||
extensions := []string{"path-deps", "response-targets", "mutation-error", "htmgo", "sse", "ws"}
|
||||
if IsDevelopment() {
|
||||
extensions = append(extensions, "livereload")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue