Compare commits
No commits in common. "d4cba7e99d4aaefccc2994da54ab82abc798d374" and "5d84d26c3abbe6a4f0064debd685a1cfef83670a" have entirely different histories.
d4cba7e99d
...
5d84d26c3a
2 changed files with 0 additions and 77 deletions
72
README.md
72
README.md
|
|
@ -1,72 +0,0 @@
|
||||||
# go-promises
|
|
||||||
|
|
||||||
Return promises from Go WASM.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Write some go code, like this.
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"syscall/js"
|
|
||||||
|
|
||||||
promises "codeberg.org/danjones000/go-promises"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Add(a, b int) int {
|
|
||||||
return a + b
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddWrapper() value.JSWrapper {
|
|
||||||
return func(this 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 Add(a.Int(), b.Int()), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
js.Global().Set("add", promises.PromisifyGoFunc(AddWrapper()))
|
|
||||||
<-make(chan struct{})
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Next, compile this code:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
GOOS=js GOARCH=wasm go build -o ./app.wasm ./
|
|
||||||
```
|
|
||||||
|
|
||||||
Copy the `app.wasm` file, as well as the `wasm_exec.js` from your go installation, into wherever you have your web files located.
|
|
||||||
|
|
||||||
```shell
|
|
||||||
cp app.wasm "$(go env GOROOT)"/misc/wasm/wasm_exec.js path/to/web/
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, you'll need some frontend code like this:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const go = new Go();
|
|
||||||
WebAssembly.instantiateStreaming(fetch("app.wasm"), go.importObject).then((result) => {
|
|
||||||
go.run(result.instance);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
Now, the "add" function is available anywhere in your javascript.
|
|
||||||
|
|
||||||
## Roadmap
|
|
||||||
|
|
||||||
- [ ] Argument validation, so you can write less code in your wrapper function.
|
|
||||||
- [ ] An easy method for attaching functions to other objects, so they don't have to be in the global scope
|
|
||||||
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"syscall/js"
|
"syscall/js"
|
||||||
|
|
||||||
promises "codeberg.org/danjones000/go-promises"
|
promises "codeberg.org/danjones000/go-promises"
|
||||||
|
|
@ -47,9 +46,5 @@ func main() {
|
||||||
return internal.GetUser(name.String(), age.Int()), nil
|
return internal.GetUser(name.String(), age.Int()), nil
|
||||||
}))
|
}))
|
||||||
|
|
||||||
js.Global().Get("String").Get("prototype").Set("toTitleCase", js.FuncOf(func(this js.Value, _ []js.Value) any {
|
|
||||||
return strings.Title(this.String())
|
|
||||||
}))
|
|
||||||
|
|
||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue