diff options
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | js/dispatch.ts | 29 | ||||
-rw-r--r-- | js/main.ts | 10 | ||||
-rw-r--r-- | js/util.ts | 8 |
4 files changed, 9 insertions, 39 deletions
@@ -206,7 +206,6 @@ run_node("bundle") { "js/assets.ts", "js/console.ts", "js/deno.d.ts", - "js/dispatch.ts", "js/globals.ts", "js/lib.deno.d.ts", "js/main.ts", diff --git a/js/dispatch.ts b/js/dispatch.ts deleted file mode 100644 index eeb00f38e..000000000 --- a/js/dispatch.ts +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. -import { typedArrayToArrayBuffer } from "./util"; -import { deno as fbs } from "./msg_generated"; - -export type MessageCallback = (msg: Uint8Array) => void; -//type MessageStructCallback = (msg: pb.IMsg) => void; - -const channels = new Map<string, MessageCallback[]>(); - -export function sub(channel: string, cb: MessageCallback): void { - let subscribers = channels.get(channel); - if (!subscribers) { - subscribers = []; - channels.set(channel, subscribers); - } - subscribers.push(cb); -} - -deno.recv((channel: string, ab: ArrayBuffer) => { - const subscribers = channels.get(channel); - if (subscribers == null) { - throw Error(`No subscribers for channel "${channel}".`); - } - - const ui8 = new Uint8Array(ab); - for (const subscriber of subscribers) { - subscriber(ui8); - } -}); diff --git a/js/main.ts b/js/main.ts index f5fa0b6ac..50f73a4c6 100644 --- a/js/main.ts +++ b/js/main.ts @@ -3,17 +3,9 @@ /// <reference path="deno.d.ts" /> import { flatbuffers } from "flatbuffers"; import { deno as fbs } from "gen/msg_generated"; -import { assert, log } from "./util"; +import { assert, log, assignCmdId } from "./util"; import * as runtime from "./runtime"; -let cmdIdCounter = 0; -function assignCmdId(): number { - // TODO(piscisaureus) Safely re-use so they don't overflow. - const cmdId = ++cmdIdCounter; - assert(cmdId < 2 ** 32, "cmdId overflow"); - return cmdId; -} - function startMsg(cmdId: number): Uint8Array { const builder = new flatbuffers.Builder(); fbs.Start.startStart(builder); diff --git a/js/util.ts b/js/util.ts index 09d74aeb0..d4563936f 100644 --- a/js/util.ts +++ b/js/util.ts @@ -20,6 +20,14 @@ export function assert(cond: boolean, msg = "assert") { } } +let cmdIdCounter = 0; +export function assignCmdId(): number { + // TODO(piscisaureus) Safely re-use so they don't overflow. + const cmdId = ++cmdIdCounter; + assert(cmdId < 2 ** 32, "cmdId overflow"); + return cmdId; +} + export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer { const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength); return ab as ArrayBuffer; |