Skip to content

Commit 71dd2c1

Browse files
committed
Utility and WASM updates
1 parent ccbf679 commit 71dd2c1

File tree

18 files changed

+135
-114
lines changed

18 files changed

+135
-114
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ templates:
1414

1515
.PHONY: build
1616
build: templates ## Build all binaries
17-
@GOOS=js GOARCH=wasm go build -o ./assets/wasm/solitaire.wasm ./app/wasm/...
17+
@GOOS=js GOARCH=wasm go build -o ./assets/wasm/solitaire.wasm ./app/wasm/wasm_cmd/...
1818
@go build -gcflags "all=-N -l" -o build/debug/solitaire .
1919

2020
.PHONY: build-release
2121
build-release: templates ## Build all binaries without debug information, clean up after
22-
@GOOS=js GOARCH=wasm go build -o ./assets/wasm/solitaire.wasm ./app/wasm/...
22+
@GOOS=js GOARCH=wasm go build -o ./assets/wasm/solitaire.wasm ./app/wasm/wasm_cmd/...
2323
@go build -ldflags '-s -w' -trimpath -o build/release/solitaire .
2424

2525
.PHONY: lint

app/wasm/funcs.go

+19-44
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,34 @@
11
//go:build js
2+
// +build js
23

3-
package main
4+
package wasm
45

5-
import (
6-
"fmt"
7-
"syscall/js"
8-
)
6+
import "syscall/js"
97

10-
func wireFunctions() {
11-
js.Global().Set("increment", js.FuncOf(increment))
12-
js.Global().Set("log", js.FuncOf(logInfo))
13-
js.Global().Set("send", js.FuncOf(send))
8+
func (w *WASM) wireFunctions() {
9+
js.Global().Set("log", js.FuncOf(w.logInfo))
10+
js.Global().Set("sendMessage", js.FuncOf(w.sendMessage))
1411
}
1512

16-
func increment(this js.Value, args []js.Value) any {
17-
if len(args) == 0 {
18-
return "exactly one argument is required"
19-
}
20-
arg := args[0].Int()
21-
return arg + 1
22-
}
23-
24-
func send(this js.Value, args []js.Value) any {
25-
if len(args) == 0 {
26-
return "at least one argument is required"
27-
}
28-
arg := args[0].String()
29-
return arg
30-
}
31-
32-
func logInfo(this js.Value, args []js.Value) any {
33-
if _rootLogger == nil {
13+
func (w *WASM) logInfo(this js.Value, args []js.Value) any {
14+
if w.logger == nil {
3415
println("ERROR: no logger available")
3516
return nil
3617
}
3718
if len(args) == 0 {
38-
_rootLogger.Warnf("at least one argument is required")
19+
w.logger.Warnf("at least one argument is required")
3920
return nil
4021
}
4122
msg := args[0].String()
42-
cleanArgs := make([]any, 0, len(args)-1)
43-
for _, x := range args[1:] {
44-
switch x.Type() {
45-
case js.TypeString:
46-
cleanArgs = append(cleanArgs, x.String())
47-
case js.TypeBoolean:
48-
cleanArgs = append(cleanArgs, x.Bool())
49-
case js.TypeNumber:
50-
cleanArgs = append(cleanArgs, x.Float())
51-
case js.TypeNull:
52-
cleanArgs = append(cleanArgs, "<null>")
53-
default:
54-
cleanArgs = append(cleanArgs, fmt.Sprintf("unhandled type [%T]", x))
55-
}
56-
}
57-
_rootLogger.Infof(msg, cleanArgs...)
23+
w.logger.Infof(msg, convertArgs(args[1:])...)
5824
return nil
5925
}
26+
27+
func (w *WASM) sendMessage(this js.Value, args []js.Value) any {
28+
if len(args) != 1 {
29+
return "exactly one argument is required"
30+
}
31+
arg := args[0].Get("t").String()
32+
w.logger.Infof("got message of type [%s]", arg)
33+
return arg
34+
}

app/wasm/init.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//go:build js
2+
// +build js
23

3-
package main
4+
package wasm
45

56
import (
67
"github.com/kyleu/solitaire/app/util"
78
)
89

910
func initWASM(l util.Logger) {
10-
//randoms := pile.Pile{ID: "test", Options: &pile.Options{}, Cards: card.RandomCards(10, suit.SuitsCommon, 1)}
11-
//Audit("Hand", util.ToJSON(randoms))
12-
//outcome := poker.Check(randoms.Cards)
13-
//Audit("Outcome", util.ToJSON(outcome))
1411
}

app/wasm/js.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build js
22
// +build js
33

4-
package main
4+
package wasm
55

66
import (
77
"syscall/js"
@@ -10,28 +10,28 @@ import (
1010
"github.com/kyleu/solitaire/app/util"
1111
)
1212

13-
func Testbed(args ...any) js.Value {
14-
return call("testbed", args...)
13+
func (w *WASM) Testbed(args ...any) js.Value {
14+
return w.call("testbed", args...)
1515
}
1616

17-
func OnMessage(typ string, msg any) js.Value {
18-
return call("onMessage", typ, msg)
17+
func (w *WASM) OnMessage(typ string, msg any) js.Value {
18+
return w.call("onMessage", typ, msg)
1919
}
2020

21-
func Log(level string, occurred time.Time, loggerName string, message string, caller util.ValueMap, stack string, fields util.ValueMap) js.Value {
21+
func (w *WASM) Log(level string, occurred time.Time, loggerName string, message string, caller util.ValueMap, stack string, fields util.ValueMap) js.Value {
2222
m := util.ValueMap{"level": level, "message": message, "caller": caller.AsMap(), "occurred": util.TimeToJS(&occurred)}
2323
if stack != "" {
2424
m["stack"] = stack
2525
}
2626
if len(fields) > 0 {
2727
m["fields"] = fields.AsMap()
2828
}
29-
return OnMessage("log", m.AsMap())
29+
return w.OnMessage("log", m.AsMap())
3030
}
3131

32-
func call(fn string, args ...any) js.Value {
32+
func (w *WASM) call(fn string, args ...any) js.Value {
3333
if x := js.Global().Get(fn); x.IsUndefined() {
34-
_rootLogger.Warnf("function [%s] is not defined", fn)
34+
w.logger.Warnf("function [%s] is not defined", fn)
3535
return js.Undefined()
3636
} else {
3737
return js.Global().Call(fn, args...)

app/wasm/util.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build js
2+
// +build js
3+
4+
package wasm
5+
6+
import (
7+
"fmt"
8+
"syscall/js"
9+
)
10+
11+
func convertArgs(args []js.Value) []any {
12+
cleanArgs := make([]any, 0, len(args)-1)
13+
for _, x := range args[1:] {
14+
switch x.Type() {
15+
case js.TypeString:
16+
cleanArgs = append(cleanArgs, x.String())
17+
case js.TypeBoolean:
18+
cleanArgs = append(cleanArgs, x.Bool())
19+
case js.TypeNumber:
20+
cleanArgs = append(cleanArgs, x.Float())
21+
case js.TypeNull:
22+
cleanArgs = append(cleanArgs, "<null>")
23+
default:
24+
cleanArgs = append(cleanArgs, fmt.Sprintf("unhandled type [%T]", x))
25+
}
26+
}
27+
return cleanArgs
28+
}

app/wasm/main.go app/wasm/wasm.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build js
22
// +build js
33

4-
package main
4+
package wasm
55

66
import (
77
"time"
@@ -10,27 +10,26 @@ import (
1010
"github.com/kyleu/solitaire/app/util"
1111
)
1212

13-
var (
14-
_rootLogger util.Logger
15-
_close chan struct{}
16-
)
13+
type WASM struct {
14+
logger util.Logger
15+
CloseCh chan struct{}
16+
}
17+
18+
func NewWASM() *WASM {
19+
ret := &WASM{CloseCh: make(chan struct{})}
1720

18-
func main() {
1921
logFn := func(level string, occurred time.Time, loggerName string, message string, caller util.ValueMap, stack string, fields util.ValueMap) {
20-
Log(level, occurred, loggerName, message, caller, stack, fields)
22+
ret.Log(level, occurred, loggerName, message, caller, stack, fields)
2123
}
2224
l, err := log.InitLogging(true, logFn)
2325
if err != nil {
2426
println(err)
2527
}
26-
_rootLogger = l
28+
ret.logger = l
2729

2830
t := util.TimerStart()
29-
wireFunctions()
30-
31-
initWASM(l)
32-
31+
ret.wireFunctions()
3332
l.Infof("[%s] started in [%s]", util.AppName, t.EndString())
34-
_close = make(chan struct{})
35-
<-_close
33+
34+
return ret
3635
}

app/wasm/wasm_cmd/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build js
2+
// +build js
3+
4+
package main
5+
6+
import "github.com/kyleu/solitaire/app/wasm"
7+
8+
func main() {
9+
w := wasm.NewWASM()
10+
<-w.CloseCh
11+
}

assets/client.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/client.css.map

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/build/wasmclient.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ cd "$dir/../.."
88

99
echo "building Solitaire WASM client library..."
1010
mkdir -p build/wasm
11-
GOOS=js GOARCH=wasm go build -o ./assets/wasm/solitaire.wasm ./app/wasm/main.go
11+
GOOS=js GOARCH=wasm go build -o ./assets/wasm/solitaire.wasm ./app/wasm/wasm_cmd/main.go

client/src/style/core.css

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ body {
1111
box-sizing: border-box;
1212
background-color: var(--color-background);
1313
color: var(--color-foreground);
14+
height: 100vh;
1415
}
1516

1617
a {

doc/module/websocket.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function send(cmd, param) {
7272
}
7373

7474
document.addEventListener("DOMContentLoaded", function() {
75-
sock = new lifelog.Socket(true, open, recv, err, "/example/connect");
75+
sock = new solitaire.Socket(true, open, recv, err, "/example/connect");
7676
console.log("loaded socket connection");
7777
});
7878

views/components/WASM.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
{% import "github.com/kyleu/solitaire/assets" %}
1+
{% import (
2+
"github.com/kyleu/solitaire/app/util"
3+
"github.com/kyleu/solitaire/assets"
4+
) %}
25

3-
{% func WASMScript() %}
6+
{% func WASMScript(cfg any) %}
47
{%s= assets.ScriptElement(`wasm/wasm_exec.js`, true) %}
58
<script>
69
document.addEventListener("DOMContentLoaded", function() {
@@ -15,7 +18,7 @@
1518
const go = new Go();
1619
WebAssembly.instantiateStreaming(fetch("{%s assets.URL(`wasm/solitaire.wasm`) %}"), go.importObject).then((result) => {
1720
go.run(result.instance);
18-
wasmInit(new Date().getTime() - start);
21+
wasmInit(new Date().getTime() - start, {%s= util.ToJSONCompact(cfg) %});
1922
});
2023
});
2124
</script>

views/components/WASM.html.go

+35-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)