1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Organize modules: timers, os

This commit is contained in:
Ryan Dahl 2018-05-21 23:00:36 -04:00
parent 08307fb841
commit 51692a5825
4 changed files with 44 additions and 34 deletions

View file

@ -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)

View file

@ -55,7 +55,9 @@ func main() {
createDirs()
createWorker()
InitHandlers()
InitOS()
InitTimers()
main_js := stringAsset("main.js")
check(worker.Load("/main.js", main_js))

View file

@ -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
}

38
timers.go Normal file
View file

@ -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
}