diff --git a/static/cosmopolite.js b/static/cosmopolite.js index 0c7b3c9..0ef5b63 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -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; } diff --git a/static/test.js b/static/test.js index 1d73fd6..33d2ee7 100644 --- a/static/test.js +++ b/static/test.js @@ -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');