diff options
| author | Ryan Dahl <ry@tinyclouds.org> | 2018-05-21 22:07:40 -0400 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2018-05-21 22:07:40 -0400 |
| commit | 08307fb84160602da12ba4d1b118c859e8a73cdb (patch) | |
| tree | b8f3bf7c4df3904937afc964ae36803567e78719 /os.ts | |
| parent | 9a6621659937c55c6005a1fa6ce9641a4ceff385 (diff) | |
Add dispatch pub/sub
Diffstat (limited to 'os.ts')
| -rw-r--r-- | os.ts | 32 |
1 files changed, 4 insertions, 28 deletions
@@ -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); - } } |
