|
|
@@ -5,45 +5,74 @@ import {
|
|
5
|
5
|
} from './constants';
|
|
6
|
6
|
|
|
7
|
7
|
/**
|
|
8
|
|
- * Stores the currnet transport that have to be used.
|
|
|
8
|
+ * Stores the currnet transport backend that have to be used. Also implements
|
|
|
9
|
+ * request/response mechanism.
|
|
9
|
10
|
*/
|
|
10
|
11
|
export default class Transport {
|
|
11
|
12
|
/**
|
|
12
|
13
|
* Creates new instance.
|
|
13
|
14
|
*
|
|
14
|
15
|
* @param {Object} options - Optional parameters for configuration of the
|
|
15
|
|
- * transport.
|
|
|
16
|
+ * transport backend.
|
|
16
|
17
|
*/
|
|
17
|
|
- constructor({ transport } = {}) {
|
|
|
18
|
+ constructor({ backend } = {}) {
|
|
|
19
|
+ /**
|
|
|
20
|
+ * The request ID counter used for the id property of the request. This
|
|
|
21
|
+ * property is used to match the responses with the request.
|
|
|
22
|
+ *
|
|
|
23
|
+ * @type {number}
|
|
|
24
|
+ */
|
|
18
|
25
|
this._requestID = 0;
|
|
19
|
26
|
|
|
|
27
|
+ /**
|
|
|
28
|
+ * Maps an IDs of the requests and handlers that will process the
|
|
|
29
|
+ * responses of those requests.
|
|
|
30
|
+ *
|
|
|
31
|
+ * @type {Map<number, Function>}
|
|
|
32
|
+ */
|
|
20
|
33
|
this._responseHandlers = new Map();
|
|
21
|
34
|
|
|
|
35
|
+ /**
|
|
|
36
|
+ * Maps an event name and listener that have been added to the Transport
|
|
|
37
|
+ * instance.
|
|
|
38
|
+ *
|
|
|
39
|
+ * @type {Map<string, Function>}
|
|
|
40
|
+ */
|
|
22
|
41
|
this._listeners = new Map();
|
|
23
|
42
|
|
|
|
43
|
+ /**
|
|
|
44
|
+ * A set with the events and requests that were received but not
|
|
|
45
|
+ * processed by any listener. They are later passed on every new
|
|
|
46
|
+ * listener until they are processed.
|
|
|
47
|
+ *
|
|
|
48
|
+ * @type {Set<Object>}
|
|
|
49
|
+ */
|
|
24
|
50
|
this._unprocessedMessages = new Set();
|
|
25
|
51
|
|
|
|
52
|
+ /**
|
|
|
53
|
+ * Alias.
|
|
|
54
|
+ */
|
|
26
|
55
|
this.addListener = this.on;
|
|
27
|
56
|
|
|
28
|
|
- if (transport) {
|
|
29
|
|
- this.setTransport(transport);
|
|
|
57
|
+ if (backend) {
|
|
|
58
|
+ this.setBackend(backend);
|
|
30
|
59
|
}
|
|
31
|
60
|
}
|
|
32
|
61
|
|
|
33
|
62
|
/**
|
|
34
|
|
- * Disposes the current transport.
|
|
|
63
|
+ * Disposes the current transport backend.
|
|
35
|
64
|
*
|
|
36
|
65
|
* @returns {void}
|
|
37
|
66
|
*/
|
|
38
|
|
- _disposeTransport() {
|
|
39
|
|
- if (this._transport) {
|
|
40
|
|
- this._transport.dispose();
|
|
41
|
|
- this._transport = null;
|
|
|
67
|
+ _disposeBackend() {
|
|
|
68
|
+ if (this._backend) {
|
|
|
69
|
+ this._backend.dispose();
|
|
|
70
|
+ this._backend = null;
|
|
42
|
71
|
}
|
|
43
|
72
|
}
|
|
44
|
73
|
|
|
45
|
74
|
/**
|
|
46
|
|
- * Handles incomming data from the transport.
|
|
|
75
|
+ * Handles incoming data from the transport backend.
|
|
47
|
76
|
*
|
|
48
|
77
|
* @param {Object} data - The data.
|
|
49
|
78
|
* @returns {void}
|
|
|
@@ -56,13 +85,9 @@ export default class Transport {
|
|
56
|
85
|
handler(data);
|
|
57
|
86
|
this._responseHandlers.delete(data.id);
|
|
58
|
87
|
}
|
|
59
|
|
-
|
|
60
|
|
- return;
|
|
61
|
|
- }
|
|
62
|
|
-
|
|
63
|
|
- if (data.type === MESSAGE_TYPE_REQUEST) {
|
|
|
88
|
+ } else if (data.type === MESSAGE_TYPE_REQUEST) {
|
|
64
|
89
|
this.emit('request', data.data, (result, error) => {
|
|
65
|
|
- this._transport.send({
|
|
|
90
|
+ this._backend.send({
|
|
66
|
91
|
type: MESSAGE_TYPE_RESPONSE,
|
|
67
|
92
|
error,
|
|
68
|
93
|
id: data.id,
|
|
|
@@ -83,7 +108,7 @@ export default class Transport {
|
|
83
|
108
|
this._responseHandlers.clear();
|
|
84
|
109
|
this._unprocessedMessages.clear();
|
|
85
|
110
|
this.removeAllListeners();
|
|
86
|
|
- this._disposeTransport();
|
|
|
111
|
+ this._disposeBackend();
|
|
87
|
112
|
}
|
|
88
|
113
|
|
|
89
|
114
|
/**
|
|
|
@@ -91,7 +116,8 @@ export default class Transport {
|
|
91
|
116
|
* the order they were registered, passing the supplied arguments to each.
|
|
92
|
117
|
*
|
|
93
|
118
|
* @param {string} eventName - The name of the event.
|
|
94
|
|
- * @returns {boolean} True if the event had listeners, false otherwise.
|
|
|
119
|
+ * @returns {boolean} True if the event has been processed by any listener,
|
|
|
120
|
+ * false otherwise.
|
|
95
|
121
|
*/
|
|
96
|
122
|
emit(eventName, ...args) {
|
|
97
|
123
|
const listenersForEvent = this._listeners.get(eventName);
|
|
|
@@ -141,7 +167,8 @@ export default class Transport {
|
|
141
|
167
|
/**
|
|
142
|
168
|
* Removes all listeners, or those of the specified eventName.
|
|
143
|
169
|
*
|
|
144
|
|
- * @param {string} eventName - The name of the event.
|
|
|
170
|
+ * @param {string} [eventName] - The name of the event. If this parameter is
|
|
|
171
|
+ * not specified all listeners will be removed.
|
|
145
|
172
|
* @returns {Transport} References to the instance of Transport class, so
|
|
146
|
173
|
* that calls can be chained.
|
|
147
|
174
|
*/
|
|
|
@@ -181,8 +208,8 @@ export default class Transport {
|
|
181
|
208
|
* @returns {void}
|
|
182
|
209
|
*/
|
|
183
|
210
|
sendEvent(data = {}) {
|
|
184
|
|
- if (this._transport) {
|
|
185
|
|
- this._transport.send({
|
|
|
211
|
+ if (this._backend) {
|
|
|
212
|
+ this._backend.send({
|
|
186
|
213
|
type: MESSAGE_TYPE_EVENT,
|
|
187
|
214
|
data
|
|
188
|
215
|
});
|
|
|
@@ -196,8 +223,8 @@ export default class Transport {
|
|
196
|
223
|
* @returns {Promise}
|
|
197
|
224
|
*/
|
|
198
|
225
|
sendRequest(data) {
|
|
199
|
|
- if (!this._transport) {
|
|
200
|
|
- return Promise.reject(new Error('No transport defined!'));
|
|
|
226
|
+ if (!this._backend) {
|
|
|
227
|
+ return Promise.reject(new Error('No transport backend defined!'));
|
|
201
|
228
|
}
|
|
202
|
229
|
|
|
203
|
230
|
this._requestID++;
|
|
|
@@ -215,7 +242,7 @@ export default class Transport {
|
|
215
|
242
|
}
|
|
216
|
243
|
});
|
|
217
|
244
|
|
|
218
|
|
- this._transport.send({
|
|
|
245
|
+ this._backend.send({
|
|
219
|
246
|
type: MESSAGE_TYPE_REQUEST,
|
|
220
|
247
|
data,
|
|
221
|
248
|
id
|
|
|
@@ -224,15 +251,15 @@ export default class Transport {
|
|
224
|
251
|
}
|
|
225
|
252
|
|
|
226
|
253
|
/**
|
|
227
|
|
- * Changes the current transport.
|
|
|
254
|
+ * Changes the current backend transport.
|
|
228
|
255
|
*
|
|
229
|
|
- * @param {Object} transport - The new transport that will be used.
|
|
|
256
|
+ * @param {Object} backend - The new transport backend that will be used.
|
|
230
|
257
|
* @returns {void}
|
|
231
|
258
|
*/
|
|
232
|
|
- setTransport(transport) {
|
|
233
|
|
- this._disposeTransport();
|
|
|
259
|
+ setBackend(backend) {
|
|
|
260
|
+ this._disposeBackend();
|
|
234
|
261
|
|
|
235
|
|
- this._transport = transport;
|
|
236
|
|
- this._transport.setReceiveCallback(this._onDataReceived.bind(this));
|
|
|
262
|
+ this._backend = backend;
|
|
|
263
|
+ this._backend.setReceiveCallback(this._onDataReceived.bind(this));
|
|
237
|
264
|
}
|
|
238
|
265
|
}
|