mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 10:09:20 -05:00
9778eceaf5
- Port protobuf messages to flatbuffers. - Demo linking to rust from C++ executable. - Start using the prototype TS libraries.
30 lines
871 B
TypeScript
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);
|
|
}
|
|
});
|