Skip to content

Commit 4d8e150

Browse files
committed
wasm
1 parent 7ea37e4 commit 4d8e150

File tree

15 files changed

+132
-168
lines changed

15 files changed

+132
-168
lines changed

go.work

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ go 1.24.0
33
use (
44
./lockfree
55
./secretary
6-
./wasm
76
)

secretary/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ gen:
5252
buf dep update
5353
buf format -w
5454
buf generate
55+
56+
.PHONY: wasm
57+
wasm:
58+
GOOS=js GOARCH=wasm go build -o ./secretaryui/secretary.wasm ./wasm
59+
60+
cpwasm:
61+
cp $(shell go env GOROOT)/lib/wasm/wasm_exec.js ./secretaryui

secretary/secretaryui/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@
226226
</div>
227227
</div>
228228

229+
<button id="add-btn">ADD</button>
230+
<button onClick="allTree();">AllTree</button>
231+
<button onClick="set();">SET</button>
232+
229233
<div id="info-box"></div>
230234

231235
<div id="result"></div>
@@ -256,4 +260,6 @@
256260

257261
<script type="module" src="/src/main.ts"></script>
258262

263+
<script src="wasm_exec.js"></script>
264+
259265
</html>

secretary/secretaryui/src/collection.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,22 @@ export async function fetchAllBTree() {
117117
await makeRequest(
118118
`${ui.url}/getalltree`,
119119
undefined,
120-
(data: any[]) => {
120+
(data: any[], error: boolean) => {
121+
122+
console.log(data)
123+
124+
if (error) {
125+
ui.WASM = true
126+
127+
// ui.func.allTree()
128+
129+
console.log(ui.func)
130+
131+
console.log("---> ", ui.func.allTree())
132+
133+
return
134+
}
135+
121136
const collectionsDiv = document.getElementById("collections")!;
122137
collectionsDiv.innerHTML = "";
123138

@@ -259,16 +274,17 @@ async function makeRequest(
259274
parameters: RequestInit | undefined,
260275
after: (result: any, error: boolean) => void
261276
) {
262-
const response = await fetch(url, parameters);
277+
let response;
263278
let result;
264279

265280
try {
281+
response = await fetch(url, parameters);
266282
result = await response.json();
267283
} catch {
268284
result = { data: null, logs: "Invalid JSON response" };
269285
}
270286

271-
const hasError = !response.ok || response.status !== 200;
287+
const hasError = !response?.ok || response.status !== 200;
272288
appendResult(url, result.data, result.logs, hasError);
273289
after(result.data, hasError);
274290
}

secretary/secretaryui/src/main.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import { BTree, BTreeNode, NodeDef, TreeDef } from './tree';
44
import { canvasSection, modalOverlay, openModalBtn, themeToggle } from './dom';
55
import { setupDraw } from './draw';
66
import Split from 'split.js'
7+
import { Func, SetupWASM } from './wasm';
78

8-
// document.addEventListener('DOMContentLoaded', () => {
99
Split(['#json-section', "#canvas-section", '#form-section'],
1010
{
1111
sizes: [15, 70, 15],
1212
// gutterSize: 4,
1313
}
1414
);
1515
(document.querySelector('.container') as HTMLElement).style.visibility = 'visible';
16-
// });
1716

1817
class Ui {
1918
graph: dia.Graph
@@ -26,6 +25,9 @@ class Ui {
2625

2726
DARK: boolean
2827

28+
func!: Func
29+
WASM: boolean = false
30+
2931
router: "normal" | "orthogonal" = "orthogonal"
3032
connector: "straight" | "curve" = "straight"
3133

@@ -66,7 +68,10 @@ class Ui {
6668
let dark = localStorage.getItem("theme") === "dark"
6769
export let ui = new Ui(dark)
6870

69-
fetchAllBTree()
71+
SetupWASM()
72+
setTimeout(() => {
73+
fetchAllBTree()
74+
}, 2000)
7075
setupCollectionRequest()
7176
setupDraw()
7277

secretary/secretaryui/src/wasm.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { resultDiv } from "./dom";
2+
import { ui } from "./main";
3+
4+
let WASM = "secretary.wasm"
5+
6+
declare const Go: any;
7+
8+
export interface Func {
9+
add: (a: number, b: number) => number
10+
allTree: () => string
11+
set: (v: string) => null
12+
}
13+
14+
interface Window {
15+
lib: Func;
16+
}
17+
18+
declare let window: Window;
19+
20+
const go = new Go();
21+
let mod: WebAssembly.Module, inst: WebAssembly.Instance;
22+
WebAssembly.instantiateStreaming(fetch(WASM), go.importObject).then(async (result) => {
23+
mod = result.module;
24+
inst = result.instance;
25+
26+
go.run(inst); // Start Go runtime
27+
28+
ui.func = window.lib
29+
30+
}).catch((err) => {
31+
console.error(err);
32+
});
33+
34+
export function SetupWASM() {
35+
document.getElementById("add-btn")?.addEventListener("click", async () => {
36+
add()
37+
});
38+
}
39+
40+
async function add() {
41+
42+
if (!inst) {
43+
console.error("WebAssembly instance not loaded yet!");
44+
return;
45+
}
46+
47+
if (typeof ui.func.add === "function") {
48+
resultDiv.innerText = "Result: " + ui.func.add(10, 20);
49+
} else {
50+
console.error("ui.func.add is undefined!");
51+
}
52+
}
53+
54+
async function allTree() {
55+
if (!inst) {
56+
console.error("WebAssembly instance not loaded yet!");
57+
return;
58+
}
59+
60+
if (typeof ui.func.allTree === "function") {
61+
resultDiv.innerText = "Result: " + ui.func.allTree();
62+
} else {
63+
console.error("ui.func.allTree is undefined!");
64+
}
65+
}
66+
67+
async function set() {
68+
if (!inst) {
69+
console.error("WebAssembly instance not loaded yet!");
70+
return;
71+
}
72+
73+
if (typeof ui.func.set === "function") {
74+
resultDiv.innerText = "Result: " + ui.func.set("hello");
75+
} else {
76+
console.error("ui.func.set is undefined!");
77+
}
78+
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/codeharik/secretary"
1313
"github.com/codeharik/secretary/utils"
14-
"github.com/codeharik/wasm"
1514
)
1615

1716
var SECRETARY *secretary.Secretary
@@ -44,6 +43,9 @@ func init() {
4443

4544
func main() {
4645
var lib map[string]any = map[string]any{
46+
"allTree": js.FuncOf(allTree),
47+
"allTree_info": "allTree() -> json",
48+
4749
"set": js.FuncOf(set),
4850
"set_info": "set(value string) -> string",
4951

@@ -54,11 +56,20 @@ func main() {
5456
"sub_info": "sub(a int, b int) -> int",
5557
}
5658

57-
wasm.SetWASMLibaray(lib)
59+
SetWASMLibaray(lib)
5860

5961
select {}
6062
}
6163

64+
func allTree(this js.Value, args []js.Value) any {
65+
data, err := SECRETARY.HandleGetAllTree()
66+
if err != nil {
67+
return js.ValueOf(err.Error())
68+
}
69+
70+
return js.ValueOf(string(data))
71+
}
72+
6273
func set(this js.Value, args []js.Value) any {
6374
value := args[0].String()
6475

wasm/utils.go renamed to secretary/wasm/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build js && wasm
22
// +build js,wasm
33

4-
package wasm
4+
package main
55

66
import (
77
"fmt"

0 commit comments

Comments
 (0)