summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--main.go4
-rw-r--r--os.go (renamed from handlers.go)33
-rw-r--r--timers.go38
4 files changed, 44 insertions, 34 deletions
diff --git a/Makefile b/Makefile
index 86a579d66..613930781 100644
--- a/Makefile
+++ b/Makefile
@@ -15,10 +15,11 @@ GO_FILES = \
assets.go \
deno_dir.go \
dispatch.go \
- handlers.go \
main.go \
main_test.go \
msg.pb.go \
+ os.go \
+ timers.go \
util.go
deno: $(GO_FILES)
diff --git a/main.go b/main.go
index 409a7940d..ebeaf87c1 100644
--- a/main.go
+++ b/main.go
@@ -55,7 +55,9 @@ func main() {
createDirs()
createWorker()
- InitHandlers()
+
+ InitOS()
+ InitTimers()
main_js := stringAsset("main.js")
check(worker.Load("/main.js", main_js))
diff --git a/handlers.go b/os.go
index 237aa822e..214492eea 100644
--- a/handlers.go
+++ b/os.go
@@ -5,12 +5,11 @@ import (
"io/ioutil"
"os"
"strings"
- "time"
)
const assetPrefix string = "/$asset$/"
-func InitHandlers() {
+func InitOS() {
Sub("os", func(buf []byte) []byte {
msg := &Msg{}
check(proto.Unmarshal(buf, msg))
@@ -30,18 +29,6 @@ func InitHandlers() {
}
return nil
})
-
- Sub("timers", func(buf []byte) []byte {
- msg := &Msg{}
- check(proto.Unmarshal(buf, msg))
- switch msg.Payload.(type) {
- case *Msg_TimerStart:
- payload := msg.GetTimerStart()
- return HandleTimerStart(payload.Id, payload.Interval, payload.Duration)
- default:
- panic("[timers] Unexpected message " + string(buf))
- }
- })
}
func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
@@ -110,21 +97,3 @@ func HandleSourceCodeCache(filename string, sourceCode string,
check(err)
return out
}
-
-func HandleTimerStart(id int32, interval bool, duration int32) []byte {
- wg.Add(1)
- go func() {
- defer wg.Done()
- time.Sleep(time.Duration(duration) * time.Millisecond)
- payload, err := proto.Marshal(&Msg{
- Payload: &Msg_TimerReady{
- TimerReady: &TimerReadyMsg{
- Id: id,
- },
- },
- })
- check(err)
- Pub("timers", payload)
- }()
- return nil
-}
diff --git a/timers.go b/timers.go
new file mode 100644
index 000000000..3a1efa103
--- /dev/null
+++ b/timers.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "github.com/golang/protobuf/proto"
+ "time"
+)
+
+func InitTimers() {
+ Sub("timers", func(buf []byte) []byte {
+ msg := &Msg{}
+ check(proto.Unmarshal(buf, msg))
+ switch msg.Payload.(type) {
+ case *Msg_TimerStart:
+ payload := msg.GetTimerStart()
+ return HandleTimerStart(payload.Id, payload.Interval, payload.Duration)
+ default:
+ panic("[timers] Unexpected message " + string(buf))
+ }
+ })
+}
+
+func HandleTimerStart(id int32, interval bool, duration int32) []byte {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ time.Sleep(time.Duration(duration) * time.Millisecond)
+ payload, err := proto.Marshal(&Msg{
+ Payload: &Msg_TimerReady{
+ TimerReady: &TimerReadyMsg{
+ Id: id,
+ },
+ },
+ })
+ check(err)
+ Pub("timers", payload)
+ }()
+ return nil
+}