Skip to content

Commit

Permalink
make connect/open/apenSet/disconnect return a promise, fixes #3622
Browse files Browse the repository at this point in the history
  • Loading branch information
TrejGun committed Jan 24, 2016
1 parent 6482a67 commit 21f0104
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
56 changes: 49 additions & 7 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var utils = require('./utils'),
Collection = require(driver + '/collection'),
STATES = require('./connectionstate'),
MongooseError = require('./error'),
muri = require('muri');
muri = require('muri'),
PromiseProvider = require('./promise_provider');

/*!
* Protocol prefix regexp.
Expand Down Expand Up @@ -275,8 +276,18 @@ Connection.prototype.open = function(host, database, port, options, callback) {
this.host = host;
this.port = port;

this._open(callback);
return this;
var self = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
self._open(!!callback, function(error) {
callback && callback(error);
if (error) {
reject(error);
return;
}
resolve();
});
});
};

/**
Expand Down Expand Up @@ -405,8 +416,18 @@ Connection.prototype.openSet = function(uris, database, options, callback) {

}

this._open(callback);
return this;
var self = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
self._open(!!callback, function(error) {
callback && callback(error);
if (error) {
reject(error);
return;
}
resolve();
});
});
};

/**
Expand All @@ -432,7 +453,7 @@ Connection.prototype.error = function(err, callback) {
* @api private
*/

Connection.prototype._open = function(callback) {
Connection.prototype._open = function(emit, callback) {
this.readyState = STATES.connecting;
this._closeCalled = false;

Expand All @@ -449,7 +470,7 @@ Connection.prototype._open = function(callback) {
if (self._hasOpened) {
if (callback) callback(err);
} else {
self.error(err, callback);
self.error(err, emit && callback);
}
return;
}
Expand Down Expand Up @@ -508,6 +529,27 @@ Connection.prototype.onOpen = function(callback) {
*/

Connection.prototype.close = function(callback) {
var self = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
self._close(function(error) {
callback && callback(error);
if (error) {
reject(error);
return;
}
resolve();
});
});
};

/**
* Handles closing the connection
*
* @param {Function} callback
* @api private
*/
Connection.prototype._close = function(callback) {
var self = this;
this._closeCalled = true;

Expand Down
29 changes: 16 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,11 @@ Mongoose.prototype.connect = function() {
var conn = this.connection;

if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) {
conn.openSet.apply(conn, arguments);
return conn.openSet.apply(conn, arguments);
} else {
conn.open.apply(conn, arguments);
return conn.open.apply(conn, arguments);
}

return this;
};

/**
Expand All @@ -249,24 +248,28 @@ Mongoose.prototype.connect = function() {
*/

Mongoose.prototype.disconnect = function(fn) {
var count = this.connections.length,
error;
var error;

this.connections.forEach(function(conn) {
conn.close(function(err) {
if (error) return;

if (error) {
return;
}
if (err) {
error = err;
if (fn) return fn(err);
throw err;
}

if (fn)
--count || fn();
});
});
return this;

var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
fn && fn(error);
if (error) {
reject(error);
return;
}
resolve();
});
};

/**
Expand Down
12 changes: 5 additions & 7 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ describe('mongoose module:', function() {
cb(new Error('bam'));
};

var failure = {};
try {
mong.disconnect();
} catch (err) {
failure = err;
}
assert.equal('bam', failure.message);
mong.disconnect()
.on('error', function (error) {
assert.equal('bam', error.message);
});

done();
});
});
Expand Down

0 comments on commit 21f0104

Please sign in to comment.