Avoid duplicate connecting

This commit is contained in:
Raymond Feng 2013-05-31 13:40:50 -07:00
parent 9d74759f90
commit 56af9673c1
1 changed files with 9 additions and 11 deletions

View File

@ -137,28 +137,26 @@ DataSource.prototype.setup = function(name, settings) {
schema.connect = function(cb) {
var schema = this;
if(schema.connected) {
if (cb) {
process.nextTick(cb);
}
if(schema.connected || schema.connecting) {
process.nextTick(function() {
cb && cb();
});
return;
}
schema.connecting = true;
if (schema.adapter.connect) {
schema.adapter.connect(function(err) {
schema.adapter.connect(function(err, result) {
if (!err) {
schema.connected = true;
schema.connecting = false;
schema.emit('connected');
}
if (cb) {
cb(err);
}
cb && cb(err, result);
});
} else {
if (cb) {
process.nextTick(cb);
}
process.nextTick(function() {
cb && cb();
});
}
};
}