Update Bluebird.

This commit is contained in:
Julien Fontanet 2015-02-05 13:23:26 +01:00
parent acdccd697c
commit 6acb87b7ea
2 changed files with 22 additions and 9 deletions

View File

@ -10,6 +10,20 @@ var WebSocket = require('ws');
//==================================================================== //====================================================================
function makeDeferred() {
var resolve, reject;
var promise = new Bluebird(function (resolve_, reject_) {
resolve = resolve_;
reject = reject_;
});
return {
promise: promise,
reject: reject,
resolve: resolve,
};
}
function notConnected() { function notConnected() {
throw new Error('not connected'); throw new Error('not connected');
} }
@ -63,15 +77,14 @@ assign(Xo.prototype, {
} }
}, },
connect: function () { connect: Bluebird.method(function () {
if (this.status === 'connected') { if (this.status === 'connected') {
return Bluebird.cast(); return;
} }
var deferred = Bluebird.defer();
this.status = 'connecting'; this.status = 'connecting';
var deferred = makeDeferred();
var opts = {}; var opts = {};
if (startsWith(this._url, 'wss')) { if (startsWith(this._url, 'wss')) {
// Due to imperfect TLS implementation in XO-Server. // Due to imperfect TLS implementation in XO-Server.
@ -133,13 +146,13 @@ assign(Xo.prototype, {
this._deferreds = {}; this._deferreds = {};
}.bind(this)); }.bind(this));
socket.on('error', function (error) { socket.addEventListener('error', function (error) {
// Fails the connect promise if possible. // Fails the connect promise if possible.
deferred.reject(error); deferred.reject(error);
}); });
return deferred.promise; return deferred.promise;
}, }),
call: function (method, params) { call: function (method, params) {
return this.connect().then(function () { return this.connect().then(function () {
@ -154,7 +167,7 @@ assign(Xo.prototype, {
params: params || [], params: params || [],
})); }));
var deferred = this._deferreds[id] = Bluebird.defer(); var deferred = this._deferreds[id] = makeDeferred();
return deferred.promise; return deferred.promise;
}.bind(this)); }.bind(this));

View File

@ -25,7 +25,7 @@
"index.js" "index.js"
], ],
"dependencies": { "dependencies": {
"bluebird": "^2.2.2", "bluebird": "^2.9.6",
"lodash.assign": "^2.4.1", "lodash.assign": "^2.4.1",
"lodash.foreach": "^2.4.1", "lodash.foreach": "^2.4.1",
"ws": "^0.4.31" "ws": "^0.4.31"