0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Remove dispatch.ts and move assignCmdId to util.ts

This commit is contained in:
Ryan Dahl 2018-08-01 12:36:17 -04:00
parent df8208557d
commit 421358e7a9
4 changed files with 9 additions and 39 deletions

View file

@ -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",

View file

@ -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);
}
});

View file

@ -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);

View file

@ -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;