1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -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 \ assets.go \
deno_dir.go \ deno_dir.go \
dispatch.go \ dispatch.go \
handlers.go \
main.go \ main.go \
main_test.go \ main_test.go \
msg.pb.go \ msg.pb.go \
os.go \
timers.go \
util.go util.go
deno: $(GO_FILES) deno: $(GO_FILES)

View file

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

View file

@ -5,12 +5,11 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"time"
) )
const assetPrefix string = "/$asset$/" const assetPrefix string = "/$asset$/"
func InitHandlers() { func InitOS() {
Sub("os", func(buf []byte) []byte { Sub("os", func(buf []byte) []byte {
msg := &Msg{} msg := &Msg{}
check(proto.Unmarshal(buf, msg)) check(proto.Unmarshal(buf, msg))
@ -30,18 +29,6 @@ func InitHandlers() {
} }
return nil 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) { func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
@ -110,21 +97,3 @@ func HandleSourceCodeCache(filename string, sourceCode string,
check(err) check(err)
return out 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
}