summaryrefslogtreecommitdiff
path: root/main.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-21 22:07:40 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-21 22:07:40 -0400
commit08307fb84160602da12ba4d1b118c859e8a73cdb (patch)
treeb8f3bf7c4df3904937afc964ae36803567e78719 /main.ts
parent9a6621659937c55c6005a1fa6ce9641a4ceff385 (diff)
Add dispatch pub/sub
Diffstat (limited to 'main.ts')
-rw-r--r--main.ts46
1 files changed, 16 insertions, 30 deletions
diff --git a/main.ts b/main.ts
index 28c1ecdff..4767f51ec 100644
--- a/main.ts
+++ b/main.ts
@@ -1,47 +1,33 @@
+import * as dispatch from "./dispatch";
import { main as pb } from "./msg.pb";
-import "./util";
+
import * as runtime from "./runtime";
-import * as timers from "./timers";
import * as util from "./util";
+// These have top-level functions that need to execute.
+import { initTimers } from "./timers";
+
// To control internal logging output
// Set with the -debug command-line flag.
export let debug = false;
+let startCalled = false;
+
+dispatch.sub("start", (payload: Uint8Array) => {
+ if (startCalled) {
+ throw Error("start message received more than once!");
+ }
+ startCalled = true;
+
+ const msg = pb.Msg.decode(payload);
+ const { cwd, argv, debugFlag, mainJs, mainMap } = msg.start;
-function start(
- cwd: string,
- argv: string[],
- debugFlag: boolean,
- mainJs: string,
- mainMap: string
-): void {
debug = debugFlag;
util.log("start", { cwd, argv, debugFlag });
+ initTimers();
runtime.setup(mainJs, mainMap);
const inputFn = argv[0];
const mod = runtime.resolveModule(inputFn, cwd + "/");
mod.compileAndRun();
-}
-
-V8Worker2.recv((ab: ArrayBuffer) => {
- const msg = pb.Msg.decode(new Uint8Array(ab));
- switch (msg.payload) {
- case "start":
- start(
- msg.start.cwd,
- msg.start.argv,
- msg.start.debugFlag,
- msg.start.mainJs,
- msg.start.mainMap
- );
- break;
- case "timerReady":
- timers.timerReady(msg.timerReady.id, msg.timerReady.done);
- break;
- default:
- console.log("Unknown message", msg);
- break;
- }
});