@@ -108,56 +108,21 @@ Client.prototype.connect = function(callback) {
108
108
self . secretKey = msg . secretKey ;
109
109
} ) ;
110
110
111
+
112
+ con . on ( 'readyForQuery' , function ( ) {
113
+ var activeQuery = self . activeQuery ;
114
+ self . activeQuery = null ;
115
+ self . readyForQuery = true ;
116
+ self . _pulseQueryQueue ( ) ;
117
+ if ( activeQuery ) {
118
+ activeQuery . handleReadyForQuery ( con ) ;
119
+ }
120
+ } ) ;
121
+
111
122
//hook up query handling events to connection
112
123
//after the connection initially becomes ready for queries
113
124
con . once ( 'readyForQuery' , function ( ) {
114
- self . _connecting = false ;
115
-
116
- //delegate rowDescription to active query
117
- con . on ( 'rowDescription' , function ( msg ) {
118
- self . activeQuery . handleRowDescription ( msg ) ;
119
- } ) ;
120
-
121
- //delegate dataRow to active query
122
- con . on ( 'dataRow' , function ( msg ) {
123
- self . activeQuery . handleDataRow ( msg ) ;
124
- } ) ;
125
-
126
- //delegate portalSuspended to active query
127
- con . on ( 'portalSuspended' , function ( msg ) {
128
- self . activeQuery . handlePortalSuspended ( con ) ;
129
- } ) ;
130
-
131
- //deletagate emptyQuery to active query
132
- con . on ( 'emptyQuery' , function ( msg ) {
133
- self . activeQuery . handleEmptyQuery ( con ) ;
134
- } ) ;
135
-
136
- //delegate commandComplete to active query
137
- con . on ( 'commandComplete' , function ( msg ) {
138
- self . activeQuery . handleCommandComplete ( msg , con ) ;
139
- } ) ;
140
-
141
- //if a prepared statement has a name and properly parses
142
- //we track that its already been executed so we don't parse
143
- //it again on the same client
144
- con . on ( 'parseComplete' , function ( msg ) {
145
- if ( self . activeQuery . name ) {
146
- con . parsedStatements [ self . activeQuery . name ] = true ;
147
- }
148
- } ) ;
149
-
150
- con . on ( 'copyInResponse' , function ( msg ) {
151
- self . activeQuery . handleCopyInResponse ( self . connection ) ;
152
- } ) ;
153
-
154
- con . on ( 'copyData' , function ( msg ) {
155
- self . activeQuery . handleCopyData ( msg , self . connection ) ;
156
- } ) ;
157
-
158
- con . on ( 'notification' , function ( msg ) {
159
- self . emit ( 'notification' , msg ) ;
160
- } ) ;
125
+ self . _attachEventListeners ( con )
161
126
162
127
//process possible callback argument to Client#connect
163
128
if ( callback ) {
@@ -169,15 +134,15 @@ Client.prototype.connect = function(callback) {
169
134
self . emit ( 'connect' ) ;
170
135
} ) ;
171
136
172
- con . on ( 'readyForQuery' , function ( ) {
173
- var activeQuery = self . activeQuery ;
174
- self . activeQuery = null ;
175
- self . readyForQuery = true ;
176
- self . _pulseQueryQueue ( ) ;
177
- if ( activeQuery ) {
178
- activeQuery . handleReadyForQuery ( con ) ;
179
- }
180
- } ) ;
137
+ if ( ! callback ) {
138
+ return new global . Promise ( function ( resolve , reject ) {
139
+ con . once ( 'connect' , ( ) => {
140
+ con . removeListener ( 'error' , reject )
141
+ resolve ( )
142
+ } )
143
+ con . once ( 'error' , reject )
144
+ } )
145
+ }
181
146
182
147
con . on ( 'error' , function ( error ) {
183
148
if ( this . activeQuery ) {
@@ -234,6 +199,58 @@ Client.prototype.connect = function(callback) {
234
199
235
200
} ;
236
201
202
+ // once a connection is established connect listeners
203
+ Client . prototype . _attachEventListeners = function ( con ) {
204
+ var self = this ;
205
+ self . _connecting = false ;
206
+
207
+ //delegate rowDescription to active query
208
+ con . on ( 'rowDescription' , function ( msg ) {
209
+ self . activeQuery . handleRowDescription ( msg ) ;
210
+ } ) ;
211
+
212
+ //delegate dataRow to active query
213
+ con . on ( 'dataRow' , function ( msg ) {
214
+ self . activeQuery . handleDataRow ( msg ) ;
215
+ } ) ;
216
+
217
+ //delegate portalSuspended to active query
218
+ con . on ( 'portalSuspended' , function ( msg ) {
219
+ self . activeQuery . handlePortalSuspended ( con ) ;
220
+ } ) ;
221
+
222
+ //deletagate emptyQuery to active query
223
+ con . on ( 'emptyQuery' , function ( msg ) {
224
+ self . activeQuery . handleEmptyQuery ( con ) ;
225
+ } ) ;
226
+
227
+ //delegate commandComplete to active query
228
+ con . on ( 'commandComplete' , function ( msg ) {
229
+ self . activeQuery . handleCommandComplete ( msg , con ) ;
230
+ } ) ;
231
+
232
+ //if a prepared statement has a name and properly parses
233
+ //we track that its already been executed so we don't parse
234
+ //it again on the same client
235
+ con . on ( 'parseComplete' , function ( msg ) {
236
+ if ( self . activeQuery . name ) {
237
+ con . parsedStatements [ self . activeQuery . name ] = true ;
238
+ }
239
+ } ) ;
240
+
241
+ con . on ( 'copyInResponse' , function ( msg ) {
242
+ self . activeQuery . handleCopyInResponse ( self . connection ) ;
243
+ } ) ;
244
+
245
+ con . on ( 'copyData' , function ( msg ) {
246
+ self . activeQuery . handleCopyData ( msg , self . connection ) ;
247
+ } ) ;
248
+
249
+ con . on ( 'notification' , function ( msg ) {
250
+ self . emit ( 'notification' , msg ) ;
251
+ } ) ;
252
+ }
253
+
237
254
Client . prototype . getStartupConf = function ( ) {
238
255
var params = this . connectionParameters ;
239
256
@@ -391,6 +408,10 @@ Client.prototype.end = function(cb) {
391
408
this . connection . end ( ) ;
392
409
if ( cb ) {
393
410
this . connection . once ( 'end' , cb ) ;
411
+ } else {
412
+ return new global . Promise ( ( resolve ) => {
413
+ this . connection . once ( 'end' , resolve ) ;
414
+ } ) ;
394
415
}
395
416
} ;
396
417
0 commit comments