Skip to content

Commit

Permalink
Finish de-oneof-ing
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 25, 2018
1 parent 5b85863 commit 0fe6ab9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 79 deletions.
60 changes: 27 additions & 33 deletions msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ message Msg {
enum Command {
ERROR = 0;
START = 1;
SOURCE_CODE_FETCH_RES = 2;
ONEOF = 100;
SOURCE_CODE_FETCH = 2;
SOURCE_CODE_FETCH_RES = 3;
SOURCE_CODE_CACHE = 4;
EXIT = 5;
TIMER_START = 6;
TIMER_READY = 7;
TIMER_CLEAR = 8;
}
Command command = 2;

Expand All @@ -23,13 +28,17 @@ message Msg {
// potentially add many hundreds of commands. Therefore we just prefix command
// arguments by their name.

// Start
// START
string start_cwd = 10;
repeated string start_argv = 11;
bool start_debug_flag = 12;
string start_main_js = 13; // The contents of dist/main.js
string start_main_map = 14; // The contents of dist/main.map

// SOURCE_CODE_FETCH
string source_code_fetch_module_specifier = 20;
string source_code_fetch_containing_file = 21;

// SOURCE_CODE_FETCH_RES
// If it's a non-http module, moduleName and filename will be the same.
// For http modules, moduleName is its resolved http URL, and filename
Expand All @@ -39,38 +48,23 @@ message Msg {
string source_code_fetch_res_source_code = 32;
string source_code_fetch_res_output_code = 33; // Non-empty only if cached.

oneof payload {
SourceCodeFetchMsg source_code_fetch = 100;
SourceCodeCacheMsg source_code_cache = 102;
ExitMsg exit = 103;
TimerStartMsg timer_start = 104;
TimerReadyMsg timer_ready = 105;
TimerClearMsg timer_clear = 106;
}
}

message SourceCodeFetchMsg {
string module_specifier = 1;
string containing_file = 2;
}
// SOURCE_CODE_CACHE
string source_code_cache_filename = 41;
string source_code_cache_source_code = 42;
string source_code_cache_output_code = 43;

message SourceCodeCacheMsg {
string filename = 1;
string source_code = 2;
string output_code = 3;
}
// EXIT
int32 exit_code = 50;

message ExitMsg { int32 code = 1; }
// TIMER_START
int32 timer_start_id = 60;
bool timer_start_interval = 61;
int32 timer_start_duration = 62; // In milliseconds.

message TimerStartMsg {
int32 id = 1;
bool interval = 2;
int32 duration = 3; // In milliseconds.
}
// TIMER_READY
int32 timer_ready_id = 70;
bool timer_ready_done = 71;

message TimerReadyMsg {
int32 id = 1;
bool done = 2;
// TIMER_CLEAR
int32 timer_clear_id = 80;
}

message TimerClearMsg { int32 id = 1; }
24 changes: 12 additions & 12 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ func InitOS() {
Sub("os", func(buf []byte) []byte {
msg := &Msg{}
check(proto.Unmarshal(buf, msg))
switch msg.Payload.(type) {
case *Msg_Exit:
payload := msg.GetExit()
os.Exit(int(payload.Code))
case *Msg_SourceCodeFetch:
payload := msg.GetSourceCodeFetch()
return HandleSourceCodeFetch(payload.ModuleSpecifier,
payload.ContainingFile)
case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache()
return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
payload.OutputCode)
switch msg.Command {
case Msg_SOURCE_CODE_FETCH:
return HandleSourceCodeFetch(
msg.SourceCodeFetchModuleSpecifier,
msg.SourceCodeFetchContainingFile)
case Msg_SOURCE_CODE_CACHE:
return HandleSourceCodeCache(
msg.SourceCodeCacheFilename,
msg.SourceCodeCacheSourceCode,
msg.SourceCodeCacheOutputCode)
case Msg_EXIT:
os.Exit(int(msg.ExitCode))
default:
panic("[os] Unexpected message " + string(buf))
}
Expand Down
16 changes: 12 additions & 4 deletions os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { sendMsg } from "./dispatch";
import { main as pb } from "./msg.pb";
import { assert } from "./util";

export function exit(code = 0): void {
sendMsg("os", { exit: { code } });
export function exit(exitCode = 0): void {
sendMsg("os", {
command: pb.Msg.Command.EXIT,
exitCode
});
}

export function sourceCodeFetch(
moduleSpecifier: string,
containingFile: string
): ModuleInfo {
const res = sendMsg("os", {
sourceCodeFetch: { moduleSpecifier, containingFile }
command: pb.Msg.Command.SOURCE_CODE_FETCH,
sourceCodeFetchModuleSpecifier: moduleSpecifier,
sourceCodeFetchContainingFile: containingFile
});
assert(res.command === pb.Msg.Command.SOURCE_CODE_FETCH_RES);
return {
Expand All @@ -29,6 +34,9 @@ export function sourceCodeCache(
outputCode: string
): void {
sendMsg("os", {
sourceCodeCache: { filename, sourceCode, outputCode }
command: pb.Msg.Command.SOURCE_CODE_CACHE,
sourceCodeCacheFilename: filename,
sourceCodeCacheSourceCode: sourceCode,
sourceCodeCacheOutputCode: outputCode
});
}
33 changes: 15 additions & 18 deletions timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ 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()
timers[payload.Id] = &Timer{
Id: payload.Id,
switch msg.Command {
case Msg_TIMER_START:
id := msg.TimerStartId
t := &Timer{
Id: id,
Done: false,
Interval: payload.Interval,
Duration: payload.Duration,
Interval: msg.TimerStartInterval,
Duration: msg.TimerStartDuration,
Cleared: false,
}
timers[payload.Id].StartTimer()
t.StartTimer()
timers[id] = t
return nil
case *Msg_TimerClear:
payload := msg.GetTimerClear()
case Msg_TIMER_CLEAR:
// TODO maybe need mutex here.
timer := timers[payload.Id]
timer := timers[msg.TimerClearId]
timer.Clear()
return nil
default:
panic("[timers] Unexpected message " + string(buf))
}
return nil
})
}

Expand All @@ -62,12 +62,9 @@ func (t *Timer) StartTimer() {
t.Done = true
}
PubMsg("timers", &Msg{
Payload: &Msg_TimerReady{
TimerReady: &TimerReadyMsg{
Id: t.Id,
Done: t.Done,
},
},
Command: Msg_TIMER_READY,
TimerReadyId: t.Id,
TimerReadyDone: t.Done,
})
if t.Done {
return
Expand Down
26 changes: 14 additions & 12 deletions timers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { main as pb } from "./msg.pb";
import * as dispatch from "./dispatch";
import { assert } from "./util";

let nextTimerId = 1;

Expand All @@ -23,7 +24,9 @@ export function initTimers() {

function onMessage(payload: Uint8Array) {
const msg = pb.Msg.decode(payload);
const { id, done } = msg.timerReady;
assert(msg.command === pb.Msg.Command.TIMER_READY);
const id = msg.timerReadyId;
const done = msg.timerReadyDone;
const timer = timers.get(id);
if (!timer) {
return;
Expand All @@ -49,11 +52,10 @@ export function setTimeout(
};
timers.set(timer.id, timer);
dispatch.sendMsg("timers", {
timerStart: {
id: timer.id,
interval: false,
duration
}
command: pb.Msg.Command.TIMER_START,
timerStartId: timer.id,
timerStartInterval: false,
timerStartDuration: duration
});
return timer.id;
}
Expand All @@ -74,17 +76,17 @@ export function setInterval(
};
timers.set(timer.id, timer);
dispatch.sendMsg("timers", {
timerStart: {
id: timer.id,
interval: true,
duration: repeat
}
command: pb.Msg.Command.TIMER_START,
timerStartId: timer.id,
timerStartInterval: true,
timerStartDuration: repeat
});
return timer.id;
}

export function clearTimer(id: number) {
dispatch.sendMsg("timers", {
timerClear: { id }
command: pb.Msg.Command.TIMER_CLEAR,
timerClearId: id
});
}

0 comments on commit 0fe6ab9

Please sign in to comment.