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

View File

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