RpcProvider.ts
6.9 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {Event} from 'microevent.ts';
import RpcProviderInterface from './RpcProviderInterface';
const MSG_RESOLVE_TRANSACTION = "resolve_transaction",
MSG_REJECT_TRANSACTION = "reject_transaction",
MSG_ERROR = "error";
interface Transaction {
id: number;
timeoutHandle?: any;
resolve(result: any): void;
reject(error: string): void;
}
class RpcProvider implements RpcProviderInterface {
constructor(
private _dispatch: RpcProvider.Dispatcher,
private _rpcTimeout = 0
) {}
dispatch(payload: any): void {
const message = payload as RpcProvider.Message;
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}`);
}
}
rpc<T, U>(id: string, payload?: T, transfer?: any): Promise<U> {
const transactionId = this._nextTransactionId++;
this._dispatch({
type: RpcProvider.MessageType.rpc,
transactionId,
id,
payload
}, transfer ? transfer : undefined);
return new Promise(
(resolve, reject) => {
const transaction = this._pendingTransactions[transactionId] = {
id: transactionId,
resolve,
reject
};
if (this._rpcTimeout > 0) {
this._pendingTransactions[transactionId].timeoutHandle =
setTimeout(() => this._transactionTimeout(transaction), this._rpcTimeout);
}
}
);
};
signal<T>(id: string, payload?: T, transfer?: any): this {
this._dispatch({
type: RpcProvider.MessageType.signal,
id,
payload,
}, transfer ? transfer : undefined);
return this;
}
registerRpcHandler<T, U>(id: string, handler: RpcProviderInterface.RpcHandler<T, U>): this {
if (this._rpcHandlers[id]) {
throw new Error(`rpc handler for ${id} already registered`);
}
this._rpcHandlers[id] = handler;
return this;
};
registerSignalHandler<T>(id: string, handler: RpcProviderInterface.SignalHandler<T>): this {
if (!this._signalHandlers[id]) {
this._signalHandlers[id] = [];
}
this._signalHandlers[id].push(handler);
return this;
}
deregisterRpcHandler<T, U>(id: string, handler: RpcProviderInterface.RpcHandler<T, U>): this {
if (this._rpcHandlers[id]) {
delete this._rpcHandlers[id];
}
return this;
};
deregisterSignalHandler<T>(id: string, handler: RpcProviderInterface.SignalHandler<T>): this {
if (this._signalHandlers[id]) {
this._signalHandlers[id] = this._signalHandlers[id].filter(h => handler !== h);
}
return this;
}
private _raiseError(error: string): void {
this.error.dispatch(new Error(error));
this._dispatch({
type: RpcProvider.MessageType.internal,
id: MSG_ERROR,
payload: error
});
}
private _handleSignal(message: RpcProvider.Message): void {
if (!this._signalHandlers[message.id]) {
return this._raiseError(`invalid signal ${message.id}`);
}
this._signalHandlers[message.id].forEach(handler => handler(message.payload));
}
private _handeRpc(message: RpcProvider.Message): void {
if (!this._rpcHandlers[message.id]) {
return this._raiseError(`invalid rpc ${message.id}`);
}
Promise.resolve(this._rpcHandlers[message.id](message.payload))
.then(
(result: any) => this._dispatch({
type: RpcProvider.MessageType.internal,
id: MSG_RESOLVE_TRANSACTION,
transactionId: message.transactionId,
payload: result
}),
(reason: string) => this._dispatch({
type: RpcProvider.MessageType.internal,
id: MSG_REJECT_TRANSACTION,
transactionId: message.transactionId,
payload: reason
})
);
}
private _handleInternal(message: RpcProvider.Message): void {
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;
}
}
private _transactionTimeout(transaction: Transaction): void {
transaction.reject('transaction timed out');
this._raiseError(`transaction ${transaction.id} timed out`);
delete this._pendingTransactions[transaction.id];
return;
}
private _clearTransaction(transaction: Transaction): void {
if (typeof(transaction.timeoutHandle) !== 'undefined') {
clearTimeout(transaction.timeoutHandle);
}
delete this._pendingTransactions[transaction.id];
}
error = new Event<Error>();
private _rpcHandlers: {[id: string]: RpcProviderInterface.RpcHandler<any, any>} = {};
private _signalHandlers: {[id: string]: Array<RpcProviderInterface.SignalHandler<any>>} = {};
private _pendingTransactions: {[id: number]: Transaction} = {};
private _nextTransactionId = 0;
}
module RpcProvider {
export enum MessageType {
signal,
rpc,
internal
};
export interface Dispatcher {
(message: Message, transfer?: Array<any>): void;
}
export interface Message {
type: MessageType;
transactionId?: number;
id: string;
payload?: any;
}
}
export default RpcProvider;