Skip to content

Commit 5ff9d6f

Browse files
committed
feat(example/basic): add interactive page to demonstrate WASM working
1 parent 3933631 commit 5ff9d6f

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

example/basic/dist/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
</head>
88

99
<body>
10+
<h1>This is an example Go WASM Project.</h1>
11+
<h3>
12+
<a href="https://gitea.teamortix.com/Team-Ortix/go-mod-wasm/src/branch/master/example/basic/src/api/main.go">
13+
Sample Go code
14+
</a>
15+
</h3>
16+
17+
<h2>Value from Go:</h2>
18+
<h3 id="value">&nbsp;</h3>
19+
20+
<h2>Call function from Go:</h2>
21+
<input type="text" id="input" value="Team Ortix" />
22+
<h3 id="funcValue">&nbsp;</h3>
1023
<script src="main.js"></script>
1124
</body>
1225

example/basic/src/api/bridge.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
// Wrapper is a simple JS function that when called with a Go Function, will return a new function that will throw
1515
// if the property `error` is an instance of JavaScript's `error`.
1616
//
17-
// All Go functions in the bridgeName proxy are expected to be the result of calling wrapper with the Go function.
17+
// All Go functions in the bridgeName proxy are expected to be the result of calling wrapper with the Go function.
1818
wrapper js.Value
1919
)
2020

@@ -39,11 +39,11 @@ func newReturnError(goErr error) js.Value {
3939
}
4040

4141
// Using this wrapper makes it possible to throw errors in go-fashion.
42-
// This means that all wrapped functions must return error and a value.
42+
// This means that all wrapped functions must return value and an error (respectively).
4343
//
4444
// The __wrapper__ function from JS will automatically throw if the returned object has an 'error' property.
45-
// Inversly, it will automatically give the result value if that property exists.
46-
// All go functions directly returned via wasm should keep this in mind.
45+
// Inversely, it will automatically give the result value if that property exists.
46+
// All Go functions directly returned via wasm should keep this in mind.
4747
func wrapGoFunc(f func(js.Value, []js.Value) (interface{}, error)) js.Func {
4848
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
4949
res, err := f(this, args)

example/basic/src/api/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const hello = "Hello!"
1313
// If returning a non-nil error value, the resulting promise will be rejected by API consumers.
1414
// The rejected value will JavaScript's Error, with the message being the go error's message.
1515
//
16-
// See other examples which use the Go wasm bridge api, which show more flexibility and type safety when interacting
16+
// See other examples which use the Go WASM bridge api, which show more flexibility and type safety when interacting
1717
// with JavaScript.
1818
func helloName(_ js.Value, args []js.Value) (interface{}, error) {
1919
return fmt.Sprintf("Hello, %s!", args[0].String()), nil

example/basic/src/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ import wasm from './api/main.go';
22

33
const { hello, helloName } = wasm;
44

5-
(async () => {
6-
console.log(await hello());
7-
console.log(await helloName("world"));
8-
})()
5+
const value = document.getElementById("value");
6+
const input = document.getElementById("input");
7+
const funcValue = document.getElementById("funcValue");
8+
9+
const run = async () => {
10+
value.innerText = await hello();
11+
12+
funcValue.innerText = await helloName(input.value);
13+
input.addEventListener("keyup", async (e) => {
14+
funcValue.innerText = await helloName(e.target.value);
15+
})
16+
}
17+
18+
run()

src/bridge.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ if (!g.__go_wasm__) {
77
/**
88
* The maximum amount of time that we would expect Wasm to take to initialize.
99
* If it doesn't initialize after this time, we send a warning to console.
10-
* Most likely something has gone wrong if it takes more than 3 sconds to initialize.
10+
* Most likely something has gone wrong if it takes more than 3 seconds to initialize.
1111
*/
1212
const maxTime = 3 * 1000;
1313

1414
/**
15-
* bridge is an easier way to refer to the Go wasm object.
15+
* bridge is an easier way to refer to the Go WASM object.
1616
*/
1717
const bridge = g.__go_wasm__;
1818

@@ -40,7 +40,7 @@ function wrapper(goFunc) {
4040
/**
4141
* Sleep is used when awaiting for Go Wasm to initialize.
4242
* It uses the lowest possible sane delay time (via requestAnimationFrame).
43-
* However, if the window is not focused, the function never returns.
43+
* However, if the window is not focused, requestAnimationFrame never returns.
4444
* A timeout will ensure to be called after 50 ms, regardless of whether or not the tab is in focus.
4545
*
4646
* @returns {Promise} an always-resolving promise when a tick has been completed
@@ -79,7 +79,7 @@ export default function (getBytes) {
7979
init();
8080
setTimeout(() => {
8181
if (bridge.__ready__ !== true) {
82-
console.warn("Golang Wasm Bridge (__go_wasm__.__ready__) still not true after max time");
82+
console.warn("Golang WASM Bridge (__go_wasm__.__ready__) still not true after max time");
8383
}
8484
}, maxTime);
8585

@@ -98,7 +98,7 @@ export default function (getBytes) {
9898
res(bridge[key]);
9999

100100
if (args.length !== 0) {
101-
console.warn("Retrieved value from web assembly returned non-error type, however called with arguments.")
101+
console.warn("Retrieved value from WASM returned non-error type, however called with arguments.")
102102
}
103103
return;
104104
}

0 commit comments

Comments
 (0)