Skip to content

Commit

Permalink
Merge pull request jsforce#202 from jsforce/change-promise-impl
Browse files Browse the repository at this point in the history
Change Promise implementation
  • Loading branch information
stomita committed Apr 9, 2015
2 parents e70006b + 520d5a7 commit 4858c74
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 85 deletions.
8 changes: 4 additions & 4 deletions lib/api/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var inherits = require('inherits'),
* @protected
* @class Bulk~Job
* @extends events.EventEmitter
*
*
* @param {Bulk} bulk - Bulk API object
* @param {String} [type] - SObject type
* @param {String} [operation] - Bulk load operation ('insert', 'update', 'upsert', 'delete', or 'hardDelete')
Expand Down Expand Up @@ -185,7 +185,7 @@ Job.prototype.check = function(callback) {
* @returns {Promise.<Bulk~JobInfo>}
*/
Job.prototype._waitAssign = function(callback) {
return (this.id ? new Promise({ id: this.id }) : this.open()).thenCall(callback);
return (this.id ? Promise.resolve({ id: this.id }) : this.open()).thenCall(callback);
};


Expand Down Expand Up @@ -706,8 +706,8 @@ var Bulk = function(conn) {
this._logger = conn._logger;
};

/**
* Polling interval in milliseconds
/**
* Polling interval in milliseconds
* @type {Number}
*/
Bulk.prototype.pollInterval = 1000;
Expand Down
3 changes: 1 addition & 2 deletions lib/api/tooling.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

'use strict';

var util = require('util'),
_ = require('underscore'),
var _ = require('underscore'),
Cache = require('../cache');

/**
Expand Down
36 changes: 18 additions & 18 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var Connection = module.exports = function(options) {
}
/**
* Metadata API object
* @member {Metadata} Connection#metadata
* @member {Metadata} Connection#metadata
*/
if (Metadata) {
this.metadata = new Metadata(this);
Expand Down Expand Up @@ -482,7 +482,7 @@ function parseIdUrl(idUrl) {

/**
* Execute query by using SOQL
*
*
* @param {String} soql - SOQL string
* @param {Callback.<QueryResult>} [callback] - Callback function
* @returns {Query.<QueryResult>}
Expand All @@ -497,7 +497,7 @@ Connection.prototype.query = function(soql, callback) {

/**
* Execute query by using SOQL, including deleted records
*
*
* @param {String} soql - SOQL string
* @param {Callback.<QueryResult>} [callback] - Callback function
* @returns {Query.<QueryResult>}
Expand All @@ -513,7 +513,7 @@ Connection.prototype.queryAll = function(soql, callback) {

/**
* Query next record set by using query locator
*
*
* @param {String} locator - Next record set locator
* @param {Callback.<QueryResult>} [callback] - Callback function
* @returns {Query.<QueryResult>}
Expand All @@ -530,7 +530,7 @@ Connection.prototype.queryMore = function(locator, callback) {
* Retrieve specified records
*
* @param {String} type - SObject Type
* @param {String|Array.<String>} ids - A record ID or array of record IDs
* @param {String|Array.<String>} ids - A record ID or array of record IDs
* @param {Object} [options] - Options for rest api.
* @param {Callback.<Record|Array.<Record>>} [callback] - Callback function
* @returns {Promise.<Record|Array.<Record>>}
Expand Down Expand Up @@ -739,7 +739,7 @@ Connection.prototype.upsert = function(type, records, extIdField, options, callb
body : JSON.stringify(record),
headers : _.defaults(options.headers || {}, {
"Content-Type" : "application/json"
})
})
}, {
noContentResponse: { success : true, errors : [] }
});
Expand Down Expand Up @@ -811,7 +811,7 @@ Connection.prototype.destroy = function(type, ids, options, callback) {

/**
* Execute search by SOSL
*
*
* @param {String} sosl - SOSL string
* @param {Callback.<Array.<RecordResult>>} [callback] - Callback function
* @returns {Promise.<Array.<RecordResult>>}
Expand Down Expand Up @@ -842,7 +842,7 @@ Connection.prototype.search = function(sosl, callback) {
* @param {Callback.<DescribeSObjectResult>} [callback] - Callback function
* @returns {Promise.<DescribeSObjectResult>}
*/
Connection.prototype.describe =
Connection.prototype.describe =
Connection.prototype.describeSObject = function(type, callback) {
var url = [ this._baseUrl(), "sobjects", type, "describe" ].join('/');
return this.request(url).thenCall(callback);
Expand Down Expand Up @@ -874,7 +874,7 @@ Connection.prototype.describeGlobal = function(callback) {
*/
Connection.prototype.sobject = function(type) {
this.sobjects = this.sobjects || {};
var sobject = this.sobjects[type] =
var sobject = this.sobjects[type] =
this.sobjects[type] || new SObject(this, type);
return sobject;
};
Expand All @@ -888,8 +888,8 @@ Connection.prototype.sobject = function(type) {
Connection.prototype.identity = function(callback) {
var self = this;
var idUrl = this.userInfo && this.userInfo.url;
return new Promise(
idUrl ?
return Promise.resolve(
idUrl ?
{ identity: idUrl } :
this.request(this._baseUrl())
).then(function(res) {
Expand Down Expand Up @@ -943,7 +943,7 @@ Connection.prototype.authorize = function(code, callback) {

/**
* Login to Salesforce
*
*
* @param {String} username - Salesforce username
* @param {String} password - Salesforce password (and security token, if required)
* @param {Callback.<UserInfo>} [callback] - Callback function
Expand Down Expand Up @@ -1059,8 +1059,8 @@ Connection.prototype.loginBySoap = function(username, password, callback) {
organizationId: orgId,
url: idUrl
};
self.initialize({
serverUrl: serverUrl.split('/').slice(0, 3).join('/'),
self.initialize({
serverUrl: serverUrl.split('/').slice(0, 3).join('/'),
sessionId: sessionId,
userInfo: userInfo
});
Expand Down Expand Up @@ -1163,7 +1163,7 @@ Connection.prototype.logoutBySoap = function(callback) {

/**
* List recently viewed records
*
*
* @param {String} [type] - SObject type
* @param {Number} [limit] - Limit num to fetch
* @param {Callback.<Array.<RecordResult>>} [callback] - Callback function
Expand All @@ -1187,7 +1187,7 @@ Connection.prototype.recent = function(type, limit, callback) {
}).thenCall(callback);
} else {
url = this._baseUrl() + "/recent";
if (limit) {
if (limit) {
url += "?limit=" + limit;
}
return this.request(url).thenCall(callback);
Expand All @@ -1197,7 +1197,7 @@ Connection.prototype.recent = function(type, limit, callback) {

/**
* @typedef {Object} UpdatedRecordsInfo
* @prop {String} latestDateCovered - The timestamp of the last date covered.
* @prop {String} latestDateCovered - The timestamp of the last date covered.
* @prop {Array.<String>} ids - Updated record IDs.
*/

Expand Down Expand Up @@ -1337,7 +1337,7 @@ Connection.prototype.theme = function(callback) {
};

/**
* Returns all registered global quick actions
* Returns all registered global quick actions
*
* @param {Callback.<Array.<QuickAction~QuickActionInfo>>} [callback] - Callback function
* @returns {Promise.<Array.<QuickAction~QuickActionInfo>>}
Expand Down
1 change: 1 addition & 0 deletions lib/jsforce.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ exports.Connection = require('./connection');
exports.OAuth2 = require('./oauth2');
exports.Date = exports.SfDate = require("./date");
exports.RecordStream = require('./record-stream');
exports.Promise = require('./promise');
107 changes: 62 additions & 45 deletions lib/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@

'use strict';

var Q = require('q'),
_ = require('underscore')._;
var _ = require('underscore');

/**
* Promises/A+ spec compliant class, with a little extension
* http://promises-aplus.github.io/promises-spec/
*
* @class Promise
* @constructor
* @param {Promise.<T>|T} o - Object to wrap with promise
* @template T
* @callback ResolvedCallback
* @param {T} result - Resolved value
* @returns {S}
* @template T,S
*/
var Promise = function(o) {
this._promise = Q(o);
};

/**
* @callback FulfilledCallback
* @param {T} result - Fulfilled value
* @callback RejectedCallback
* @param {Error} reason - Rejected reason
* @returns {S}
* @template T,S
* @template S
*/

/**
* @callback ResolveCallback
* @param {T} result
* @template T
*/

/**
Expand All @@ -32,21 +31,37 @@ var Promise = function(o) {
* @template S
*/

/**
* @callback PromiseCallback
* @param {ResolveCallback.<T>} resolve
* @param {RejectCallback} reject
* @template T
*/

/**
* Promise class with a little extension
*
* @class Promise
* @constructor
* @param {PromiseCallback.<T>}
* @template T
*/
var Promise = require('promise/lib/es6-extensions');

/**
* The "then" method from the Promises/A+ specification
*
* @method Promise#then
* @param {FulfilledCallback.<T, S1>} [onFulfilled]
* @param {RejectedCallback.<S2>} [onRejected]
* @returns {Promise.<S1|S2>}
*/
Promise.prototype.then = function() {
// Delegate Q promise implementation and wrap by our Promise instance
return new Promise(this._promise.then.apply(this._promise, arguments));
};

/**
* Call "then" using given node-style callback function
* Call "then" using given node-style callback function.
* This is basically same as "nodeify" except that it always return the original promise
*
* @method Promise#thenCall
* @param {Callback.<T>} [callback] - Callback function
* @returns {Promise}
*/
Expand All @@ -68,54 +83,57 @@ Promise.prototype.thenCall = function(callback) {
/**
* A sugar method, equivalent to promise.then(undefined, onRejected).
*
* @method Promise#catch
* @param {RejectedCallback.<S>} onRejected
* @returns {Promise.<S>}
*/
Promise.prototype.fail = function() {
return new Promise(this._promise.fail.apply(this._promise, arguments));
};

/**
* Synonym of Promise#catch
*
* @method Promise#fail
* @param {RejectedCallback.<S>} onRejected
* @returns {Promise.<S>}
*/
Promise.prototype.fail = Promise.prototype.catch;

/**
* Alias for completion
*
* @method Promise#done
* @param {FulfilledCallback.<T, S>} [onFulfilled]
* @returns {Promise.<S>}
*/
Promise.prototype.done = function() {
return new Promise(this._promise.done.apply(this._promise, arguments));
};

/**
* @param {...Promise.<*>} p
* Returns resolving promise with given reason
*
* @method Promise.resolve
* @param {*} result - Resolved value
* @returns {Promise}
*/
Promise.when = function() {
return new Promise(Q.when.apply(Q, arguments));
};

/**
* Returns rejecting promise with given reason
*
* @method Promise.reject
* @param {Error} reason - Rejecting reason
* @returns {Promise}
*/
Promise.reject = function(reason) {
return new Promise(Q.reject(reason));
};

/**
* Returns a promise that is fulfilled with an array containing the fulfillment value of each promise,
* Returns a promise that is fulfilled with an array containing the fulfillment value of each promise,
* or is rejected with the same rejection reason as the first promise to be rejected.
*
* @method Promise.all
* @param {Array.<Promise.<*>|*>} promises
* @returns {Promise.<Array.<*>>}
*/
Promise.all = function() {
return new Promise(Q.all.apply(Q, arguments));
};

/**
* Returns a deferred object
*
* @method Promise.defer
* @returns {Deferred}
*/
Promise.defer = function() {
Expand All @@ -129,25 +147,24 @@ Promise.defer = function() {
* @constructor
*/
var Deferred = function() {
this._deferred = Q.defer();
this.promise = new Promise(this._deferred.promise);
var self = this;
this.promise = new Promise(function(resolve, reject) {
self.resolve = resolve;
self.reject = reject;
});
};

/**
* Resolve promise
* @method Deferred#resolve
* @param {*} result - Resolving result
*/
Deferred.prototype.resolve = function() {
return this._deferred.resolve.apply(this._promise, arguments);
};

/**
* Reject promise
* @method Deferred#reject
* @param {Error} error - Rejecting reason
*/
Deferred.prototype.reject = function() {
return this._deferred.reject.apply(this._promise, arguments);
};

/**
*
Expand Down
Loading

0 comments on commit 4858c74

Please sign in to comment.