RpcProvider.js
6.73 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var microevent_ts_1 = require("microevent.ts");
var MSG_RESOLVE_TRANSACTION = "resolve_transaction", MSG_REJECT_TRANSACTION = "reject_transaction", MSG_ERROR = "error";
var RpcProvider = /** @class */ (function () {
function RpcProvider(_dispatch, _rpcTimeout) {
if (_rpcTimeout === void 0) { _rpcTimeout = 0; }
this._dispatch = _dispatch;
this._rpcTimeout = _rpcTimeout;
this.error = new microevent_ts_1.Event();
this._rpcHandlers = {};
this._signalHandlers = {};
this._pendingTransactions = {};
this._nextTransactionId = 0;
}
RpcProvider.prototype.dispatch = function (payload) {
var message = payload;
switch (message.type) {
case RpcProvider.MessageType.signal:
return this._handleSignal(message);
case RpcProvider.MessageType.rpc:
return this._handeRpc(message);
case RpcProvider.MessageType.internal:
return this._handleInternal(message);
default:
this._raiseError("invalid message type " + message.type);
}
};
RpcProvider.prototype.rpc = function (id, payload, transfer) {
var _this = this;
var transactionId = this._nextTransactionId++;
this._dispatch({
type: RpcProvider.MessageType.rpc,
transactionId: transactionId,
id: id,
payload: payload
}, transfer ? transfer : undefined);
return new Promise(function (resolve, reject) {
var transaction = _this._pendingTransactions[transactionId] = {
id: transactionId,
resolve: resolve,
reject: reject
};
if (_this._rpcTimeout > 0) {
_this._pendingTransactions[transactionId].timeoutHandle =
setTimeout(function () { return _this._transactionTimeout(transaction); }, _this._rpcTimeout);
}
});
};
;
RpcProvider.prototype.signal = function (id, payload, transfer) {
this._dispatch({
type: RpcProvider.MessageType.signal,
id: id,
payload: payload,
}, transfer ? transfer : undefined);
return this;
};
RpcProvider.prototype.registerRpcHandler = function (id, handler) {
if (this._rpcHandlers[id]) {
throw new Error("rpc handler for " + id + " already registered");
}
this._rpcHandlers[id] = handler;
return this;
};
;
RpcProvider.prototype.registerSignalHandler = function (id, handler) {
if (!this._signalHandlers[id]) {
this._signalHandlers[id] = [];
}
this._signalHandlers[id].push(handler);
return this;
};
RpcProvider.prototype.deregisterRpcHandler = function (id, handler) {
if (this._rpcHandlers[id]) {
delete this._rpcHandlers[id];
}
return this;
};
;
RpcProvider.prototype.deregisterSignalHandler = function (id, handler) {
if (this._signalHandlers[id]) {
this._signalHandlers[id] = this._signalHandlers[id].filter(function (h) { return handler !== h; });
}
return this;
};
RpcProvider.prototype._raiseError = function (error) {
this.error.dispatch(new Error(error));
this._dispatch({
type: RpcProvider.MessageType.internal,
id: MSG_ERROR,
payload: error
});
};
RpcProvider.prototype._handleSignal = function (message) {
if (!this._signalHandlers[message.id]) {
return this._raiseError("invalid signal " + message.id);
}
this._signalHandlers[message.id].forEach(function (handler) { return handler(message.payload); });
};
RpcProvider.prototype._handeRpc = function (message) {
var _this = this;
if (!this._rpcHandlers[message.id]) {
return this._raiseError("invalid rpc " + message.id);
}
Promise.resolve(this._rpcHandlers[message.id](message.payload))
.then(function (result) { return _this._dispatch({
type: RpcProvider.MessageType.internal,
id: MSG_RESOLVE_TRANSACTION,
transactionId: message.transactionId,
payload: result
}); }, function (reason) { return _this._dispatch({
type: RpcProvider.MessageType.internal,
id: MSG_REJECT_TRANSACTION,
transactionId: message.transactionId,
payload: reason
}); });
};
RpcProvider.prototype._handleInternal = function (message) {
switch (message.id) {
case MSG_RESOLVE_TRANSACTION:
if (!this._pendingTransactions[message.transactionId]) {
return this._raiseError("no pending transaction with id " + message.transactionId);
}
this._pendingTransactions[message.transactionId].resolve(message.payload);
this._clearTransaction(this._pendingTransactions[message.transactionId]);
break;
case MSG_REJECT_TRANSACTION:
if (!this._pendingTransactions[message.transactionId]) {
return this._raiseError("no pending transaction with id " + message.transactionId);
}
this._pendingTransactions[message.transactionId].reject(message.payload);
this._clearTransaction(this._pendingTransactions[message.transactionId]);
break;
case MSG_ERROR:
this.error.dispatch(new Error("remote error: " + message.payload));
break;
default:
this._raiseError("unhandled internal message " + message.id);
break;
}
};
RpcProvider.prototype._transactionTimeout = function (transaction) {
transaction.reject('transaction timed out');
this._raiseError("transaction " + transaction.id + " timed out");
delete this._pendingTransactions[transaction.id];
return;
};
RpcProvider.prototype._clearTransaction = function (transaction) {
if (typeof (transaction.timeoutHandle) !== 'undefined') {
clearTimeout(transaction.timeoutHandle);
}
delete this._pendingTransactions[transaction.id];
};
return RpcProvider;
}());
(function (RpcProvider) {
var MessageType;
(function (MessageType) {
MessageType[MessageType["signal"] = 0] = "signal";
MessageType[MessageType["rpc"] = 1] = "rpc";
MessageType[MessageType["internal"] = 2] = "internal";
})(MessageType = RpcProvider.MessageType || (RpcProvider.MessageType = {}));
;
})(RpcProvider || (RpcProvider = {}));
exports.default = RpcProvider;