summaryrefslogtreecommitdiff
path: root/js/dispatch.ts
blob: a83e5a0e5332bc23e0ad758a7008c6d7604996e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
import { typedArrayToArrayBuffer } from "./util";
import { _global } from "./globals";
import { deno as pb } from "./msg.pb";

export type MessageCallback = (msg: Uint8Array) => void;
//type MessageStructCallback = (msg: pb.IMsg) => void;

const send = V8Worker2.send;
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);
}

/*
export function subMsg(channel: string, cb: MessageStructCallback): void {
  sub(channel, (payload: Uint8Array) => {
    const msg = pb.Msg.decode(payload);
    if (msg.error != null) {
      f.onError(new Error(msg.error));
    } else {
      cb(msg);
    }
  });
}
  */

export function pub(channel: string, payload: Uint8Array): null | ArrayBuffer {
  const msg = pb.BaseMsg.fromObject({ channel, payload });
  const ui8 = pb.BaseMsg.encode(msg).finish();
  const ab = typedArrayToArrayBuffer(ui8);
  return send(ab);
}

// Internal version of "pub".
// TODO add internal version of "sub"
export function pubInternal(channel: string, obj: pb.IMsg): null | pb.Msg {
  const msg = pb.Msg.fromObject(obj);
  const ui8 = pb.Msg.encode(msg).finish();
  const resBuf = pub(channel, ui8);
  if (resBuf != null && resBuf.byteLength > 0) {
    const res = pb.Msg.decode(new Uint8Array(resBuf));
    if (res != null && res.error != null && res.error.length > 0) {
      throw Error(res.error);
    }
    return res;
  } else {
    return null;
  }
}

V8Worker2.recv((ab: ArrayBuffer) => {
  const msg = pb.BaseMsg.decode(new Uint8Array(ab));
  const subscribers = channels.get(msg.channel);
  if (subscribers == null) {
    throw Error(`No subscribers for channel "${msg.channel}".`);
  }

  for (const subscriber of subscribers) {
    subscriber(msg.payload);
  }
});

// Delete the V8Worker2 from the global object, so that no one else can receive
// messages.
_global["V8Worker2"] = null;