1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00
denoland-deno/js/dispatch.ts
Ryan Dahl 9778eceaf5 Use C++ to do flatbuffer parsing.
- Port protobuf messages to flatbuffers.
- Demo linking to rust from C++ executable.
- Start using the prototype TS libraries.
2018-07-06 17:33:06 -04:00

30 lines
871 B
TypeScript

// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// 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);
}
});