mirror of
https://github.com/taigrr/wasm-experiments
synced 2025-01-18 04:03:21 -08:00
Add vugu example
This commit is contained in:
32
vugu/index.html
Normal file
32
vugu/index.html
Normal file
@@ -0,0 +1,32 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<script src="/wasm_exec.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="target">
|
||||
<img style="position: absolute; top: 50%; left: 50%;"
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/galleriffic/2.0.1/css/loader.gif">
|
||||
</div>
|
||||
<script>
|
||||
var wasmSupported = (typeof WebAssembly === "object");
|
||||
if (wasmSupported) {
|
||||
if (!WebAssembly.instantiateStreaming) { // polyfill
|
||||
WebAssembly.instantiateStreaming = async (resp, importObject) => {
|
||||
const source = await (await resp).arrayBuffer();
|
||||
return await WebAssembly.instantiate(source, importObject);
|
||||
};
|
||||
}
|
||||
const go = new Go();
|
||||
WebAssembly.instantiateStreaming(fetch("/main.wasm"), go.importObject).then((result) => {
|
||||
go.run(result.instance);
|
||||
});
|
||||
} else {
|
||||
document.getElementById("target").innerHTML = 'This application requires WebAssembly support. Please upgrade your browser.';
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
27
vugu/main.go
Normal file
27
vugu/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// +build js,wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/vugu/vugu"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rootInst, err := vugu.New(&Root{}, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
env := vugu.NewJSEnv("#target", rootInst, vugu.RegisteredComponentTypes())
|
||||
env.DebugWriter = os.Stdout
|
||||
|
||||
for ok := true; ok; ok = env.EventWait() {
|
||||
err = env.Render()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
172
vugu/root.go
Normal file
172
vugu/root.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package main
|
||||
|
||||
// DO NOT EDIT: This file was generated by vugu. Please regenerate instead of editing or add additional code in a separate file.
|
||||
|
||||
import "fmt"
|
||||
import "reflect"
|
||||
import "github.com/vugu/vugu"
|
||||
|
||||
import "encoding/json"
|
||||
import "net/http"
|
||||
import "log"
|
||||
|
||||
type RootData struct {
|
||||
bpi bpi
|
||||
isLoading bool
|
||||
}
|
||||
|
||||
type bpi struct {
|
||||
Time struct {
|
||||
Updated string `json:"updated"`
|
||||
} `json:"time"`
|
||||
BPI map[string]struct {
|
||||
Code string `json:"code"`
|
||||
Symbol string `json:"symbol"`
|
||||
RateFloat float64 `json:"rate_float"`
|
||||
} `json:"bpi"`
|
||||
}
|
||||
|
||||
func (data *RootData) HandleClick(event *vugu.DOMEvent) {
|
||||
data.bpi = bpi{}
|
||||
|
||||
go func(ee vugu.EventEnv) {
|
||||
ee.Lock()
|
||||
data.isLoading = true
|
||||
ee.UnlockRender()
|
||||
|
||||
res, err := http.Get("https://api.coindesk.com/v1/bpi/currentprice.json")
|
||||
if err != nil {
|
||||
log.Printf("Error fetch()ing: %v", err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var newb bpi
|
||||
err = json.NewDecoder(res.Body).Decode(&newb)
|
||||
if err != nil {
|
||||
log.Printf("Error JSON decoding: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
ee.Lock()
|
||||
defer ee.UnlockRender()
|
||||
data.bpi = newb
|
||||
data.isLoading = false
|
||||
}(event.EventEnv())
|
||||
}
|
||||
|
||||
var _ vugu.ComponentType = (*Root)(nil)
|
||||
|
||||
func (comp *Root) BuildVDOM(dataI interface{}) (vdom *vugu.VGNode, css *vugu.VGNode, reterr error) {
|
||||
data := dataI.(*RootData)
|
||||
_ = data
|
||||
_ = fmt.Sprint
|
||||
_ = reflect.Value{}
|
||||
event := vugu.DOMEventStub
|
||||
_ = event
|
||||
var n *vugu.VGNode
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute{vugu.VGAttribute{Namespace: "", Key: "class", Val: "demo-comp"}}}
|
||||
vdom = n
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
if data.isLoading {
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "Loading...", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
}
|
||||
}
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
if len(data.bpi.BPI) > 0 {
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "Updated: ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "span", DataAtom: vugu.VGAtom(40708), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n.InnerHTML = fmt.Sprint(data.bpi.Time.Updated)
|
||||
}
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "ul", DataAtom: vugu.VGAtom(42754), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t\t\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
for key, value := range data.bpi.BPI {
|
||||
_, _ = key, value
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "li", DataAtom: vugu.VGAtom(45570), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t\t\t\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "span", DataAtom: vugu.VGAtom(40708), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n.InnerHTML = fmt.Sprint(key)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: " ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "span", DataAtom: vugu.VGAtom(40708), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n.InnerHTML = fmt.Sprint(fmt.Sprint(value.Symbol, value.RateFloat))
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t\t\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
}
|
||||
}
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
}
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
}
|
||||
}
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n\t", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "button", DataAtom: vugu.VGAtom(102662), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
// @click = { data.HandleClick(event) }
|
||||
{
|
||||
var i_ interface{} = data
|
||||
idat_ := reflect.ValueOf(&i_).Elem().InterfaceData()
|
||||
var i2_ interface{} = data.HandleClick
|
||||
i2dat_ := reflect.ValueOf(&i2_).Elem().InterfaceData()
|
||||
n.SetDOMEventHandler("click", vugu.DOMEventHandler{
|
||||
ReceiverAndMethodHash: uint64(idat_[0]) ^ uint64(idat_[1]) ^ uint64(i2dat_[0]) ^ uint64(i2dat_[1]),
|
||||
Method: reflect.ValueOf(data).MethodByName("HandleClick"),
|
||||
Args: []interface{}{event},
|
||||
})
|
||||
}
|
||||
if false {
|
||||
// force compiler to check arguments for type safety
|
||||
data.HandleClick(event)
|
||||
}
|
||||
{
|
||||
parent := n
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "Fetch Bitcoin Price Index", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
}
|
||||
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
|
||||
parent.AppendChild(n)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Root struct {}
|
||||
|
||||
func (ct *Root) NewData(props vugu.Props) (interface{}, error) { return &RootData{}, nil }
|
||||
|
||||
func init() { vugu.RegisterComponentType("root", &Root{}) }
|
||||
64
vugu/root.vugu
Normal file
64
vugu/root.vugu
Normal file
@@ -0,0 +1,64 @@
|
||||
<div class="demo-comp">
|
||||
<div vg-if='data.isLoading'>Loading...</div>
|
||||
<div vg-if='len(data.bpi.BPI) > 0'>
|
||||
<div>Updated: <span vg-html='data.bpi.Time.Updated'></span></div>
|
||||
<ul>
|
||||
<li vg-for='data.bpi.BPI'>
|
||||
<span vg-html='key'></span> <span vg-html='fmt.Sprint(value.Symbol, value.RateFloat)'></span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<button @click="data.HandleClick(event)">Fetch Bitcoin Price Index</button>
|
||||
</div>
|
||||
|
||||
<script type="application/x-go">
|
||||
import "encoding/json"
|
||||
import "net/http"
|
||||
import "log"
|
||||
|
||||
type RootData struct {
|
||||
bpi bpi
|
||||
isLoading bool
|
||||
}
|
||||
|
||||
type bpi struct {
|
||||
Time struct {
|
||||
Updated string `json:"updated"`
|
||||
} `json:"time"`
|
||||
BPI map[string]struct {
|
||||
Code string `json:"code"`
|
||||
Symbol string `json:"symbol"`
|
||||
RateFloat float64 `json:"rate_float"`
|
||||
} `json:"bpi"`
|
||||
}
|
||||
|
||||
func (data *RootData) HandleClick(event *vugu.DOMEvent) {
|
||||
data.bpi = bpi{}
|
||||
|
||||
go func(ee vugu.EventEnv) {
|
||||
ee.Lock()
|
||||
data.isLoading = true
|
||||
ee.UnlockRender()
|
||||
|
||||
res, err := http.Get("https://api.coindesk.com/v1/bpi/currentprice.json")
|
||||
if err != nil {
|
||||
log.Printf("Error fetch()ing: %v", err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var newb bpi
|
||||
err = json.NewDecoder(res.Body).Decode(&newb)
|
||||
if err != nil {
|
||||
log.Printf("Error JSON decoding: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
ee.Lock()
|
||||
defer ee.UnlockRender()
|
||||
data.bpi = newb
|
||||
data.isLoading = false
|
||||
}(event.EventEnv())
|
||||
}
|
||||
|
||||
</script>
|
||||
Reference in New Issue
Block a user