mess around with support WASM builds

This commit is contained in:
Joel Wetzell
2026-03-30 14:46:35 -05:00
parent 882af2948a
commit 050ada6a70
31 changed files with 487 additions and 6 deletions

5
app/demo/document.go Normal file
View File

@@ -0,0 +1,5 @@
package main
import "syscall/js"
var document = js.Global().Get("document")

74
app/demo/index.html Normal file
View File

@@ -0,0 +1,74 @@
<html>
<head>
<meta charset="utf-8" />
<script src="wasm_exec.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
padding: 0;
}
#editor-container {
flex-grow: 1;
}
#log-container {
background-color: #1e1e1e;
width: 100%;
height: 200px;
overflow-y: auto;
overflow-x: scroll;
box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.5);
}
#logs {
margin: 0;
padding-left: 2px;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 13px;
line-height: 1.4;
color: #a8cc8c;
}
</style>
<script>
const container = document.getElementById("log-container");
function isScrolledToBottom() {
const scrollThreshold = 1;
return (
container.scrollHeight - container.clientHeight <=
container.scrollTop + scrollThreshold
);
}
function scrollToBottomManual() {
const container = document.getElementById("log-container");
// Set scrollTop to the maximum value possible (scrollHeight - clientHeight)
container.scrollTop = container.scrollHeight - container.clientHeight;
}
</script>
</head>
<body>
<div id="editor-container">
<button id="button1">Button 1</button>
<button id="button2">Button 2</button>
<div id="output1"></div>
<div id="output2"></div>
</div>
<div id="log-container">
<pre id="logs"></pre>
</div>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(
fetch("main.wasm"),
go.importObject,
).then((result) => {
go.run(result.instance);
});
</script>
</body>
</html>

42
app/demo/log.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import "syscall/js"
type LogWriter struct {
Element js.Value
Container js.Value
}
func (pw *LogWriter) ScrollToBottom() {
scrollHeight := pw.Container.Get("scrollHeight").Int()
clientHeight := pw.Container.Get("clientHeight").Int()
pw.Container.Set("scrollTop", scrollHeight-clientHeight)
}
func (pw *LogWriter) IsScrolledToBottom() bool {
scrollHeight := pw.Container.Get("scrollHeight").Int()
clientHeight := pw.Container.Get("clientHeight").Int()
scrollTop := pw.Container.Get("scrollTop").Int()
return scrollHeight-clientHeight <= scrollTop+25
}
func (pw *LogWriter) Write(p []byte) (n int, err error) {
if !pw.Element.IsUndefined() {
currentText := pw.Element.Get("textContent").String()
newText := currentText + string(p)
pw.Element.Set("textContent", newText)
if pw.IsScrolledToBottom() {
pw.ScrollToBottom()
}
}
return len(p), nil
}
func NewLogWriter(id string) *LogWriter {
element := document.Call("getElementById", id)
container := element.Get("parentElement")
return &LogWriter{
Element: element,
Container: container,
}
}

121
app/demo/main.go Normal file
View File

@@ -0,0 +1,121 @@
package main
import (
"context"
"fmt"
"log/slog"
"github.com/jwetzell/showbridge-go"
"github.com/jwetzell/showbridge-go/internal/config"
)
func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
slog.SetDefault(slog.New(slog.NewTextHandler(NewLogWriter("logs"), &slog.HandlerOptions{
Level: slog.LevelDebug,
})))
router, moduleConfigErrors, routeConfigErrors := showbridge.NewRouter(config.Config{
Api: config.ApiConfig{
Enabled: false,
Port: 0,
},
Modules: []config.ModuleConfig{
{
Id: "timer",
Type: "time.interval",
Params: map[string]any{
"duration": 1000,
},
},
{
Id: "button1",
Type: "web.onclick",
Params: map[string]any{
"id": "button1",
},
},
{
Id: "button2",
Type: "web.onclick",
Params: map[string]any{
"id": "button2",
},
},
},
Routes: []config.RouteConfig{
{
Input: "timer",
Processors: []config.ProcessorConfig{
{
Type: "debug.log",
},
},
},
{
Input: "button1",
Processors: []config.ProcessorConfig{
{
Type: "string.create",
Params: map[string]any{
"template": "{{.Payload.UnixMilli}}",
},
},
{
Type: "debug.log",
},
{
Type: "web.set",
Params: map[string]any{
"id": "output1",
"property": "innerText",
"value": "Button1 Pressed @ {{.Payload}}",
},
},
},
},
{
Input: "button2",
Processors: []config.ProcessorConfig{
{
Type: "string.create",
Params: map[string]any{
"template": "{{.Payload.UnixMilli}}",
},
},
{
Type: "debug.log",
},
{
Type: "web.set",
Params: map[string]any{
"id": "output2",
"property": "innerText",
"value": "Button2 Pressed @ {{.Payload}}",
},
},
},
},
},
})
if len(moduleConfigErrors) > 0 {
for _, err := range moduleConfigErrors {
println("Module config error:", err.Error)
}
}
if len(routeConfigErrors) > 0 {
for _, err := range routeConfigErrors {
println("Route config error:", err.Error)
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
router.Start(ctx)
fmt.Println("router stopped")
}()
<-ctx.Done()
}