Permalink
Browse files

refactor doOpen

  • Loading branch information...
1 parent 065f608 commit 9a882c61abefef737ab615e7edd6a933fcb39d1e @aheckmann aheckmann committed Jul 13, 2012
Showing with 22 additions and 26 deletions.
  1. +22 −26 lib/connection.js
View
@@ -198,25 +198,7 @@ Connection.prototype.open = function (host, database, port, options, callback) {
this.port = port;
this.readyState = STATES.connecting;
- this.emit('opening'); // backward compat. same as on('connecting')
-
- // open connection
- this.doOpen(function (err) {
- if (err) {
- // TODO emit if no listeners
- if (self._events && self._events.error &&
- ('function' == typeof self._events.error || self._events.error.length)) {
- self.emit("error", err);
- }
- self.readyState = STATES.disconnected;
- callback(err);
- return;
- }
-
- self.onOpen();
- callback(err);
- });
-
+ this._open(callback);
return this;
};
@@ -265,6 +247,7 @@ Connection.prototype.openSet = function (uris, database, options, callback) {
return this;
}
+ this.replica = true;
this.host = [];
this.port = [];
@@ -293,23 +276,36 @@ Connection.prototype.openSet = function (uris, database, options, callback) {
return this;
}
+ this._open(callback);
+ return this;
+};
+
+Connection.prototype._open = function (callback) {
this.readyState = STATES.connecting;
- this.emit('opening');
+
+ var self = this;
+
+ var method = this.replica
+ ? 'doOpenSet'
+ : 'doOpen';
// open connection
- this.doOpenSet(function (err) {
+ this[method](function (err) {
if (err) {
- if (self._events && self._events.error && self._events.error.length) {
+ // TODO emit if no listeners
+ if (self._events && self._events.error &&
+ ('function' == typeof self._events.error || self._events.error.length)) {
self.emit("error", err);
}
self.readyState = STATES.disconnected;
- } else {
- self.onOpen();
+ callback(err);
+ return;
}
- callback(err || null);
+ self.onOpen();
+ callback(err);
});
-};
+}
/**
* Called when the connection is opened

0 comments on commit 9a882c6

Please sign in to comment.