summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-08-01 12:36:17 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-08-02 12:49:40 -0400
commit421358e7a9612527fdd9ed9a9a59635c12cdaab5 (patch)
tree0f4d4ca9526745512c7a95a5ea27030a5080e98c
parentdf8208557d684938e5e50aadbd41b9bcbc37d072 (diff)
Remove dispatch.ts and move assignCmdId to util.ts
-rw-r--r--BUILD.gn1
-rw-r--r--js/dispatch.ts29
-rw-r--r--js/main.ts10
-rw-r--r--js/util.ts8
4 files changed, 9 insertions, 39 deletions
diff --git a/BUILD.gn b/BUILD.gn
index 481999a37..95c777f7f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -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;