Add onConnect/onDisconnect callbacks

This commit is contained in:
Ian Gulliver
2014-05-27 15:48:03 -07:00
parent 1dd1490518
commit 6d6950624f
2 changed files with 31 additions and 6 deletions

View File

@@ -62,7 +62,9 @@ var typeLogin;
profile: string}} */
var typeLogout;
/** @typedef {{onLogin: (function(string, string)|undefined),
/** @typedef {{onConnect: (function()|undefined),
onDisconnect: (function()|undefined),
onLogin: (function(string, string)|undefined),
onLogout: (function(string)|undefined),
onMessage: (function(typeMessage)|undefined),
onPin: (function(typeMessage)|undefined),
@@ -847,6 +849,9 @@ Cosmopolite.prototype.onSocketOpen_ = function() {
if (this.channelState_ == this.ChannelState.OPENING) {
this.channelState_ = this.ChannelState.OPEN;
if (this.callbacks_.onConnect) {
this.callbacks_.onConnect();
}
} else {
return;
}
@@ -869,6 +874,9 @@ Cosmopolite.prototype.onSocketClose_ = function() {
if (this.channelState_ == this.ChannelState.OPEN) {
this.channelState_ = this.ChannelState.CLOSED;
if (this.callbacks_.onDisconnect) {
this.callbacks_.onDisconnect();
}
} else {
return;
}

View File

@@ -71,15 +71,26 @@ test('Construct/shutdown', function() {
ok(true, 'shutdown() succeeds');
});
asyncTest('onLogout fires', function() {
expect(1);
asyncTest('onConnect/onLogout fires', function() {
expect(2);
var numCallbacks = 0;
logout(function() {
var callbacks = {
'onConnect': function() {
ok(true, 'onConnect fired');
if (++numCallbacks == 2) {
cosmo.shutdown();
start();
}
},
'onLogout': function(login_url) {
ok(true, 'onLogout fired');
cosmo.shutdown();
start();
if (++numCallbacks == 2) {
cosmo.shutdown();
start();
}
}
};
var cosmo = new Cosmopolite(callbacks, null, randstring());
@@ -313,12 +324,18 @@ asyncTest('Message ordering', function() {
});
asyncTest('Reconnect channel', function() {
expect(2);
expect(5);
var subject = randstring();
var message = randstring();
var callbacks = {
'onConnect': function() {
ok(true, 'onConnect fired');
},
'onDisconnect': function() {
ok(true, 'onDisconnect fired');
},
'onMessage': function(msg) {
equal(msg['subject']['name'], subject, 'subject matches');
equal(msg['message'], message, 'message matches');