Clean up use of promises to do more sensible things with exceptions.
This commit is contained in:
@@ -262,7 +262,7 @@ Cosmopolite.prototype.shutdown = function() {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.subscribe = function(subject, opt_messages, opt_lastID) {
|
Cosmopolite.prototype.subscribe = function(subject, opt_messages, opt_lastID) {
|
||||||
return new Promise(function(resolve, reject) {
|
return this.newPromise_(function(resolve, reject) {
|
||||||
/** @type {Cosmopolite.typeSubject} */
|
/** @type {Cosmopolite.typeSubject} */
|
||||||
var canonicalSubject = this.canonicalSubject_(subject);
|
var canonicalSubject = this.canonicalSubject_(subject);
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
@@ -301,7 +301,7 @@ Cosmopolite.prototype.subscribe = function(subject, opt_messages, opt_lastID) {
|
|||||||
'send', 'event', 'cosmopolite', 'subscribe', subjectString);
|
'send', 'event', 'cosmopolite', 'subscribe', subjectString);
|
||||||
} else {
|
} else {
|
||||||
delete this.subscriptions_[subjectString];
|
delete this.subscriptions_[subjectString];
|
||||||
reject();
|
reject(new Error(result));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
@@ -318,7 +318,7 @@ Cosmopolite.prototype.subscribe = function(subject, opt_messages, opt_lastID) {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.unsubscribe = function(subject) {
|
Cosmopolite.prototype.unsubscribe = function(subject) {
|
||||||
return new Promise(function(resolve, reject) {
|
return this.newPromise_(function(resolve, reject) {
|
||||||
/** @type {Cosmopolite.typeSubject} */
|
/** @type {Cosmopolite.typeSubject} */
|
||||||
var canonicalSubject = this.canonicalSubject_(subject);
|
var canonicalSubject = this.canonicalSubject_(subject);
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
@@ -340,7 +340,7 @@ Cosmopolite.prototype.unsubscribe = function(subject) {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.sendMessage = function(subject, message) {
|
Cosmopolite.prototype.sendMessage = function(subject, message) {
|
||||||
return new Promise(function(resolve, reject) {
|
return this.newPromise_(function(resolve, reject) {
|
||||||
var args = {
|
var args = {
|
||||||
'subject': this.canonicalSubject_(subject),
|
'subject': this.canonicalSubject_(subject),
|
||||||
'message': JSON.stringify(message),
|
'message': JSON.stringify(message),
|
||||||
@@ -415,7 +415,7 @@ Cosmopolite.prototype.getPins = function(subject) {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.getProfile = function() {
|
Cosmopolite.prototype.getProfile = function() {
|
||||||
return new Promise(function(resolve, reject) {
|
return this.newPromise_(function(resolve, reject) {
|
||||||
if (this.profile_) {
|
if (this.profile_) {
|
||||||
resolve(this.profile_);
|
resolve(this.profile_);
|
||||||
} else {
|
} else {
|
||||||
@@ -449,7 +449,7 @@ Cosmopolite.prototype.currentProfile = function() {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.pin = function(subject, message) {
|
Cosmopolite.prototype.pin = function(subject, message) {
|
||||||
return new Promise(function(resolve, reject) {
|
return this.newPromise_(function(resolve, reject) {
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
var id = this.uuid_();
|
var id = this.uuid_();
|
||||||
var args = {
|
var args = {
|
||||||
@@ -474,7 +474,7 @@ Cosmopolite.prototype.pin = function(subject, message) {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.unpin = function(id) {
|
Cosmopolite.prototype.unpin = function(id) {
|
||||||
return new Promise(function(resolve, reject) {
|
return this.newPromise_(function(resolve, reject) {
|
||||||
var pin = this.pins_[id];
|
var pin = this.pins_[id];
|
||||||
var args = {
|
var args = {
|
||||||
'subject': pin['subject'],
|
'subject': pin['subject'],
|
||||||
@@ -516,7 +516,7 @@ Cosmopolite.prototype.init_ = function() {
|
|||||||
* @type {Promise}
|
* @type {Promise}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this.channelAPIPromise_ = new Promise(function(resolve, reject) {
|
this.channelAPIPromise_ = this.newPromise_(function(resolve, reject) {
|
||||||
var script = document.createElement('script');
|
var script = document.createElement('script');
|
||||||
script.src = '/_ah/channel/jsapi';
|
script.src = '/_ah/channel/jsapi';
|
||||||
script.async = true;
|
script.async = true;
|
||||||
@@ -568,6 +568,25 @@ Cosmopolite.prototype.init_ = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a new Promise object with exception handling.
|
||||||
|
*
|
||||||
|
* @param {function(...)} callback
|
||||||
|
*
|
||||||
|
* @return {Promise}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
Cosmopolite.prototype.newPromise_ = function(callback) {
|
||||||
|
return new Promise(callback).catch(function(err) {
|
||||||
|
this.trackEvent('send', 'exception', {
|
||||||
|
'exDescription': err.message
|
||||||
|
});
|
||||||
|
console.log(err);
|
||||||
|
throw err;
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a string identifying us to be included in log messages.
|
* Generate a string identifying us to be included in log messages.
|
||||||
*
|
*
|
||||||
@@ -725,7 +744,7 @@ Cosmopolite.prototype.onMessageSent_ = function(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (reject) {
|
if (reject) {
|
||||||
reject();
|
reject(new Error(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user