|
1 | | -/*! angular-vertxbus - v1.0.0 - 2015-03-01 |
| 1 | +/*! angular-vertxbus - v1.1.0 - 2015-04-06 |
2 | 2 | * http://github.com/knalli/angular-vertxbus |
3 | 3 | * Copyright (c) 2015 ; Licensed */ |
4 | 4 | (function() { |
|
140 | 140 | - recconnect() |
141 | 141 | */ |
142 | 142 | this.$get = ['$timeout', '$log', function($timeout, $log) { |
143 | | - var EventBusOriginal, EventBusStub, connect, debugEnabled, enabled, eventBus, prefix, reconnectEnabled, sockjsOptions, sockjsReconnectInterval, sockjsStateInterval, url, urlPath, urlServer, _ref; |
| 143 | + var EventBusOriginal, EventBusStub, connect, debugEnabled, disconnectTimeoutEnabled, enabled, eventBus, prefix, reconnectEnabled, sockjsOptions, sockjsReconnectInterval, sockjsStateInterval, url, urlPath, urlServer, _ref; |
144 | 144 | _ref = angular.extend({}, DEFAULT_OPTIONS, options), 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; |
145 | 145 | EventBusStub = null; |
146 | 146 | EventBusOriginal = typeof vertx !== "undefined" && vertx !== null ? vertx.EventBus : void 0; |
|
150 | 150 | $log.debug("[Vert.x EB Stub] Enabled: connecting '" + url + "'"); |
151 | 151 | } |
152 | 152 | eventBus = null; |
| 153 | + disconnectTimeoutEnabled = true; |
153 | 154 | connect = function() { |
154 | 155 | eventBus = new EventBusOriginal(url, void 0, sockjsOptions); |
155 | 156 | eventBus.onopen = function() { |
|
161 | 162 | } |
162 | 163 | }; |
163 | 164 | eventBus.onclose = function() { |
164 | | - if (debugEnabled) { |
165 | | - $log.debug("[Vert.x EB Stub] Reconnect in " + sockjsReconnectInterval + "ms"); |
166 | | - } |
167 | 165 | if (typeof EventBusStub.onclose === 'function') { |
168 | 166 | EventBusStub.onclose(); |
169 | 167 | } |
170 | | - if (reconnectEnabled) { |
171 | | - $timeout(connect, sockjsReconnectInterval); |
| 168 | + if (!disconnectTimeoutEnabled) { |
| 169 | + if (debugEnabled) { |
| 170 | + $log.debug("[Vert.x EB Stub] Reconnect immediately"); |
| 171 | + } |
| 172 | + disconnectTimeoutEnabled = true; |
| 173 | + connect(); |
| 174 | + } else { |
| 175 | + if (reconnectEnabled) { |
| 176 | + if (debugEnabled) { |
| 177 | + $log.debug("[Vert.x EB Stub] Reconnect in " + sockjsReconnectInterval + "ms"); |
| 178 | + } |
| 179 | + $timeout(connect, sockjsReconnectInterval); |
| 180 | + } |
172 | 181 | } |
173 | 182 | }; |
174 | 183 | }; |
175 | 184 | connect(); |
176 | 185 | EventBusStub = { |
177 | | - reconnect: function() { |
178 | | - return eventBus.close(); |
| 186 | + reconnect: function(immediately) { |
| 187 | + if (immediately == null) { |
| 188 | + immediately = false; |
| 189 | + } |
| 190 | + if (eventBus.readyState() === EventBusStub.EventBus.OPEN) { |
| 191 | + if (immediately) { |
| 192 | + disconnectTimeoutEnabled = false; |
| 193 | + } |
| 194 | + return eventBus.close(); |
| 195 | + } else { |
| 196 | + return connect(); |
| 197 | + } |
179 | 198 | }, |
180 | 199 | close: function() { |
181 | 200 | return eventBus.close(); |
|
528 | 547 | vertxEventBus.unregisterHandler(address, callbackMap.get(callback)); |
529 | 548 | callbackMap.remove(callback); |
530 | 549 | }, |
531 | | - send: function(address, message, timeout) { |
| 550 | + send: function(address, message, timeout, expectReply) { |
532 | 551 | var deferred, dispatched, next; |
533 | 552 | if (timeout == null) { |
534 | 553 | timeout = 10000; |
535 | 554 | } |
| 555 | + if (expectReply == null) { |
| 556 | + expectReply = true; |
| 557 | + } |
536 | 558 | deferred = $q.defer(); |
537 | 559 | next = function() { |
538 | | - vertxEventBus.send(address, message, function(reply) { |
539 | | - if (deferred) { |
| 560 | + if (expectReply) { |
| 561 | + vertxEventBus.send(address, message, function(reply) { |
540 | 562 | return deferred.resolve(reply); |
541 | | - } |
542 | | - }); |
543 | | - if (deferred) { |
| 563 | + }); |
544 | 564 | return $interval((function() { |
545 | 565 | return deferred.reject(); |
546 | 566 | }), timeout, 1); |
| 567 | + } else { |
| 568 | + vertxEventBus.send(address, message); |
| 569 | + return deferred.resolve(); |
547 | 570 | } |
548 | 571 | }; |
549 | 572 | next.displayName = "" + CONSTANTS.MODULE + "/" + CONSTANTS.COMPONENT + ": util.send (ensureOpenAuthConnection callback)"; |
|
641 | 664 | return util.unregisterHandler(address, callback); |
642 | 665 | } |
643 | 666 | }, |
644 | | - send: function(address, message, timeout) { |
| 667 | + send: function(address, message, timeout, expectReply) { |
645 | 668 | if (timeout == null) { |
646 | 669 | timeout = 10000; |
647 | 670 | } |
648 | | - return util.send(address, message, timeout); |
| 671 | + if (expectReply == null) { |
| 672 | + expectReply = true; |
| 673 | + } |
| 674 | + return util.send(address, message, timeout, expectReply); |
649 | 675 | }, |
650 | 676 | publish: function(address, message) { |
651 | 677 | return util.publish(address, message); |
|
0 commit comments