Split profile() into getProfile() and currentProfile()
This commit is contained in:
@@ -43,6 +43,7 @@ var Cosmopolite = function(callbacks, urlPrefix, namespace) {
|
|||||||
|
|
||||||
this.rpcQueue_ = [];
|
this.rpcQueue_ = [];
|
||||||
this.subscriptions_ = {};
|
this.subscriptions_ = {};
|
||||||
|
this.profilePromises_ = [];
|
||||||
|
|
||||||
this.messageQueueKey_ = this.namespace_ + ':message_queue';
|
this.messageQueueKey_ = this.namespace_ + ':message_queue';
|
||||||
if (this.messageQueueKey_ in localStorage) {
|
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 our current profile ID, if known.
|
||||||
*
|
*
|
||||||
* @return {?string} Profile ID.
|
* @return {?string} Profile ID.
|
||||||
* @const
|
* @const
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.profile = function() {
|
Cosmopolite.prototype.currentProfile = function() {
|
||||||
return this.profile_ || null;
|
return this.profile_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -756,6 +770,10 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
|
|||||||
}
|
}
|
||||||
if (e['profile']) {
|
if (e['profile']) {
|
||||||
this.profile_ = e['profile'];
|
this.profile_ = e['profile'];
|
||||||
|
this.profilePromises_.forEach(function(resolve) {
|
||||||
|
resolve(this.profile_);
|
||||||
|
}.bind(this));
|
||||||
|
this.profilePromises_ = [];
|
||||||
}
|
}
|
||||||
switch (e['event_type']) {
|
switch (e['event_type']) {
|
||||||
case 'login':
|
case 'login':
|
||||||
|
|||||||
100
static/test.js
100
static/test.js
@@ -380,35 +380,30 @@ asyncTest('subscribe ACL', function() {
|
|||||||
var subject = randstring();
|
var subject = randstring();
|
||||||
|
|
||||||
logout(function() {
|
logout(function() {
|
||||||
var tempCallbacks = {
|
var tempCosmo = new Cosmopolite({}, null, randstring());
|
||||||
'onLogout': function() {
|
tempCosmo.getProfile().then(function(tempProfile) {
|
||||||
var tempProfile = tempCosmo.profile();
|
tempCosmo.shutdown();
|
||||||
tempCosmo.shutdown();
|
|
||||||
|
|
||||||
var callbacks = {
|
var cosmo = new Cosmopolite({}, null, randstring());
|
||||||
'onLogout': function() {
|
cosmo.getProfile().then(function(profile) {
|
||||||
cosmo.subscribe({
|
cosmo.subscribe({
|
||||||
'name': subject,
|
'name': subject,
|
||||||
'readable_only_by': cosmo.profile(),
|
'readable_only_by': profile,
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
ok(true, 'correct ACL succeeds');
|
ok(true, 'correct ACL succeeds');
|
||||||
|
|
||||||
cosmo.subscribe({
|
cosmo.subscribe({
|
||||||
'name': subject,
|
'name': subject,
|
||||||
'readable_only_by': tempProfile,
|
'readable_only_by': tempProfile,
|
||||||
}).then(null, function() {
|
}).then(null, function() {
|
||||||
ok(true, 'bad ACL fails');
|
ok(true, 'bad ACL fails');
|
||||||
cosmo.shutdown();
|
cosmo.shutdown();
|
||||||
start();
|
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();
|
var message = randstring();
|
||||||
|
|
||||||
logout(function() {
|
logout(function() {
|
||||||
var tempCallbacks = {
|
var tempCosmo = new Cosmopolite({}, null, randstring());
|
||||||
'onLogout': function() {
|
tempCosmo.getProfile().then(function(tempProfile) {
|
||||||
var tempProfile = tempCosmo.profile();
|
tempCosmo.shutdown();
|
||||||
tempCosmo.shutdown();
|
|
||||||
|
|
||||||
var callbacks = {
|
var cosmo = new Cosmopolite({}, null, randstring());
|
||||||
'onLogout': function() {
|
cosmo.getProfile().then(function(profile) {
|
||||||
cosmo.sendMessage({
|
cosmo.sendMessage({
|
||||||
'name': subject,
|
'name': subject,
|
||||||
'writable_only_by': cosmo.profile(),
|
'writable_only_by': profile,
|
||||||
}, message).then(function() {
|
}, message).then(function() {
|
||||||
ok(true, 'correct ACL succeeds');
|
ok(true, 'correct ACL succeeds');
|
||||||
|
|
||||||
cosmo.sendMessage({
|
cosmo.sendMessage({
|
||||||
'name': subject,
|
'name': subject,
|
||||||
'writable_only_by': tempProfile,
|
'writable_only_by': tempProfile,
|
||||||
}, message).then(null, function() {
|
}, message).then(null, function() {
|
||||||
ok(true, 'bad ACL fails');
|
ok(true, 'bad ACL fails');
|
||||||
cosmo.shutdown();
|
cosmo.shutdown();
|
||||||
start();
|
start();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
};
|
});
|
||||||
var cosmo = new Cosmopolite(callbacks, null, randstring());
|
|
||||||
},
|
|
||||||
};
|
|
||||||
var tempCosmo = new Cosmopolite(tempCallbacks, null, randstring());
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -463,13 +453,13 @@ asyncTest('Login', function() {
|
|||||||
var callbacks = {
|
var callbacks = {
|
||||||
'onLogout': function(login_url) {
|
'onLogout': function(login_url) {
|
||||||
ok(true, 'onLogout fired');
|
ok(true, 'onLogout fired');
|
||||||
anonymousProfile = cosmo.profile();
|
anonymousProfile = cosmo.currentProfile();
|
||||||
// Entirely magic URL that sets the login cookie and redirects.
|
// 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');
|
window.open('/_ah/login?email=test%40example.com&action=Login&continue=/cosmopolite/static/login_complete.html');
|
||||||
},
|
},
|
||||||
'onLogin': function(login_url) {
|
'onLogin': function(login_url) {
|
||||||
ok(true, 'onLogin fired');
|
ok(true, 'onLogin fired');
|
||||||
notEqual(anonymousProfile, cosmo.profile(), 'profile changed');
|
notEqual(anonymousProfile, cosmo.currentProfile(), 'profile changed');
|
||||||
cosmo.shutdown();
|
cosmo.shutdown();
|
||||||
logout();
|
logout();
|
||||||
start();
|
start();
|
||||||
@@ -495,7 +485,7 @@ asyncTest('Profile merge', function() {
|
|||||||
'message #' + messages + ': subject matches');
|
'message #' + messages + ': subject matches');
|
||||||
equal(msg['message'], message,
|
equal(msg['message'], message,
|
||||||
'message #' + messages + ': message matches');
|
'message #' + messages + ': message matches');
|
||||||
equal(msg['sender'], cosmo.profile(),
|
equal(msg['sender'], cosmo.currentProfile(),
|
||||||
'message #' + messages + ': profile matches');
|
'message #' + messages + ': profile matches');
|
||||||
if (messages == 1) {
|
if (messages == 1) {
|
||||||
cosmo.unsubscribe(subject);
|
cosmo.unsubscribe(subject);
|
||||||
|
|||||||
Reference in New Issue
Block a user