fix(runtime): solve chan blocks; other small improvements

This commit is contained in:
Joao Goncalves
2025-11-25 23:19:50 -01:00
parent a71d6f0960
commit 36c0fb9050
2 changed files with 10 additions and 12 deletions

View File

@@ -184,7 +184,7 @@ func (c *Context) Signal(v any) *signal {
func (c *Context) injectSignals(sigs map[string]any) { func (c *Context) injectSignals(sigs map[string]any) {
if sigs == nil { if sigs == nil {
c.app.logErr(c, "signal injection failed: nil signals in ctx") c.app.logErr(c, "signal injection failed: nil signals")
return return
} }
@@ -320,7 +320,9 @@ func (c *Context) ExecScript(s string) {
func (c *Context) stopAllRoutines() { func (c *Context) stopAllRoutines() {
select { select {
case c.ctxDisposedChan <- struct{}{}: case c.ctxDisposedChan <- struct{}{}:
c.app.logDebug(c, "stopped all routines")
default: default:
c.app.logDebug(c, "did not stop all routines")
} }
} }
@@ -337,6 +339,6 @@ func newContext(id string, route string, v *V) *Context {
componentRegistry: make(map[string]*Context), componentRegistry: make(map[string]*Context),
actionRegistry: make(map[string]func()), actionRegistry: make(map[string]func()),
signals: new(sync.Map), signals: new(sync.Map),
ctxDisposedChan: make(chan struct{}), ctxDisposedChan: make(chan struct{}, 1),
} }
} }

14
via.go
View File

@@ -153,7 +153,7 @@ func (v *V) Page(route string, initContextFn func(c *Context)) {
v.devModePageInitFnMap[route] = initContextFn v.devModePageInitFnMap[route] = initContextFn
} }
v.mux.HandleFunc("GET "+route, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v.mux.HandleFunc("GET "+route, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v.logDebug(nil, "GET %s", route) v.logDebug(nil, "GET %s", r.URL.String())
if strings.Contains(r.URL.Path, "favicon") { if strings.Contains(r.URL.Path, "favicon") {
return return
} }
@@ -389,11 +389,7 @@ func New() *V {
defer close(c.patchChan) defer close(c.patchChan)
go func() { go func() {
if v.cfg.DevMode {
c.Sync() c.Sync()
} else {
c.SyncSignals()
}
}() }()
for { for {
@@ -403,23 +399,23 @@ func New() *V {
return return
case patch, ok := <-c.patchChan: case patch, ok := <-c.patchChan:
if !ok { if !ok {
return continue
} }
switch patch.typ { switch patch.typ {
case patchTypeElements: case patchTypeElements:
if err := sse.PatchElements(patch.content); err != nil { if err := sse.PatchElements(patch.content); err != nil {
v.logErr(c, "PatchElements failed: %v", err) v.logErr(c, "PatchElements failed: %v", err)
return continue
} }
case patchTypeSignals: case patchTypeSignals:
if err := sse.PatchSignals([]byte(patch.content)); err != nil { if err := sse.PatchSignals([]byte(patch.content)); err != nil {
v.logErr(c, "PatchSignals failed: %v", err) v.logErr(c, "PatchSignals failed: %v", err)
return continue
} }
case patchTypeScript: case patchTypeScript:
if err := sse.ExecuteScript(patch.content, datastar.WithExecuteScriptAutoRemove(true)); err != nil { if err := sse.ExecuteScript(patch.content, datastar.WithExecuteScriptAutoRemove(true)); err != nil {
v.logErr(c, "ExecuteScript failed: %v", err) v.logErr(c, "ExecuteScript failed: %v", err)
return continue
} }
} }
} }