@@ -21,14 +21,15 @@ module.exports = Connection;function Connection(options = {}) {
21
21
let timer
22
22
let id = 1
23
23
let ended
24
+ let ready = false
24
25
25
26
const queries = Queue ( )
26
27
, statements = new Map ( )
27
- , connection = { send, end, destroy, ready : false , active : false }
28
+ , connection = { send, end, destroy }
28
29
29
30
const socket = postgresSocket ( options , {
31
+ ready : ( ) => socket . write ( frontend . connect ( options ) ) ,
30
32
data,
31
- ready,
32
33
error,
33
34
close
34
35
} )
@@ -60,13 +61,13 @@ module.exports = Connection;function Connection(options = {}) {
60
61
function resolve ( x ) {
61
62
backend . query . resolve ( x )
62
63
backend . query = null
63
- timeout && connection . active && queries . length === 0 && idle ( )
64
+ timeout && queries . length === 0 && idle ( )
64
65
}
65
66
66
67
function reject ( err ) {
67
68
backend . query ? backend . query . reject ( err ) : error ( err )
68
69
backend . query = null
69
- timeout && connection . active && queries . length === 0 && idle ( )
70
+ timeout && queries . length === 0 && idle ( )
70
71
}
71
72
72
73
function end ( ) {
@@ -96,19 +97,21 @@ module.exports = Connection;function Connection(options = {}) {
96
97
query . result = [ ]
97
98
query . result . count = null
98
99
timeout && clearTimeout ( timer )
99
- ! connection . ready || backend . query
100
- ? queries . push ( query )
101
- : ( backend . query = query )
102
100
103
101
const buffer = query . simple
104
102
? simple ( str , query )
105
103
: statements . has ( sig )
106
104
? prepared ( statements . get ( sig ) , args , query )
107
105
: prepare ( sig , str , args , query )
108
106
109
- connection . ready
110
- ? socket . write ( buffer )
111
- : ( messages . push ( buffer ) , socket . connect ( ) )
107
+ if ( ready && ! backend . query ) {
108
+ backend . query = query
109
+ socket . write ( buffer )
110
+ } else {
111
+ queries . push ( query )
112
+ messages . push ( buffer )
113
+ ! ready && socket . connect ( )
114
+ }
112
115
}
113
116
114
117
function simple ( str , query ) {
@@ -135,18 +138,17 @@ module.exports = Connection;function Connection(options = {}) {
135
138
}
136
139
137
140
function onready ( ) {
138
- if ( ! backend . query )
139
- backend . query = queries . shift ( )
140
-
141
141
if ( ! backend . query && queries . length === 0 && ended )
142
142
return ended ( )
143
143
144
- if ( ! connection . ready ) {
144
+ if ( ! ready ) {
145
145
messages . forEach ( socket . write )
146
146
messages = [ ]
147
- connection . ready = true
148
- connection . onconnect && connection . onconnect ( )
147
+ ready = true
149
148
}
149
+
150
+ if ( ! backend . query )
151
+ backend . query = queries . shift ( )
150
152
}
151
153
152
154
function data ( x ) {
@@ -164,14 +166,10 @@ module.exports = Connection;function Connection(options = {}) {
164
166
}
165
167
}
166
168
167
- function ready ( ) {
168
- socket . write ( frontend . connect ( options ) )
169
- }
170
-
171
169
function close ( ) {
172
170
error ( errors . connection ( 'CLOSED' , options ) )
173
171
statements . clear ( )
174
- connection . ready = connection . active = false
172
+ ready = false
175
173
}
176
174
177
175
function unknown ( ) {
@@ -196,7 +194,7 @@ function postgresSocket(options, {
196
194
return Promise . resolve ( )
197
195
} ,
198
196
end : ( ) => {
199
- return new Promise ( r => socket . end ( r ) )
197
+ return new Promise ( r => socket && socket . end ( r ) )
200
198
} ,
201
199
connect
202
200
}
0 commit comments