diff --git a/Makefile b/Makefile index 86a579d66b..6139307814 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 409a7940d8..ebeaf87c13 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 similarity index 76% rename from handlers.go rename to os.go index 237aa822e6..214492eeab 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 0000000000..3a1efa1033 --- /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 +}