-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (125 loc) · 4.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"strconv"
"syscall/js"
"wasmnet/pool"
"wasmnet/utils"
)
var (
poolExecCount int
)
func registerCallbacks() {
js.Global().Set("helloWorld", js.FuncOf(helloWorld))
js.Global().Set("launchPool", js.FuncOf(launchPool))
js.Global().Set("poolHandler", js.FuncOf(poolHandler))
js.Global().Set("launchForLoop", js.FuncOf(launchForLoop))
js.Global().Set("forHandler", js.FuncOf(forHandler))
js.Global().Set("launchPeerJS", js.FuncOf(launchPeerJS))
js.Global().Set("peerJSHandler", js.FuncOf(peerJSHandler))
js.Global().Set("launchWebGL", js.FuncOf(launchWebGL))
}
func main() {
c := make(chan struct{}, 0)
println("Go WebAssembly")
// Register js functions to go
registerCallbacks()
<-c
}
// Hello World example
func helloWorld(this js.Value, i []js.Value) interface{} {
phrase := "Hello, world!"
js.Global().Call("alert", phrase)
return phrase
}
// Built-in task-runner / worker-pool
func launchPool(this js.Value, i []js.Value) interface{} {
poolDiv := js.Global().Get("document").Call("getElementById", "poolDiv")
js.Global().Get("document").Call("getElementById", "runPoolButton").Set("disabled", false)
if poolDiv.Get("style").Get("display").String() == "none" {
//fmt.Println("Show.1")
poolDiv.Get("style").Set("display", "block")
} else if poolDiv.Get("style").Get("display").String() == "block" {
//fmt.Println("Hide.2")
poolDiv.Get("style").Set("display", "none")
} else {
//fmt.Println("Show.0")
poolDiv.Get("style").Set("display", "block")
}
return nil
}
// Handles input submitted via Frontend of form (forLoopDiv)
func poolHandler(this js.Value, i []js.Value) interface{} {
//limitValue := js.Global().Get("document").Call("getElementById", i[0].String()).Get("value").String()
//js.Global().Set("output", limitValue)
outputTextArea := js.Global().Get("document").Call("getElementById", "poolOutput")
outputTextArea.Set("value", "")
poolExecCount += 1
runCount := fmt.Sprintf("POOL_RUN %d", poolExecCount)
utils.UpdatePoolOutput(runCount)
pool.InitClient(100)
return nil
}
// Show/Hide Frontend forLoopDiv
func launchForLoop(this js.Value, i []js.Value) interface{} {
forLoopDiv := js.Global().Get("document").Call("getElementById", "forLoopDiv")
js.Global().Get("document").Call("getElementById", "runForButton").Set("disabled", false)
if forLoopDiv.Get("style").Get("display").String() == "none" {
forLoopDiv.Get("style").Set("display", "block")
} else if forLoopDiv.Get("style").Get("display").String() == "block" {
forLoopDiv.Get("style").Set("display", "none")
} else {
forLoopDiv.Get("style").Set("display", "block")
}
return nil
}
// Handles input submitted via Frontend of form (forLoopDiv)
func forHandler(this js.Value, i []js.Value) interface{} {
limitValue := js.Global().Get("document").Call("getElementById", i[0].String()).Get("value").String()
js.Global().Set("output", limitValue)
intLimit, _ := strconv.Atoi(limitValue)
println("forHandler() limit:", intLimit)
textAreaInput := js.Global().Get("document").Call("getElementById", "forInput")
fmt.Println("TextArea Located:", textAreaInput.Get("name"))
targetLine := fmt.Sprintf("%d", intLimit)
utils.UpdateInputElement(targetLine) // update via Frontend
if intLimit != 0 {
for i := 0; i <= intLimit; i++ {
targetLine := fmt.Sprintf("i = %d\n", i)
fmt.Println(targetLine)
utils.UpdateConsoleElement(targetLine) // update via Frontend
}
}
return nil
}
// Show/Hide Frontend peerJSDiv
func launchPeerJS(this js.Value, i []js.Value) interface{} {
peerJSDiv := js.Global().Get("document").Call("getElementById", "peerJSDiv")
js.Global().Get("document").Call("getElementById", "runPeerJSButton").Set("disabled", false)
if peerJSDiv.Get("style").Get("display").String() == "none" {
peerJSDiv.Get("style").Set("display", "block")
} else if peerJSDiv.Get("style").Get("display").String() == "block" {
peerJSDiv.Get("style").Set("display", "none")
} else {
peerJSDiv.Get("style").Set("display", "block")
}
return nil
}
// Handles input submitted via Frontend of form (peerJSDiv)
func peerJSHandler(this js.Value, i []js.Value) interface{} {
//targetURL := js.Global().Get("document").Call("getElementById", i[0].String()).Get("value").String()
return nil
}
// Show/Hide Frontend webGLDiv
func launchWebGL(this js.Value, i []js.Value) interface{} {
webGLDiv := js.Global().Get("document").Call("getElementById", "webGLDiv")
if webGLDiv.Get("style").Get("display").String() == "none" {
webGLDiv.Get("style").Set("display", "block")
} else if webGLDiv.Get("style").Get("display").String() == "block" {
webGLDiv.Get("style").Set("display", "none")
} else {
webGLDiv.Get("style").Set("display", "block")
}
// webGL.InitCanvas()
return nil
}