|
1 | | -/*! angular-vertxbus - v0.5.0 - 2014-03-17 |
| 1 | +/*! angular-vertxbus - v0.6.0 - 2014-05-19 |
2 | 2 | * http://github.com/knalli/angular-vertxbus |
3 | 3 | * Copyright (c) 2014 ; Licensed */ |
4 | 4 | (function () { |
|
12 | 12 | reconnectEnabled: true, |
13 | 13 | sockjsStateInterval: 10000, |
14 | 14 | sockjsReconnectInterval: 10000, |
15 | | - sockjsOptions: {} |
| 15 | + sockjsOptions: {}, |
| 16 | + messageBuffer: 0 |
16 | 17 | }; |
17 | 18 | /* |
18 | 19 | An AngularJS wrapper for projects using the VertX Event Bus |
|
29 | 30 | * sockjsReconnectInterval (default 10000 ms): defines the wait time for a reconnect after a disconnect has been recognized |
30 | 31 | * sockjsOptions (default {}): optional SockJS options (new SockJS(url, undefined, options)) |
31 | 32 | */ |
32 | | - module = angular.module('knalli.angular-vertxbus', ['ng']).constant('angularVertxbusOptions', DEFAULT_OPTIONS).provider('vertxEventBus', [ |
| 33 | + module = angular.module('knalli.angular-vertxbus', ['ng']).constant('angularVertxbusOptions', angular.extend({}, DEFAULT_OPTIONS)).provider('vertxEventBus', [ |
33 | 34 | 'angularVertxbusOptions', |
34 | 35 | function (angularVertxbusOptions) { |
35 | 36 | this.enable = function (value) { |
|
51 | 52 | value = DEFAULT_OPTIONS.prefix; |
52 | 53 | } |
53 | 54 | angularVertxbusOptions.prefix = value; |
| 55 | + return this; |
54 | 56 | }; |
55 | 57 | this.useUrlServer = function (value) { |
56 | 58 | if (value == null) { |
57 | 59 | value = DEFAULT_OPTIONS.urlServer; |
58 | 60 | } |
59 | 61 | angularVertxbusOptions.urlServer = value; |
| 62 | + return this; |
60 | 63 | }; |
61 | 64 | this.useUrlPath = function (value) { |
62 | 65 | if (value == null) { |
63 | 66 | value = DEFAULT_OPTIONS.urlPath; |
64 | 67 | } |
65 | 68 | angularVertxbusOptions.urlPath = value; |
| 69 | + return this; |
66 | 70 | }; |
67 | 71 | this.useReconnect = function (value) { |
68 | 72 | if (value == null) { |
69 | 73 | value = DEFAULT_OPTIONS.reconnectEnabled; |
70 | 74 | } |
71 | 75 | angularVertxbusOptions.reconnectEnabled = value; |
| 76 | + return this; |
72 | 77 | }; |
73 | 78 | this.useSockJsStateInterval = function (value) { |
74 | 79 | if (value == null) { |
75 | 80 | value = DEFAULT_OPTIONS.sockjsStateInterval; |
76 | 81 | } |
77 | 82 | angularVertxbusOptions.sockjsStateInterval = value; |
| 83 | + return this; |
78 | 84 | }; |
79 | 85 | this.useSockJsReconnectInterval = function (value) { |
80 | 86 | if (value == null) { |
81 | 87 | value = DEFAULT_OPTIONS.sockjsReconnectInterval; |
82 | 88 | } |
83 | 89 | angularVertxbusOptions.sockjsReconnectInterval = value; |
| 90 | + return this; |
84 | 91 | }; |
85 | 92 | this.useSockJsOptions = function (value) { |
86 | 93 | if (value == null) { |
87 | 94 | value = DEFAULT_OPTIONS.sockjsOptions; |
88 | 95 | } |
89 | 96 | angularVertxbusOptions.sockjsOptions = value; |
| 97 | + return this; |
| 98 | + }; |
| 99 | + this.useMessageBuffer = function (value) { |
| 100 | + if (value == null) { |
| 101 | + value = DEFAULT_OPTIONS.messageBuffer; |
| 102 | + } |
| 103 | + angularVertxbusOptions.messageBuffer = value; |
| 104 | + return this; |
90 | 105 | }; |
91 | 106 | /* |
92 | 107 | A stub representing the VertX Event Bus (core functionality) |
|
160 | 175 | return eventBus.publish(address, message); |
161 | 176 | }, |
162 | 177 | registerHandler: function (address, handler) { |
163 | | - return eventBus.registerHandler(address, handler); |
| 178 | + eventBus.registerHandler(address, handler); |
| 179 | + return function () { |
| 180 | + stub.unregisterHandler(address, handler); |
| 181 | + }; |
164 | 182 | }, |
165 | 183 | unregisterHandler: function (address, handler) { |
166 | 184 | return eventBus.unregisterHandler(address, handler); |
|
207 | 225 | 'vertxEventBus', |
208 | 226 | 'angularVertxbusOptions', |
209 | 227 | function ($rootScope, $q, $interval, $timeout, vertxEventBus, angularVertxbusOptions) { |
210 | | - var api, connectionState, debugEnabled, enabled, prefix, reconnectEnabled, sockjsOptions, sockjsReconnectInterval, sockjsStateInterval, urlPath, urlServer, util, wrapped, _ref, _ref1; |
211 | | - _ref = angular.extend({}, DEFAULT_OPTIONS, angularVertxbusOptions), enabled = _ref.enabled, debugEnabled = _ref.debugEnabled, prefix = _ref.prefix, urlServer = _ref.urlServer, urlPath = _ref.urlPath, reconnectEnabled = _ref.reconnectEnabled, sockjsStateInterval = _ref.sockjsStateInterval, sockjsReconnectInterval = _ref.sockjsReconnectInterval, sockjsOptions = _ref.sockjsOptions; |
| 228 | + var MessageQueueHolder, api, connectionState, debugEnabled, enabled, ensureOpenConnection, messageBuffer, messageQueueHolder, prefix, reconnectEnabled, sockjsOptions, sockjsReconnectInterval, sockjsStateInterval, urlPath, urlServer, util, wrapped, _ref, _ref1; |
| 229 | + MessageQueueHolder = function () { |
| 230 | + function MessageQueueHolder(maxSize) { |
| 231 | + this.maxSize = maxSize != null ? maxSize : 10; |
| 232 | + this.items = []; |
| 233 | + } |
| 234 | + MessageQueueHolder.prototype.push = function (item) { |
| 235 | + this.items.push(item); |
| 236 | + return this.recalibrateBufferSize(); |
| 237 | + }; |
| 238 | + MessageQueueHolder.prototype.recalibrateBufferSize = function () { |
| 239 | + while (this.items.length > this.maxSize) { |
| 240 | + this.first(); |
| 241 | + } |
| 242 | + return this; |
| 243 | + }; |
| 244 | + MessageQueueHolder.prototype.last = function () { |
| 245 | + return this.items.pop(); |
| 246 | + }; |
| 247 | + MessageQueueHolder.prototype.first = function () { |
| 248 | + return this.items.shift(0); |
| 249 | + }; |
| 250 | + MessageQueueHolder.prototype.size = function () { |
| 251 | + return this.items.length; |
| 252 | + }; |
| 253 | + return MessageQueueHolder; |
| 254 | + }(); |
| 255 | + _ref = angular.extend({}, DEFAULT_OPTIONS, angularVertxbusOptions), enabled = _ref.enabled, debugEnabled = _ref.debugEnabled, prefix = _ref.prefix, urlServer = _ref.urlServer, urlPath = _ref.urlPath, reconnectEnabled = _ref.reconnectEnabled, sockjsStateInterval = _ref.sockjsStateInterval, sockjsReconnectInterval = _ref.sockjsReconnectInterval, sockjsOptions = _ref.sockjsOptions, messageBuffer = _ref.messageBuffer; |
212 | 256 | connectionState = vertxEventBus != null ? (_ref1 = vertxEventBus.EventBus) != null ? _ref1.CLOSED : void 0 : void 0; |
| 257 | + messageQueueHolder = new MessageQueueHolder(messageBuffer); |
213 | 258 | if (enabled && vertxEventBus) { |
214 | 259 | vertxEventBus.onopen = function () { |
215 | | - var address, callback, callbacks, _i, _len, _ref2; |
| 260 | + var address, callback, callbacks, fn, _i, _len, _ref2; |
216 | 261 | wrapped.getConnectionState(true); |
217 | 262 | $rootScope.$broadcast('' + prefix + 'system.connected'); |
218 | 263 | _ref2 = wrapped.handlers; |
|
225 | 270 | util.registerHandler(address, callback); |
226 | 271 | } |
227 | 272 | } |
228 | | - return $rootScope.$digest(); |
| 273 | + $rootScope.$digest(); |
| 274 | + if (messageBuffer && messageQueueHolder.size()) { |
| 275 | + while (messageQueueHolder.size()) { |
| 276 | + fn = messageQueueHolder.first(); |
| 277 | + if (typeof fn === 'function') { |
| 278 | + fn(); |
| 279 | + } |
| 280 | + } |
| 281 | + $rootScope.$digest(); |
| 282 | + } |
229 | 283 | }; |
230 | 284 | vertxEventBus.onclose = function () { |
231 | 285 | wrapped.getConnectionState(true); |
232 | 286 | return $rootScope.$broadcast('' + prefix + 'system.disconnected'); |
233 | 287 | }; |
234 | 288 | } |
| 289 | + ensureOpenConnection = function (fn) { |
| 290 | + if (wrapped.getConnectionState() === vertxEventBus.EventBus.OPEN) { |
| 291 | + fn(); |
| 292 | + return true; |
| 293 | + } else if (messageBuffer) { |
| 294 | + messageQueueHolder.push(fn); |
| 295 | + return true; |
| 296 | + } |
| 297 | + return false; |
| 298 | + }; |
235 | 299 | util = { |
236 | 300 | registerHandler: function (address, callback) { |
237 | 301 | if (typeof callback !== 'function') { |
|
255 | 319 | return vertxEventBus.unregisterHandler(address, callback); |
256 | 320 | }, |
257 | 321 | send: function (address, message, expectReply, timeout) { |
258 | | - var deferred; |
| 322 | + var deferred, dispatched; |
259 | 323 | if (timeout == null) { |
260 | 324 | timeout = 10000; |
261 | 325 | } |
262 | 326 | if (expectReply) { |
263 | 327 | deferred = $q.defer(); |
264 | 328 | } |
265 | | - vertxEventBus.send(address, message, function (reply) { |
| 329 | + dispatched = ensureOpenConnection(function () { |
| 330 | + vertxEventBus.send(address, message, function (reply) { |
| 331 | + if (deferred) { |
| 332 | + deferred.resolve(reply); |
| 333 | + } |
| 334 | + if (typeof expectReply === 'function') { |
| 335 | + return expectReply(reply); |
| 336 | + } |
| 337 | + }); |
266 | 338 | if (deferred) { |
267 | | - deferred.resolve(reply); |
268 | | - } |
269 | | - if (typeof expectReply === 'function') { |
270 | | - return expectReply(reply); |
| 339 | + return $timeout(function () { |
| 340 | + return deferred.reject(); |
| 341 | + }, timeout); |
271 | 342 | } |
272 | 343 | }); |
273 | | - if (deferred) { |
274 | | - $timeout(function () { |
275 | | - return deferred.reject(); |
276 | | - }, timeout); |
| 344 | + if (deferred && !dispatched) { |
| 345 | + deferred.reject(); |
277 | 346 | } |
278 | 347 | return deferred != null ? deferred.promise : void 0; |
279 | 348 | }, |
280 | 349 | publish: function (address, message) { |
281 | | - return vertxEventBus.publish(address, message); |
| 350 | + var dispatched; |
| 351 | + dispatched = ensureOpenConnection(function () { |
| 352 | + return vertxEventBus.publish(address, message); |
| 353 | + }); |
| 354 | + return dispatched; |
282 | 355 | } |
283 | 356 | }; |
284 | 357 | wrapped = { |
|
289 | 362 | } |
290 | 363 | wrapped.handlers[address].push(callback); |
291 | 364 | if (connectionState === vertxEventBus.EventBus.OPEN) { |
292 | | - return util.registerHandler(address, callback); |
| 365 | + util.registerHandler(address, callback); |
293 | 366 | } |
| 367 | + return function () { |
| 368 | + wrapped.unregisterHandler(address, callback); |
| 369 | + }; |
294 | 370 | }, |
295 | 371 | unregisterHandler: function (address, callback) { |
296 | 372 | var index; |
297 | | - if (wrapped.handlers[address] && callback(wrapped.handlers[address])) { |
| 373 | + if (wrapped.handlers[address]) { |
298 | 374 | index = wrapped.handlers[address].indexOf(callback); |
299 | 375 | if (index > -1) { |
300 | 376 | wrapped.handlers[address].splice(index, 1); |
|
308 | 384 | if (timeout == null) { |
309 | 385 | timeout = 10000; |
310 | 386 | } |
311 | | - if (connectionState === vertxEventBus.EventBus.OPEN) { |
312 | | - return util.send(address, message, expectReply, timeout); |
313 | | - } else { |
314 | | - return $q.reject('unknown'); |
315 | | - } |
| 387 | + return util.send(address, message, expectReply, timeout); |
316 | 388 | }, |
317 | 389 | publish: function (address, message) { |
318 | | - if (connectionState === vertxEventBus.EventBus.OPEN) { |
319 | | - return util.publish(address, message); |
320 | | - } |
| 390 | + return util.publish(address, message); |
321 | 391 | }, |
322 | 392 | getConnectionState: function (immediate) { |
323 | 393 | if (vertxEventBus != null ? vertxEventBus.EventBus : void 0) { |
|
348 | 418 | readyState: wrapped.getConnectionState, |
349 | 419 | isEnabled: function () { |
350 | 420 | return enabled; |
| 421 | + }, |
| 422 | + getBufferCount: function () { |
| 423 | + return messageQueueHolder.size(); |
351 | 424 | } |
352 | 425 | }; |
353 | 426 | return api; |
|
0 commit comments