RpcClient.js
3.44 KB
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
74
75
76
77
78
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const RpcMessage_1 = require("./RpcMessage");
const RpcRemoteError_1 = require("./error/RpcRemoteError");
/* eslint-enable @typescript-eslint/no-explicit-any */
function createRpcClient(port) {
let callIndex = 0;
const callbacks = new Map();
let isListenerRegistered = false;
const returnOrThrowListener = (message) => __awaiter(this, void 0, void 0, function* () {
if (RpcMessage_1.isRpcReturnMessage(message)) {
const key = RpcMessage_1.getRpcMessageKey(message);
const callback = callbacks.get(key);
if (callback) {
callback.return(message.payload);
callbacks.delete(key);
}
}
if (RpcMessage_1.isRpcThrowMessage(message)) {
const key = RpcMessage_1.getRpcMessageKey(message);
const callback = callbacks.get(key);
if (callback) {
callback.throw(new RpcRemoteError_1.RpcRemoteError(message.payload.message, message.payload.stack));
callbacks.delete(key);
}
}
});
const errorListener = (error) => __awaiter(this, void 0, void 0, function* () {
callbacks.forEach((callback, key) => {
callback.throw(error);
callbacks.delete(key);
});
});
return {
isConnected: () => port.isOpen() && isListenerRegistered,
connect: () => __awaiter(this, void 0, void 0, function* () {
if (!port.isOpen()) {
yield port.open();
}
if (!isListenerRegistered) {
port.addMessageListener(returnOrThrowListener);
port.addErrorListener(errorListener);
isListenerRegistered = true;
}
}),
disconnect: () => __awaiter(this, void 0, void 0, function* () {
if (isListenerRegistered) {
port.removeMessageListener(returnOrThrowListener);
port.removeErrorListener(errorListener);
isListenerRegistered = false;
}
if (port.isOpen()) {
yield port.close();
}
}),
dispatchCall: (procedure, payload) => __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const call = RpcMessage_1.createRpcCall(procedure, callIndex++, payload);
const key = RpcMessage_1.getRpcMessageKey(call);
callbacks.set(key, { return: resolve, throw: reject });
port.dispatchMessage(call).catch((error) => {
callbacks.delete(key);
reject(error);
});
});
}),
};
}
exports.createRpcClient = createRpcClient;