Skip to content

Commit

Permalink
Merge pull request #5 from ef-ctx/fix/dont-notify-lost-on-failed-conn…
Browse files Browse the repository at this point in the history
…ection

Fix/dont notify lost on failed connection
  • Loading branch information
jamesbrobb committed Jan 7, 2016
2 parents e330224 + 9e4cb14 commit feb6394
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
55 changes: 52 additions & 3 deletions src/connection/connection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,44 @@ 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;
$timeout(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;

Expand All @@ -68,6 +91,7 @@ describe('ngAutobahn.connection', function () {
});
};


// -- SESSION ---------------------------------
function Session() {}

Expand Down Expand Up @@ -100,6 +124,8 @@ describe('ngAutobahn.connection', function () {
if (once) {
_restablishAfterClose = true;
}

_currentConnection.close();
_isConnected = false;
}

Expand All @@ -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();
});

Expand Down Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/connectionPing/connectionPing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
Expand Down

0 comments on commit feb6394

Please sign in to comment.