From cc70e62e8dd4c3b75f87dd8282804cf9a3106181 Mon Sep 17 00:00:00 2001 From: Fredrik Bonander Date: Thu, 7 Jan 2016 10:49:23 +0000 Subject: [PATCH 1/2] fix(NgAutobahnConnectionPing): typo on isOpen --- src/utils/connectionPing/connectionPing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/connectionPing/connectionPing.js b/src/utils/connectionPing/connectionPing.js index c15b73f..eb12fe4 100644 --- a/src/utils/connectionPing/connectionPing.js +++ b/src/utils/connectionPing/connectionPing.js @@ -68,7 +68,7 @@ $rootScope.$on(NG_AUTOBAHN_CONNECTION_EVENTS.LOST, _ping.stop); $rootScope.$on(NG_AUTOBAHN_CONNECTION_EVENTS.CLOSE, _ping.stop); - if (ngAutobahnConnection.isOpened) { + if (ngAutobahnConnection.isOpen) { _ping.start(); } }; From 9e4cb1418c518a37d90a898bd1944a8de9ccfbc6 Mon Sep 17 00:00:00 2001 From: Fredrik Bonander Date: Thu, 7 Jan 2016 11:16:44 +0000 Subject: [PATCH 2/2] fix(CxSocketConnection): notify about lost conenction on success Only notify about a lost connection (from `onclose`) after a establishing a connction successfully --- src/connection/connection.js | 16 ++++++--- src/connection/connection.spec.js | 55 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/connection/connection.js b/src/connection/connection.js index 8b4da39..9a6cf44 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -165,16 +165,22 @@ return defer.promise; - function onOpen(session) { - _session = session; - defer.resolve(session); - _connectionOpenedHandler(); + function onCloseAndNotify() { + _connection.onclose = null; + _connectionLostHandler(); } function onErrorOpening() { _connection.onclose = null; defer.reject(); - _connectionLostHandler(); + } + + function onOpen(session) { + _session = session; + defer.resolve(session); + + _connectionOpenedHandler(); + _connection.onclose = onCloseAndNotify; } } diff --git a/src/connection/connection.spec.js b/src/connection/connection.spec.js index 2718a90..c1a88f1 100644 --- a/src/connection/connection.spec.js +++ b/src/connection/connection.spec.js @@ -41,10 +41,26 @@ describe('ngAutobahn.connection', function () { autobahn = (function () { var self = this, _isConnected = true, - _restablishAfterClose = false; + _restablishAfterClose = false, + _currentConnection; // -- CONNECTION ------------------------------- - function Connection() {} + function Connection() { + var self = this; + + _currentConnection = this; + + self._ext_onclose = function () {}; + + Object.defineProperty(this, 'onclose', { + get: function () { + return self._onclose; + }, + set: function (value) { + self._ext_onclose = value; + } + }); + } Connection.prototype.close = function () { var self = this; @@ -52,10 +68,17 @@ describe('ngAutobahn.connection', function () { if (_restablishAfterClose) { _isConnected = true; } + self.onclose(); }); }; + Connection.prototype._onclose = function () { + if (this._ext_onclose) { + this._ext_onclose(); + } + }; + Connection.prototype.open = function () { var self = this; @@ -68,6 +91,7 @@ describe('ngAutobahn.connection', function () { }); }; + // -- SESSION --------------------------------- function Session() {} @@ -100,6 +124,8 @@ describe('ngAutobahn.connection', function () { if (once) { _restablishAfterClose = true; } + + _currentConnection.close(); _isConnected = false; } @@ -114,6 +140,7 @@ describe('ngAutobahn.connection', function () { window.autobahn = autobahn; spyOn(autobahn.Connection.prototype, 'open').and.callThrough(); spyOn(autobahn.Connection.prototype, 'close').and.callThrough(); + spyOn(autobahn.Connection.prototype, '_onclose').and.callThrough(); spyOn(autobahn.Session.prototype, 'call').and.callThrough(); }); @@ -146,17 +173,39 @@ describe('ngAutobahn.connection', function () { describe('establishing connection', function () { - it('should resolve the promise when the connection is stablished', function () { + it('should resolve the promise when the connection is established', function () { socketConnection.openConnection().then(promiseHandlers.success); $timeout.flush(); expect(promiseHandlers.success).toHaveBeenCalled(); }); + it('should notify about connection lost after a connection is established and the is lost', function () { + var foo = { + bar: function () {} + }; + spyOn(foo, 'bar'); + + $rootScope.$on(NG_AUTOBAHN_CONNECTION_EVENTS.LOST, foo.bar); + + socketConnection.openConnection().then(promiseHandlers.success); + $timeout.flush(); + + expect(promiseHandlers.success).toHaveBeenCalled(); + + autobahn.dropConnection(); + $timeout.flush(); + + expect(autobahn.Connection.prototype._onclose).toHaveBeenCalled(); + expect(foo.bar).toHaveBeenCalled(); + }); + it('should reject the promise when the connection can NOT be stablished', function () { socketConnection.openConnection().then(promiseHandlers.success, promiseHandlers.error); autobahn.dropConnection(); $timeout.flush(); + expect(promiseHandlers.error).toHaveBeenCalled(); + expect(autobahn.Connection.prototype._onclose).toHaveBeenCalled(); }); it('should resolve the same instance of _session when calling openConnection twice', function () {