Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.wasm
dist
dist
.gocache
26 changes: 26 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "npm run build",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
},
"windows": {
"options": {
"shell": {
"executable": "powershell.exe"
}
}
}
}
]
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ import (
"github.com/aaronpowell/webpack-golang-wasm-async-loader/gobridge"
)

func add(i ...js.Value) js.Value {
func add(i []js.Value) (interface{},error) {
ret := 0

for _, item := range i {
val, _ := strconv.Atoi(item.String())
ret += val
}

return js.ValueOf(ret)
return ret, nil
}

func main() {
Expand All @@ -96,9 +96,9 @@ To do this a function is exported from the package called `RegisterCallback` whi

* A `string` representing the name to register it as in JavaScript (and what you'll call it using)
* The `func` to register as a callback
* Note: The `func` must has a signature of `(args ...js.Value) js.Value` and you are responsible to box/unbox the JavaScript values to the appropriate Go types. Similarly you need to box the return type as a `js.Value`
* The `func` must has a signature of `(args js.Value) (interface{}, error)` so you can raise an error if you need

If you want to register a static value that's been created from Go to be available in JavaScript you can do that with `RegisterValue`, which takes a name and a `js.Value`. Values are converted to functions that return a Promise so they can be treated asynchronously like function invocations.
If you want to register a static value that's been created from Go to be available in JavaScript you can do that with `RegisterValue`, which takes a name and a value. Values are converted to functions that return a Promise so they can be treated asynchronously like function invocations.

In JavaScript a global object is registered as `__gobridge__` which the registrations happen against.

Expand Down
4 changes: 3 additions & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Helps when your error handling is looking at the error argument to the callback...
* Merged [PR #1](https://github.com/aaronpowell/webpack-golang-wasm-async-loader/pull/1) to update to Go 1.12
* Added error handling support
* Improved examples
66 changes: 66 additions & 0 deletions azure-pipelines.pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pr:
- master

pool:
vmImage: 'Ubuntu-16.04'

steps:
- task: NodeTool@0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'

- script: |
npm install
npm run build
displayName: 'npm install and build'

- script: |
npm pack
displayName: 'Package for npm release'

- task: CopyFiles@2
inputs:
sourceFolder: '$(Build.SourcesDirectory)/dist'
contents: '**'
targetFolder: $(Build.ArtifactStagingDirectory)/dist
displayName: 'Copy dist output'

- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)/dist'
artifactName: dist
displayName: 'Publish dist files as artifacts'

- task: CopyFiles@2
inputs:
sourceFolder: '$(Build.SourcesDirectory)'
contents: '*.tgz'
targetFolder: $(Build.ArtifactStagingDirectory)/npm
displayName: 'Copy npm package'

- task: CopyFiles@2
inputs:
sourceFolder: '$(Build.SourcesDirectory)'
contents: 'package.json'
targetFolder: $(Build.ArtifactStagingDirectory)/npm
displayName: 'Copy package.json'

- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)/npm'
artifactName: npm
displayName: 'Publish npm artifact'

- task: CopyFiles@2
inputs:
sourceFolder: '$(Build.SourcesDirectory)'
contents: 'ReleaseNotes.md'
targetFolder: $(Build.ArtifactStagingDirectory)/release-notes
displayName: 'Copy Release Notes'

- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)/release-notes'
artifactName: release-notes
displayName: 'Publish Release Notes artifact'
4 changes: 3 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
trigger:
- master
branches:
include:
- master

pool:
vmImage: 'Ubuntu-16.04'
Expand Down
18 changes: 18 additions & 0 deletions example/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "golang-wasm-async-loader-example-nodejs",
"private": true,
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"dependencies": {
},
"devDependencies": {
},
"scripts": {
"predemo": "GOOS=js GOARCH=wasm go build -o ./src/main.wasm ./src/main.go",
"demo": "node ./src/index.js"
},
"keywords": [],
"author": "",
"license": "MIT"
}
20 changes: 20 additions & 0 deletions example/node/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const gobridge = require('../../../dist/gobridge');
const { join } = require('path');
require('../../../lib/wasm_exec.js');
require('isomorphic-fetch');
const { readFileSync } = require('fs');

global.requestAnimationFrame = global.setImmediate;

let p = new Promise(resolve =>
resolve(readFileSync(join(__dirname, 'main.wasm')))
);
const wasm = gobridge.default(p);

async function run() {
let result = await wasm.add(1,2);

console.log(result);
};

run();
38 changes: 38 additions & 0 deletions example/node/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//+ build js,wasm

package main

import (
"errors"
"strconv"
"syscall/js"

"../../../gobridge"
)

var global = js.Global()

func add(this js.Value, args []js.Value) (interface{}, error) {
ret := 0

for _, item := range args {
val, _ := strconv.Atoi(item.String())
ret += val
}

return ret, nil
}

func err(this js.Value, args []js.Value) (interface{}, error) {
return nil, errors.New("This is an error")
}

func main() {
c := make(chan struct{}, 0)
println("Web Assembly is ready")
gobridge.RegisterCallback("add", add)
gobridge.RegisterCallback("raiseError", err)
gobridge.RegisterValue("someValue", "Hello World")

<-c
}
31 changes: 0 additions & 31 deletions example/src/main.go

This file was deleted.

File renamed without changes.
Loading