From 0732ee215f7e896c50dda3d521e1abe0d6c4ca0f Mon Sep 17 00:00:00 2001 From: brianc Date: Fri, 4 Feb 2011 20:03:41 -0600 Subject: [PATCH] readability refactoring --- lib/client.js | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/client.js b/lib/client.js index 7c865a62..6db8e904 100644 --- a/lib/client.js +++ b/lib/client.js @@ -66,31 +66,24 @@ p.connect = function() { con.password(md5password); }); + //hook up query handling events to connection + //after the connection initially becomes ready for queries con.once('readyForQuery', function() { - //hook up query handling events to connection - //after the connection initially becomes ready for queries - var ready = function() { - if(self.activeQuery) { - self.activeQuery.handleReadyForQuery(); - } - self.readyForQuery = true; - this.activeQuery = null; - self.pulseQueryQueue(); - }; - + //delegate row descript to active query con.on('rowDescription', function(msg) { self.activeQuery.handleRowDescription(msg); }); - + //delegate datarow to active query con.on('dataRow', function(msg) { self.activeQuery.handleDataRow(msg); }); - + //TODO should query gain access to connection? con.on('portalSuspended', function(msg) { self.activeQuery.getRows(con); }); con.on('commandComplete', function(msg) { + //delegate command complete to query self.activeQuery.handleCommandComplete(msg); //need to sync after each command complete of a prepared statement if(self.activeQuery.isPreparedStatement) { @@ -98,12 +91,15 @@ p.connect = function() { } }); - con.on('readyForQuery', function() { - ready(); - }); - - ready(); + }); + con.on('readyForQuery', function() { + if(self.activeQuery) { + self.activeQuery.handleReadyForQuery(); + } + this.activeQuery = null; + self.readyForQuery = true; + self.pulseQueryQueue(); }); con.on('error', function(error) { @@ -122,12 +118,11 @@ p.connect = function() { p.pulseQueryQueue = function() { if(this.readyForQuery===true) { - if(this.queryQueue.length > 0) { + this.activeQuery = this.queryQueue.shift(); + if(this.activeQuery) { this.readyForQuery = false; - var query = this.queryQueue.shift(); - this.activeQuery = query; this.hasExecuted = true; - query.submit(this.connection); + this.activeQuery.submit(this.connection); } else if(this.hasExecuted) { this.activeQuery = null; this.emit('drain')