From 56af9673c16fdb0f2a9fab019be1e3e78a47550a Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 31 May 2013 13:40:50 -0700 Subject: [PATCH] Avoid duplicate connecting --- lib/datasource.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index b4e31ca9..77bdfe9f 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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(); + }); } }; }