Skip to content

Commit

Permalink
add a readme, missing build tags, fix old code; addresses #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Smirnov committed Sep 27, 2018
1 parent 893f96a commit b1af1bc
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 17 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Go DOM binding (and more) for WebAssembly

This library provides a Go API for different Web APIs for WebAssembly target.

It's in an active development, but an API will be carefully versioned to
avoid breaking users.
Use Go dependency management tools to lock a specific version.

**Features:**

- Better JS API (wrappers for `syscall/js`)
- Basic DOM manipulation, styles, events
- Input elements
- SVG elements and transforms
- `LocalStorage` and `SessionStorage`
- Extension APIs (tested on Chrome):
- Native Messaging
- Bookmarks
- Tabs
- `net`-like library for WebSockets
- Tested with gRPC
- `wasm-server` for fast prototyping

## Quickstart

Pull the library and install `wasm-server` (optional):

```
go get -u github.com/dennwc/dom
go install github.com/dennwc/dom/cmd/wasm-server
```

Run an example app:

```
cd $GOPATH/src/github.com/dennwc/dom
wasm-server
```

Check result: http://localhost:8080/

The source code is recompiled on each page refresh, so feel free to experiment!
4 changes: 3 additions & 1 deletion extension/chrome/bookmarks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build js

package chrome

import "github.com/dennwc/dom/js"
Expand Down Expand Up @@ -33,7 +35,7 @@ func (b jsBookmarks) callAsync(name string, args ...interface{}) js.Value {
cb := js.NewEventCallback(func(v js.Value) {
ch <- v
})
defer cb.Close()
defer cb.Release()
args = append(args, cb)
b.v.Call(name, args...)
return <-ch
Expand Down
2 changes: 2 additions & 0 deletions extension/chrome/chrome.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build js

package chrome

import "github.com/dennwc/dom/js"
Expand Down
4 changes: 3 additions & 1 deletion extension/chrome/tabs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build js

package chrome

import "github.com/dennwc/dom/js"
Expand Down Expand Up @@ -26,7 +28,7 @@ func (t jsTabs) callAsync(name string, args ...interface{}) js.Value {
cb := js.NewEventCallback(func(v js.Value) {
ch <- v
})
defer cb.Close()
defer cb.Release()
args = append(args, cb)
t.v.Call(name, args...)
return <-ch
Expand Down
2 changes: 2 additions & 0 deletions js/bytes.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build wasm

package js

import (
Expand Down
2 changes: 2 additions & 0 deletions js/errors.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build wasm

package js

type Error struct {
Expand Down
2 changes: 2 additions & 0 deletions js/funcs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build wasm

package js

import (
Expand Down
3 changes: 3 additions & 0 deletions js/js.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//+build wasm

// Package JS provides additional functionality on top of syscall/js package for WASM.
package js

import (
Expand Down
16 changes: 16 additions & 0 deletions storage/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package storage

type Storage interface {
// Length returns an integer representing the number of data items stored in the Storage object.
Length() int
// Key will return the name of the nth key in the storage.
Key(ind int) string
// GetItem will return that key's value.
GetItem(key string) (string, bool)
// SetItem will add that key to the storage, or update that key's value if it already exists.
SetItem(key, val string)
// RemoveItem will remove that key from the storage.
RemoveItem(key string)
// Clear will empty all keys out of the storage.
Clear()
}
17 changes: 2 additions & 15 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
//+build js

package storage

import (
"syscall/js"
)

type Storage interface {
// Length returns an integer representing the number of data items stored in the Storage object.
Length() int
// Key will return the name of the nth key in the storage.
Key(ind int) string
// GetItem will return that key's value.
GetItem(key string) (string, bool)
// SetItem will add that key to the storage, or update that key's value if it already exists.
SetItem(key, val string)
// RemoveItem will remove that key from the storage.
RemoveItem(key string)
// Clear will empty all keys out of the storage.
Clear()
}

func getStorage(name string) Storage {
s := js.Global().Get("window").Get(name)
if s == js.Null() || s == js.Undefined() {
Expand Down
2 changes: 2 additions & 0 deletions svg/svg.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build wasm

package svg

import (
Expand Down

0 comments on commit b1af1bc

Please sign in to comment.