🎉 Commit of go-promises

This commit is contained in:
Dan Jones 2024-12-27 15:19:49 -06:00
commit 5d84d26c3a
Signed by: dan
GPG key ID: 5B3B0F7217473A29
13 changed files with 238 additions and 0 deletions

5
internal/add.go Normal file
View file

@ -0,0 +1,5 @@
package internal
func Add(a, b int) int {
return a + b
}

2
internal/assets/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
app.wasm
wasm_exec.js

View file

@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("app.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
</script>
</head>
<body></body>
</html>

View file

@ -0,0 +1,14 @@
package main
import (
"fmt"
"net/http"
)
func main() {
err := http.ListenAndServe(":9090", http.FileServer(http.Dir("./internal/assets")))
if err != nil {
fmt.Println("Failed to start server", err)
return
}
}

50
internal/cmd/wasm/main.go Normal file
View file

@ -0,0 +1,50 @@
package main
import (
"errors"
"fmt"
"syscall/js"
promises "codeberg.org/danjones000/go-promises"
"codeberg.org/danjones000/go-promises/internal"
)
func main() {
fmt.Println("Some go")
js.Global().Set("add", promises.PromisifyGoFunc(func(_ js.Value, args []js.Value) (any, error) {
if len(args) < 2 {
return nil, errors.New("add called with too few arguments")
}
a := args[0]
b := args[1]
if a.Type() != js.TypeNumber {
return nil, fmt.Errorf("First argument must be a number. %s given", a.Type())
}
if b.Type() != js.TypeNumber {
return nil, fmt.Errorf("secong argument must be a number. %s given", b.Type())
}
return internal.Add(a.Int(), b.Int()), nil
}))
js.Global().Set("getUser", promises.PromisifyGoFunc(func(this js.Value, args []js.Value) (any, error) {
if len(args) < 2 {
return nil, errors.New("getUser called with too few arguments")
}
name := args[0]
age := args[1]
if name.Type() != js.TypeString {
return nil, fmt.Errorf("First argument must be a string. %s given", name.Type())
}
if age.Type() != js.TypeNumber {
return nil, fmt.Errorf("Second argument must be a string. %s given", age.Type())
}
return internal.GetUser(name.String(), age.Int()), nil
}))
<-make(chan struct{})
}

10
internal/struct.go Normal file
View file

@ -0,0 +1,10 @@
package internal
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func GetUser(name string, age int) User {
return User{name, age}
}