-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertx-eventbus.js
296 lines (260 loc) · 8.19 KB
/
vertx-eventbus.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (c) 2011-2015 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
!function (factory) {
if (typeof require === 'function' && typeof module !== 'undefined') {
// CommonJS loader
var SockJS = require('sockjs-client');
if(!SockJS) {
throw new Error('vertx-eventbus.js requires sockjs-client, see http://sockjs.org');
}
factory(SockJS);
} else if (typeof define === 'function' && define.amd) {
// AMD loader
define('vertx-eventbus', ['sockjs'], factory);
} else {
// plain old include
if (typeof this.SockJS === 'undefined') {
throw new Error('vertx-eventbus.js requires sockjs-client, see http://sockjs.org');
}
EventBus = factory(this.SockJS);
}
}(function (SockJS) {
function makeUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (a, b) {
return b = Math.random() * 16, (a == 'y' ? b & 3 | 8 : b | 0).toString(16);
});
}
function mergeHeaders(defaultHeaders, headers) {
if (defaultHeaders) {
if(!headers) {
return defaultHeaders;
}
for (var headerName in defaultHeaders) {
if (defaultHeaders.hasOwnProperty(headerName)) {
// user can overwrite the default headers
if (typeof headers[headerName] === 'undefined') {
headers[headerName] = defaultHeaders[headerName];
}
}
}
}
// headers are required to be a object
return headers || {};
}
/**
* EventBus
*
* @param url
* @param options
* @constructor
*/
var EventBus = function (url, options) {
var self = this;
options = options || {};
var pingInterval = options.vertxbus_ping_interval || 5000;
var pingTimerID;
// attributes
this.sockJSConn = new SockJS(url, null, options);
this.state = EventBus.CONNECTING;
this.handlers = {};
this.replyHandlers = {};
this.defaultHeaders = null;
// default event handlers
this.onerror = console.error;
var sendPing = function () {
self.sockJSConn.send(JSON.stringify({type: 'ping'}));
};
this.sockJSConn.onopen = function () {
// Send the first ping then send a ping every pingInterval milliseconds
sendPing();
pingTimerID = setInterval(sendPing, pingInterval);
self.state = EventBus.OPEN;
self.onopen && self.onopen();
};
this.sockJSConn.onclose = function () {
self.state = EventBus.CLOSED;
if (pingTimerID) clearInterval(pingTimerID);
self.onclose && self.onclose();
};
this.sockJSConn.onmessage = function (e) {
var json = JSON.parse(e.data);
// define a reply function on the message itself
if (json.replyAddress) {
Object.defineProperty(json, 'reply', {
value: function (message, headers, callback) {
self.send(json.replyAddress, message, headers, callback);
}
});
}
if (self.handlers[json.address]) {
// iterate all registered handlers
var handlers = self.handlers[json.address];
for (var i = 0; i < handlers.length; i++) {
if (json.type === 'err') {
handlers[i]({failureCode: json.failureCode, failureType: json.failureType, message: json.message});
} else {
handlers[i](null, json);
}
}
} else if (self.replyHandlers[json.address]) {
// Might be a reply message
var handler = self.replyHandlers[json.address];
delete self.replyHandlers[json.address];
if (json.type === 'err') {
handler({failureCode: json.failureCode, failureType: json.failureType, message: json.message});
} else {
handler(null, json);
}
} else {
if (json.type === 'err') {
self.onerror(json);
} else {
console.warn('No handler found for message: ', json);
}
}
}
};
/**
* Send a message
*
* @param {String} address
* @param {Object} message
* @param {Object} [headers]
* @param {Function} [callback]
*/
EventBus.prototype.send = function (address, message, headers, callback) {
// are we ready?
if (this.state != EventBus.OPEN) {
throw new Error('INVALID_STATE_ERR');
}
if (typeof headers === 'function') {
callback = headers;
headers = {};
}
var envelope = {
type: 'send',
address: address,
headers: mergeHeaders(this.defaultHeaders, headers),
body: message
};
if (callback) {
var replyAddress = makeUUID();
envelope.replyAddress = replyAddress;
this.replyHandlers[replyAddress] = callback;
}
this.sockJSConn.send(JSON.stringify(envelope));
};
/**
* Publish a message
*
* @param {String} address
* @param {Object} message
* @param {Object} [headers]
*/
EventBus.prototype.publish = function (address, message, headers) {
// are we ready?
if (this.state != EventBus.OPEN) {
throw new Error('INVALID_STATE_ERR');
}
this.sockJSConn.send(JSON.stringify({
type: 'publish',
address: address,
headers: mergeHeaders(this.defaultHeaders, headers),
body: message
}));
};
/**
* Register a new handler
*
* @param {String} address
* @param {Object} [headers]
* @param {Function} callback
*/
EventBus.prototype.registerHandler = function (address, headers, callback) {
// are we ready?
if (this.state != EventBus.OPEN) {
throw new Error('INVALID_STATE_ERR');
}
if (typeof headers === 'function') {
callback = headers;
headers = {};
}
// ensure it is an array
if (!this.handlers[address]) {
this.handlers[address] = [];
// First handler for this address so we should register the connection
this.sockJSConn.send(JSON.stringify({
type: 'register',
address: address,
headers: mergeHeaders(this.defaultHeaders, headers)
}));
}
this.handlers[address].push(callback);
};
/**
* Unregister a handler
*
* @param {String} address
* @param {Object} [headers]
* @param {Function} callback
*/
EventBus.prototype.unregisterHandler = function (address, headers, callback) {
// are we ready?
if (this.state != EventBus.OPEN) {
throw new Error('INVALID_STATE_ERR');
}
var handlers = this.handlers[address];
if (handlers) {
if (typeof headers === 'function') {
callback = headers;
headers = {};
}
var idx = handlers.indexOf(callback);
if (idx != -1) {
handlers.splice(idx, 1);
if (handlers.length === 0) {
// No more local handlers so we should unregister the connection
this.sockJSConn.send(JSON.stringify({
type: 'unregister',
address: address,
headers: mergeHeaders(this.defaultHeaders, headers)
}));
delete this.handlers[address];
}
}
}
};
/**
* Closes the connection to the EvenBus Bridge.
*/
EventBus.prototype.close = function () {
this.state = vertx.EventBus.CLOSING;
this.sockJSConn.close();
};
EventBus.CONNECTING = 0;
EventBus.OPEN = 1;
EventBus.CLOSING = 2;
EventBus.CLOSED = 3;
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = EventBus;
} else {
exports.EventBus = EventBus;
}
} else {
return EventBus;
}
});