Skip to content

Commit f62c6bc

Browse files
committed
MOAR CHANGES
1 parent 5464bc7 commit f62c6bc

File tree

8 files changed

+601
-8
lines changed

8 files changed

+601
-8
lines changed

buildwasm.ps1

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
$os = $env:GOOS
2+
$env:GOOS = "js"
3+
$arch = $env:GOARCH
4+
$env:GOARCH = "wasm"
5+
6+
go build -o main.wasm .\main.go .\stopwatch.go
7+
8+
$env:GOOS = $os
9+
$env:GOARCH = $arch

index.html

+42-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,32 @@
22
<html lang="en">
33

44
<head>
5-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<meta charset="utf-8">
6+
<script src="wasm_exec.js"></script>
7+
<script>
8+
const go = new Go();
9+
let mod, inst;
10+
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
11+
mod = result.module
12+
inst = result.instance
13+
go.run(result.instance);
14+
});
15+
16+
function disableButtons(disableStart) {
17+
let startb = document.getElementById("startbutton")
18+
let flipb = document.getElementById("flipbutton")
19+
let stopb = document.getElementById("stopbutton")
20+
if (disableStart) {
21+
startb.disabled = true;
22+
flipb.disabled = false;
23+
stopb.disabled = false;
24+
} else {
25+
startb.disabled = false;
26+
flipb.disabled = true;
27+
stopb.disabled = true;
28+
}
29+
}
30+
</script>
631
<style>
732
@font-face {
833
font-family: "Px437 IBM BIOS";
@@ -17,16 +42,18 @@
1742
}
1843

1944
button {
20-
2145
background: #282828;
2246
color: #33FF33;
2347
border-color: #33FF33;
2448
border-style: dashed;
2549
font-family: "Px437 IBM BIOS";
2650
font-size: 20px;
2751
padding: 10px;
28-
margin: 0 auto;
29-
display: block;
52+
}
53+
54+
button:disabled {
55+
color: #1B871B;
56+
border-color: #1B871B;
3057
}
3158
</style>
3259
</head>
@@ -35,20 +62,27 @@
3562
<table>
3663
<tr>
3764
<td>
38-
Working: <span id="working"></span>
65+
Working:
3966
</td>
4067
<td>
41-
Distracted: <span id="distracted"></span>
68+
<span id="working">00:00:00</span>
4269
</td>
4370
</tr>
4471
<tr>
4572
<td>
46-
<br />
73+
Distracted:
74+
</td>
75+
<td>
76+
<span id="distracted">00:00:00</span>
4777
</td>
4878
</tr>
4979
<tr>
5080
<td colspan="2">
51-
<button onclick="test()">Flip</button>
81+
<span class="mid">
82+
<button id="startbutton" onclick="start()">Start</button>
83+
<button id="flipbutton" onclick="flip()" disabled>Flip</button>
84+
<button id="stopbutton" onclick="stop()" disabled>Stop</button>
85+
</span>
5286
</td>
5387
</tr>
5488
</table>

main.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"syscall/js"
5+
"time"
6+
"unicode"
7+
8+
"github.com/dennwc/dom"
9+
)
10+
11+
// Format Duration
12+
func fd(d time.Duration) string {
13+
var ns string
14+
var period bool
15+
for _, r := range d.String() {
16+
if (period && unicode.IsDigit(r)) || r == '.' {
17+
period = true
18+
continue
19+
}
20+
ns += string(r)
21+
}
22+
return ns
23+
}
24+
25+
func main() {
26+
var working stopwatch
27+
var distracted stopwatch
28+
29+
js.Global().Set("start", js.NewCallback(func(v []js.Value) {
30+
dom.GetDocument().GetElementById("startbutton")
31+
working = newStopwatch(true)
32+
distracted = stopwatch{}
33+
js.Global().Call("disableButtons", true)
34+
}))
35+
js.Global().Set("flip", js.NewCallback(func(v []js.Value) {
36+
working.Flip()
37+
distracted.Flip()
38+
}))
39+
js.Global().Set("stop", js.NewCallback(func(v []js.Value) {
40+
working.Stop()
41+
distracted.Stop()
42+
js.Global().Call("disableButtons", false)
43+
}))
44+
45+
for {
46+
dom.GetDocument().GetElementById("working").SetInnerHTML(fd(working.Elapsed()))
47+
dom.GetDocument().GetElementById("distracted").SetInnerHTML(fd(distracted.Elapsed()))
48+
dom.ConsoleLog("\rWorking: %s (%s)\t\tDistracted %s (%s)\t\t", fd(working.Elapsed()), fd(working.lastElapsed), fd(distracted.Elapsed()), fd(distracted.lastElapsed))
49+
time.Sleep(time.Millisecond * 100)
50+
}
51+
}

main.wasm

2.71 MB
Binary file not shown.

run.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.\buildwasm.ps1
2+
go run .\testserver\main.go

stopwatch.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import "time"
4+
5+
type stopwatch struct {
6+
elapsed time.Duration
7+
lastElapsed time.Duration
8+
start time.Time
9+
lastStart time.Time
10+
isRunning bool
11+
}
12+
13+
func (s *stopwatch) Start() {
14+
if !s.isRunning {
15+
s.start = time.Now()
16+
s.lastStart = s.start
17+
s.isRunning = true
18+
}
19+
}
20+
21+
func (s *stopwatch) Stop() {
22+
if s.isRunning {
23+
s.lastElapsed = time.Since(s.lastStart)
24+
s.elapsed += time.Since(s.start)
25+
s.isRunning = false
26+
}
27+
}
28+
29+
func (s *stopwatch) Flip() {
30+
if s.isRunning {
31+
s.Stop()
32+
} else {
33+
s.Start()
34+
}
35+
}
36+
37+
func (s *stopwatch) Elapsed() time.Duration {
38+
if s.isRunning {
39+
s.elapsed += time.Since(s.start)
40+
s.start = time.Now()
41+
}
42+
return s.elapsed
43+
}
44+
45+
func newStopwatch(start bool) stopwatch {
46+
var sw stopwatch
47+
if start {
48+
sw.Start()
49+
}
50+
return sw
51+
}

testserver/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func main() {
8+
http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))
9+
}

0 commit comments

Comments
 (0)