From 1cb61cdf62d0a5b78f6b463371518b38320bae62 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 19 May 2014 21:11:19 +0300 Subject: [PATCH] Split profile() into getProfile() and currentProfile() --- static/cosmopolite.js | 22 +++++++++- static/test.js | 100 +++++++++++++++++++----------------------- 2 files changed, 65 insertions(+), 57 deletions(-) diff --git a/static/cosmopolite.js b/static/cosmopolite.js index 3a2066e..fcdf7d5 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -43,6 +43,7 @@ var Cosmopolite = function(callbacks, urlPrefix, namespace) { this.rpcQueue_ = []; this.subscriptions_ = {}; + this.profilePromises_ = []; this.messageQueueKey_ = this.namespace_ + ':message_queue'; if (this.messageQueueKey_ in localStorage) { @@ -248,13 +249,26 @@ Cosmopolite.prototype.getKeyMessage = function(subject, key) { }; /** + * Return a Promise for our profile ID. + */ +Cosmopolite.prototype.getProfile = function() { + return new Promise(function(resolve, reject) { + if (this.profile_) { + resolve(this.profile_); + } else { + this.profilePromises_.push(resolve); + } + }.bind(this)); +}; + + /** * Return our current profile ID, if known. * * @return {?string} Profile ID. * @const */ -Cosmopolite.prototype.profile = function() { - return this.profile_ || null; +Cosmopolite.prototype.currentProfile = function() { + return this.profile_; }; /** @@ -756,6 +770,10 @@ Cosmopolite.prototype.onServerEvent_ = function(e) { } if (e['profile']) { this.profile_ = e['profile']; + this.profilePromises_.forEach(function(resolve) { + resolve(this.profile_); + }.bind(this)); + this.profilePromises_ = []; } switch (e['event_type']) { case 'login': diff --git a/static/test.js b/static/test.js index 1c062b0..dddfe7f 100644 --- a/static/test.js +++ b/static/test.js @@ -380,35 +380,30 @@ asyncTest('subscribe ACL', function() { var subject = randstring(); logout(function() { - var tempCallbacks = { - 'onLogout': function() { - var tempProfile = tempCosmo.profile(); - tempCosmo.shutdown(); + var tempCosmo = new Cosmopolite({}, null, randstring()); + tempCosmo.getProfile().then(function(tempProfile) { + tempCosmo.shutdown(); - var callbacks = { - 'onLogout': function() { - cosmo.subscribe({ - 'name': subject, - 'readable_only_by': cosmo.profile(), - }).then(function() { - ok(true, 'correct ACL succeeds'); + var cosmo = new Cosmopolite({}, null, randstring()); + cosmo.getProfile().then(function(profile) { + cosmo.subscribe({ + 'name': subject, + 'readable_only_by': profile, + }).then(function() { + ok(true, 'correct ACL succeeds'); - cosmo.subscribe({ - 'name': subject, - 'readable_only_by': tempProfile, - }).then(null, function() { - ok(true, 'bad ACL fails'); - cosmo.shutdown(); - start(); - }); + cosmo.subscribe({ + 'name': subject, + 'readable_only_by': tempProfile, + }).then(null, function() { + ok(true, 'bad ACL fails'); + cosmo.shutdown(); + start(); + }); - }); - }, - }; - var cosmo = new Cosmopolite(callbacks, null, randstring()); - }, - }; - var tempCosmo = new Cosmopolite(tempCallbacks, null, randstring()); + }); + }); + }); }); }); @@ -419,35 +414,30 @@ asyncTest('sendMessage ACL', function() { var message = randstring(); logout(function() { - var tempCallbacks = { - 'onLogout': function() { - var tempProfile = tempCosmo.profile(); - tempCosmo.shutdown(); + var tempCosmo = new Cosmopolite({}, null, randstring()); + tempCosmo.getProfile().then(function(tempProfile) { + tempCosmo.shutdown(); - var callbacks = { - 'onLogout': function() { - cosmo.sendMessage({ - 'name': subject, - 'writable_only_by': cosmo.profile(), - }, message).then(function() { - ok(true, 'correct ACL succeeds'); + var cosmo = new Cosmopolite({}, null, randstring()); + cosmo.getProfile().then(function(profile) { + cosmo.sendMessage({ + 'name': subject, + 'writable_only_by': profile, + }, message).then(function() { + ok(true, 'correct ACL succeeds'); - cosmo.sendMessage({ - 'name': subject, - 'writable_only_by': tempProfile, - }, message).then(null, function() { - ok(true, 'bad ACL fails'); - cosmo.shutdown(); - start(); - }); + cosmo.sendMessage({ + 'name': subject, + 'writable_only_by': tempProfile, + }, message).then(null, function() { + ok(true, 'bad ACL fails'); + cosmo.shutdown(); + start(); + }); - }); - }, - }; - var cosmo = new Cosmopolite(callbacks, null, randstring()); - }, - }; - var tempCosmo = new Cosmopolite(tempCallbacks, null, randstring()); + }); + }); + }); }); }); @@ -463,13 +453,13 @@ asyncTest('Login', function() { var callbacks = { 'onLogout': function(login_url) { ok(true, 'onLogout fired'); - anonymousProfile = cosmo.profile(); + anonymousProfile = cosmo.currentProfile(); // Entirely magic URL that sets the login cookie and redirects. window.open('/_ah/login?email=test%40example.com&action=Login&continue=/cosmopolite/static/login_complete.html'); }, 'onLogin': function(login_url) { ok(true, 'onLogin fired'); - notEqual(anonymousProfile, cosmo.profile(), 'profile changed'); + notEqual(anonymousProfile, cosmo.currentProfile(), 'profile changed'); cosmo.shutdown(); logout(); start(); @@ -495,7 +485,7 @@ asyncTest('Profile merge', function() { 'message #' + messages + ': subject matches'); equal(msg['message'], message, 'message #' + messages + ': message matches'); - equal(msg['sender'], cosmo.profile(), + equal(msg['sender'], cosmo.currentProfile(), 'message #' + messages + ': profile matches'); if (messages == 1) { cosmo.unsubscribe(subject);