fix: shakespere example not compiling; remove sleep statements from patchChan loop
This commit is contained in:
@@ -107,3 +107,72 @@ func main() {
|
|||||||
|
|
||||||
v.Start()
|
v.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type H = h.H
|
||||||
|
|
||||||
|
func valueToString(v any) string {
|
||||||
|
if v == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if b, ok := v.([]byte); ok {
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
return fmt.Sprint(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderTable takes sql.Rows and an array of CSS class names for each column.
|
||||||
|
// Returns a complete HTML table as a gomponent.
|
||||||
|
func RenderTable(rows *sql.Rows, columnClasses []string) (H, error) {
|
||||||
|
cols, err := rows.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
headerCells := make([]h.H, len(cols))
|
||||||
|
for i, col := range cols {
|
||||||
|
headerCells[i] = h.Th(h.Attr("scope", "col"), h.Text(col))
|
||||||
|
}
|
||||||
|
thead := h.THead(h.Tr(headerCells...))
|
||||||
|
|
||||||
|
var bodyRows []h.H
|
||||||
|
for rows.Next() {
|
||||||
|
values := make([]any, len(cols))
|
||||||
|
scanArgs := make([]any, len(cols))
|
||||||
|
for i := range values {
|
||||||
|
scanArgs[i] = &values[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Scan(scanArgs...); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cells := make([]h.H, len(values))
|
||||||
|
if len(values) > 0 {
|
||||||
|
var thAttrs []h.H
|
||||||
|
thAttrs = append(thAttrs, h.Attr("scope", "row"))
|
||||||
|
if len(columnClasses) > 0 && columnClasses[0] != "" {
|
||||||
|
thAttrs = append(thAttrs, h.Class(columnClasses[0]))
|
||||||
|
}
|
||||||
|
thAttrs = append(thAttrs, h.Text(valueToString(values[0])))
|
||||||
|
cells[0] = h.Th(thAttrs...)
|
||||||
|
|
||||||
|
for i := 1; i < len(values); i++ {
|
||||||
|
var tdAttrs []h.H
|
||||||
|
if i < len(columnClasses) && columnClasses[i] != "" {
|
||||||
|
tdAttrs = append(tdAttrs, h.Class(columnClasses[i]))
|
||||||
|
}
|
||||||
|
tdAttrs = append(tdAttrs, h.Text(valueToString(values[i])))
|
||||||
|
cells[i] = h.Td(tdAttrs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyRows = append(bodyRows, h.Tr(cells...))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tbody := h.TBody(bodyRows...)
|
||||||
|
return h.Table(thead, tbody), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/go-via/via/h"
|
|
||||||
)
|
|
||||||
|
|
||||||
type H = h.H
|
|
||||||
|
|
||||||
func valueToString(v any) string {
|
|
||||||
if v == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if b, ok := v.([]byte); ok {
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
return fmt.Sprint(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RenderTable takes sql.Rows and an array of CSS class names for each column.
|
|
||||||
// Returns a complete HTML table as a gomponent.
|
|
||||||
func RenderTable(rows *sql.Rows, columnClasses []string) (H, error) {
|
|
||||||
cols, err := rows.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
headerCells := make([]h.H, len(cols))
|
|
||||||
for i, col := range cols {
|
|
||||||
headerCells[i] = h.Th(h.Attr("scope", "col"), h.Text(col))
|
|
||||||
}
|
|
||||||
thead := h.THead(h.Tr(headerCells...))
|
|
||||||
|
|
||||||
var bodyRows []h.H
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]any, len(cols))
|
|
||||||
scanArgs := make([]any, len(cols))
|
|
||||||
for i := range values {
|
|
||||||
scanArgs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(scanArgs...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cells := make([]h.H, len(values))
|
|
||||||
if len(values) > 0 {
|
|
||||||
var thAttrs []h.H
|
|
||||||
thAttrs = append(thAttrs, h.Attr("scope", "row"))
|
|
||||||
if len(columnClasses) > 0 && columnClasses[0] != "" {
|
|
||||||
thAttrs = append(thAttrs, h.Class(columnClasses[0]))
|
|
||||||
}
|
|
||||||
thAttrs = append(thAttrs, h.Text(valueToString(values[0])))
|
|
||||||
cells[0] = h.Th(thAttrs...)
|
|
||||||
|
|
||||||
for i := 1; i < len(values); i++ {
|
|
||||||
var tdAttrs []h.H
|
|
||||||
if i < len(columnClasses) && columnClasses[i] != "" {
|
|
||||||
tdAttrs = append(tdAttrs, h.Class(columnClasses[i]))
|
|
||||||
}
|
|
||||||
tdAttrs = append(tdAttrs, h.Text(valueToString(values[i])))
|
|
||||||
cells[i] = h.Td(tdAttrs...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyRows = append(bodyRows, h.Tr(cells...))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tbody := h.TBody(bodyRows...)
|
|
||||||
return h.Table(thead, tbody), nil
|
|
||||||
}
|
|
||||||
11
via.go
11
via.go
@@ -18,7 +18,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-via/via/h"
|
"github.com/go-via/via/h"
|
||||||
"github.com/starfederation/datastar-go/datastar"
|
"github.com/starfederation/datastar-go/datastar"
|
||||||
@@ -213,6 +212,8 @@ func (v *V) unregisterCtx(id string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
v.logDebug(nil, "ctx '%s' removed from registry", id)
|
v.logDebug(nil, "ctx '%s' removed from registry", id)
|
||||||
|
v.logDebug(nil, "number of sessions in registry: %d", v.currSessionNum())
|
||||||
|
|
||||||
delete(v.contextRegistry, id)
|
delete(v.contextRegistry, id)
|
||||||
v.currSessionNum()
|
v.currSessionNum()
|
||||||
}
|
}
|
||||||
@@ -334,7 +335,6 @@ func (v *V) devModeRestore() {
|
|||||||
v.registerCtx(c)
|
v.registerCtx(c)
|
||||||
}
|
}
|
||||||
v.logDebug(nil, "devmode restored ctx registry")
|
v.logDebug(nil, "devmode restored ctx registry")
|
||||||
os.Remove(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type patchType int
|
type patchType int
|
||||||
@@ -398,7 +398,6 @@ func New() *V {
|
|||||||
return
|
return
|
||||||
case patch, ok := <-c.patchChan:
|
case patch, ok := <-c.patchChan:
|
||||||
if !ok {
|
if !ok {
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch patch.typ {
|
switch patch.typ {
|
||||||
@@ -418,8 +417,6 @@ func New() *V {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -465,7 +462,9 @@ func New() *V {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
v.logDebug(c, "session close event triggered")
|
v.logDebug(c, "session close event triggered")
|
||||||
v.devModeRemovePersisted(c)
|
if v.cfg.DevMode {
|
||||||
|
v.devModeRemovePersisted(c)
|
||||||
|
}
|
||||||
v.unregisterCtx(c.id)
|
v.unregisterCtx(c.id)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user