lib/client: added optional callback function to client.prototype.connect(); issue #52

This commit is contained in:
booo 2011-09-21 17:41:07 +02:00 committed by brianc
parent aded1af4e5
commit 5f7e85162a

View File

@ -28,7 +28,7 @@ sys.inherits(Client, EventEmitter);
var p = Client.prototype;
p.connect = function() {
p.connect = function(callback) {
var self = this;
var con = this.connection;
if(this.host && this.host.indexOf('/') === 0) {
@ -84,11 +84,17 @@ p.connect = function() {
}
});
self.emit('connect');
if (!callback) {
self.emit('connect');
} else {
callback(null,self);
//remove callback for proper error handling after the connect event
callback = null;
}
con.on('notification', function(msg) {
self.emit('notification', msg);
})
});
});
@ -103,7 +109,11 @@ p.connect = function() {
con.on('error', function(error) {
if(!self.activeQuery) {
self.emit('error', error);
if(!callback) {
self.emit('error', error);
} else {
callback(error);
}
} else {
//need to sync after error during a prepared statement
if(self.activeQuery.isPreparedStatement) {
@ -116,7 +126,7 @@ p.connect = function() {
con.on('notice', function(msg) {
self.emit('notice', msg);
})
});
};