1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Promise = require("bluebird");
|
4 | class PromiseCallback {
|
5 | constructor(resolve, reject) {
|
6 | this.resolve = resolve;
|
7 | this.reject = reject;
|
8 | }
|
9 | }
|
10 | class JsonRpc {
|
11 | constructor(transport, domains = null) {
|
12 | this.transport = transport;
|
13 | this.messageIdCounter = 0;
|
14 | this.callbacks = new Map();
|
15 | this.domains = new Map();
|
16 | this.domains = new Map();
|
17 | if (domains != null) {
|
18 | for (let name of Object.getOwnPropertyNames(domains)) {
|
19 | this.domains.set(name, domains[name]);
|
20 | }
|
21 | }
|
22 | transport.messageReceived = this.messageReceived.bind(this);
|
23 | }
|
24 | registerDomain(name, commands) {
|
25 | if (this.domains.has(name)) {
|
26 | throw Error("Domain " + name + " is already registered");
|
27 | }
|
28 | this.domains.set(name, commands);
|
29 | }
|
30 | send(domain, command, ...params) {
|
31 | this.transport.send(-1, domain, command, params);
|
32 | }
|
33 | call(domain, command, ...params) {
|
34 | return new Promise((resolve, reject) => {
|
35 | const id = this.messageIdCounter++;
|
36 | this.callbacks.set(id, new PromiseCallback(resolve, reject));
|
37 | this.transport.send(id, domain, command, params);
|
38 | });
|
39 | }
|
40 | messageReceived(message) {
|
41 | if (message.length === 1 || (message.length === 2 && !(typeof message[1] === "string"))) {
|
42 | const promiseCallback = this.callbacks.get(message[0]);
|
43 | const singletonArray = safeGet(message, 1);
|
44 | if (singletonArray == null) {
|
45 | promiseCallback.resolve();
|
46 | }
|
47 | else {
|
48 | promiseCallback.resolve(singletonArray[0]);
|
49 | }
|
50 | }
|
51 | else {
|
52 | let id;
|
53 | let offset;
|
54 | if (typeof message[0] === "string") {
|
55 | id = -1;
|
56 | offset = 0;
|
57 | }
|
58 | else {
|
59 | id = message[0];
|
60 | offset = 1;
|
61 | }
|
62 | const domainName = message[offset];
|
63 | if (domainName === "e") {
|
64 | console.assert(id != -1);
|
65 | console.assert(message.length === 3);
|
66 | this.callbacks.get(id).reject(message[2]);
|
67 | return;
|
68 | }
|
69 | const onRejected = id === -1 ? null : (error) => this.transport.sendError(id, error);
|
70 | try {
|
71 | const object = this.domains.get(domainName);
|
72 | if (object == null) {
|
73 | const e = "Cannot find domain " + domainName;
|
74 | console.warn(e);
|
75 | if (onRejected != null) {
|
76 | onRejected(e);
|
77 | }
|
78 | return;
|
79 | }
|
80 | const method = object[message[offset + 1]];
|
81 | const args = safeGet(message, offset + 2);
|
82 | const result = (args === null) ? method.call(object) : method.apply(object, args);
|
83 | if (id !== -1) {
|
84 | const onFulfilled = (result) => this.transport.sendResult(id, result);
|
85 | if (result instanceof Promise) {
|
86 | result.done(onFulfilled, onRejected);
|
87 | }
|
88 | else {
|
89 | onFulfilled(result);
|
90 | }
|
91 | }
|
92 | }
|
93 | catch (e) {
|
94 | console.error(e);
|
95 | if (onRejected != null) {
|
96 | onRejected(e);
|
97 | }
|
98 | }
|
99 | }
|
100 | }
|
101 | }
|
102 | exports.JsonRpc = JsonRpc;
|
103 | function safeGet(a, index) {
|
104 | return index < a.length ? a[index] : null;
|
105 | }
|
106 |
|
\ | No newline at end of file |