summaryrefslogtreecommitdiff
path: root/os.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 /os.ts
parent9a6621659937c55c6005a1fa6ce9641a4ceff385 (diff)
Add dispatch pub/sub
Diffstat (limited to 'os.ts')
-rw-r--r--os.ts32
1 files changed, 4 insertions, 28 deletions
diff --git a/os.ts b/os.ts
index 82baac25d..b2f9e93ba 100644
--- a/os.ts
+++ b/os.ts
@@ -1,18 +1,15 @@
-import { main as pb } from "./msg.pb";
import { ModuleInfo } from "./types";
-import { typedArrayToArrayBuffer } from "./util";
+import { sendMsgFromObject } from "./dispatch";
export function exit(code = 0): void {
- sendMsgFromObject({
- exit: { code }
- });
+ sendMsgFromObject("os", { exit: { code } });
}
export function sourceCodeFetch(
moduleSpecifier: string,
containingFile: string
): ModuleInfo {
- const res = sendMsgFromObject({
+ const res = sendMsgFromObject("os", {
sourceCodeFetch: { moduleSpecifier, containingFile }
});
return res.sourceCodeFetchRes;
@@ -23,28 +20,7 @@ export function sourceCodeCache(
sourceCode: string,
outputCode: string
): void {
- const res = sendMsgFromObject({
+ sendMsgFromObject("os", {
sourceCodeCache: { filename, sourceCode, outputCode }
});
- throwOnError(res);
-}
-
-export function sendMsgFromObject(obj: pb.IMsg): null | pb.Msg {
- const msg = pb.Msg.fromObject(obj);
- const ui8 = pb.Msg.encode(msg).finish();
- const ab = typedArrayToArrayBuffer(ui8);
- const resBuf = V8Worker2.send(ab);
- if (resBuf != null && resBuf.byteLength > 0) {
- const res = pb.Msg.decode(new Uint8Array(resBuf));
- throwOnError(res);
- return res;
- } else {
- return null;
- }
-}
-
-function throwOnError(res: pb.Msg) {
- if (res != null && res.error != null && res.error.length > 0) {
- throw Error(res.error);
- }
}