HuckRidgeSW / hvue

A GopherJS & go/wasm binding for Vue.js
MIT License
49 stars 10 forks source link

WASM support #11

Closed HuckRidgeSW closed 6 years ago

HuckRidgeSW commented 6 years ago

Updated the core code and the examples so that Go/wasm and GopherJS can both compile and run them. Used github.com/gopherjs/gopherwasm/js as a compatibility layer from go/wasm-style code to GopherJS.

This required api changes: mostly changing from GopherJS's *js.Object to go/wasm's js.Value (technically, gopherwasm/js's js.Value), but there were some more substantive changes, too.

Calling a Go function from JavaScript and having it run synchronously and return a value doesn't currently work (https://github.com/golang/go/issues/26045). Vue expects that to work (reasonably enough!). So some hvue functions to wrap that functionality currently just panic. I think hvue.Computed and hvue.ComputedWithGetSet are the only ones. Some others just do nothing and return (hvue.Validator).

Another big difference is that GopherJS has "split" objects, where Go/wasm just has js.Value. That is, in GopherJS, you can say

struct foo {
    *js.Object
    bar int `js:"bar"`
}

and then when you set bar, that automagically translates to setting Object.bar, where foo.Object is the Javascript version of the object. (See https://github.com/gopherjs/gopherjs/wiki/JavaScript-Tips-and-Gotchas for more on that.)

Go/wasm doesn't do that. This makes JavaScript object reference quite a bit less magical, which is a good thing, but a little more cumbersome, too (alas). So, you can scatter .Get("...") and .Set("...", value) everywhere (making typoed field names quite problematic), or you can do like I did and write access functions (where typoed field names are still problematic but much less likely).