Skip to content

Commit d995350

Browse files
author
Brian Carlson
committed
Make active query a private prop
1 parent 01fadd9 commit d995350

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

packages/pg/lib/client.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Client extends EventEmitter {
4242
this._connected = false
4343
this._connectionError = false
4444
this._queryable = true
45+
this._activeQuery = null
4546

4647
this.enableChannelBinding = Boolean(c.enableChannelBinding) // set true to use SCRAM-SHA-256-PLUS when offered
4748
this.connection =
@@ -70,16 +71,31 @@ class Client extends EventEmitter {
7071
this._connectionTimeoutMillis = c.connectionTimeoutMillis || 0
7172
}
7273

74+
get activeQuery() {
75+
console.warn('Warning: Client.activeQuery is deprecated and will be removed in a future version.')
76+
return this._activeQuery
77+
}
78+
79+
set activeQuery(val) {
80+
console.warn('Warning: Client.activeQuery is deprecated and will be removed in a future version.')
81+
this._activeQuery = val
82+
}
83+
84+
_getActiveQuery() {
85+
return this._activeQuery
86+
}
87+
7388
_errorAllQueries(err) {
7489
const enqueueError = (query) => {
7590
process.nextTick(() => {
7691
query.handleError(err, this.connection)
7792
})
7893
}
7994

80-
if (this.activeQuery) {
81-
enqueueError(this.activeQuery)
82-
this.activeQuery = null
95+
const activeQuery = this._getActiveQuery()
96+
if (activeQuery) {
97+
enqueueError(activeQuery)
98+
this._activeQuery = null
8399
}
84100

85101
this.queryQueue.forEach(enqueueError)
@@ -314,8 +330,8 @@ class Client extends EventEmitter {
314330
}
315331
this.emit('connect')
316332
}
317-
const { activeQuery } = this
318-
this.activeQuery = null
333+
const activeQuery = this._getActiveQuery()
334+
this._activeQuery = null
319335
this.readyForQuery = true
320336
if (activeQuery) {
321337
activeQuery.handleReadyForQuery(this.connection)
@@ -355,67 +371,69 @@ class Client extends EventEmitter {
355371
if (this._connecting) {
356372
return this._handleErrorWhileConnecting(msg)
357373
}
358-
const activeQuery = this.activeQuery
374+
const activeQuery = this._getActiveQuery()
359375

360376
if (!activeQuery) {
361377
this._handleErrorEvent(msg)
362378
return
363379
}
364380

365-
this.activeQuery = null
381+
this._activeQuery = null
366382
activeQuery.handleError(msg, this.connection)
367383
}
368384

369385
_handleRowDescription(msg) {
370386
// delegate rowDescription to active query
371-
this.activeQuery.handleRowDescription(msg)
387+
this._getActiveQuery().handleRowDescription(msg)
372388
}
373389

374390
_handleDataRow(msg) {
375391
// delegate dataRow to active query
376-
this.activeQuery.handleDataRow(msg)
392+
this._getActiveQuery().handleDataRow(msg)
377393
}
378394

379395
_handlePortalSuspended(msg) {
380396
// delegate portalSuspended to active query
381-
this.activeQuery.handlePortalSuspended(this.connection)
397+
this._getActiveQuery().handlePortalSuspended(this.connection)
382398
}
383399

384400
_handleEmptyQuery(msg) {
385401
// delegate emptyQuery to active query
386-
this.activeQuery.handleEmptyQuery(this.connection)
402+
this._getActiveQuery().handleEmptyQuery(this.connection)
387403
}
388404

389405
_handleCommandComplete(msg) {
390-
if (this.activeQuery == null) {
406+
const activeQuery = this._getActiveQuery()
407+
if (activeQuery == null) {
391408
const error = new Error('Received unexpected commandComplete message from backend.')
392409
this._handleErrorEvent(error)
393410
return
394411
}
395412
// delegate commandComplete to active query
396-
this.activeQuery.handleCommandComplete(msg, this.connection)
413+
activeQuery.handleCommandComplete(msg, this.connection)
397414
}
398415

399416
_handleParseComplete() {
400-
if (this.activeQuery == null) {
417+
const activeQuery = this._getActiveQuery()
418+
if (activeQuery == null) {
401419
const error = new Error('Received unexpected parseComplete message from backend.')
402420
this._handleErrorEvent(error)
403421
return
404422
}
405423
// if a prepared statement has a name and properly parses
406424
// we track that its already been executed so we don't parse
407425
// it again on the same client
408-
if (this.activeQuery.name) {
409-
this.connection.parsedStatements[this.activeQuery.name] = this.activeQuery.text
426+
if (activeQuery.name) {
427+
this.connection.parsedStatements[activeQuery.name] = activeQuery.text
410428
}
411429
}
412430

413431
_handleCopyInResponse(msg) {
414-
this.activeQuery.handleCopyInResponse(this.connection)
432+
this._getActiveQuery().handleCopyInResponse(this.connection)
415433
}
416434

417435
_handleCopyData(msg) {
418-
this.activeQuery.handleCopyData(msg, this.connection)
436+
this._getActiveQuery().handleCopyData(msg, this.connection)
419437
}
420438

421439
_handleNotification(msg) {
@@ -497,21 +515,22 @@ class Client extends EventEmitter {
497515

498516
_pulseQueryQueue() {
499517
if (this.readyForQuery === true) {
500-
this.activeQuery = this.queryQueue.shift()
501-
if (this.activeQuery) {
518+
this._activeQuery = this.queryQueue.shift()
519+
const activeQuery = this._getActiveQuery()
520+
if (activeQuery) {
502521
this.readyForQuery = false
503522
this.hasExecuted = true
504523

505-
const queryError = this.activeQuery.submit(this.connection)
524+
const queryError = activeQuery.submit(this.connection)
506525
if (queryError) {
507526
process.nextTick(() => {
508-
this.activeQuery.handleError(queryError, this.connection)
527+
activeQuery.handleError(queryError, this.connection)
509528
this.readyForQuery = true
510529
this._pulseQueryQueue()
511530
})
512531
}
513532
} else if (this.hasExecuted) {
514-
this.activeQuery = null
533+
this._activeQuery = null
515534
this.emit('drain')
516535
}
517536
}
@@ -540,7 +559,7 @@ class Client extends EventEmitter {
540559
result = new this._Promise((resolve, reject) => {
541560
query.callback = (err, res) => (err ? reject(err) : resolve(res))
542561
}).catch((err) => {
543-
// replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
562+
// replace the stack trace that leads to \`TCP.onStreamRead\` with one that leads back to the
544563
// application that created the query
545564
Error.captureStackTrace(err)
546565
throw err
@@ -626,7 +645,7 @@ class Client extends EventEmitter {
626645
}
627646
}
628647

629-
if (this.activeQuery || !this._queryable) {
648+
if (this._getActiveQuery() || !this._queryable) {
630649
// if we have an active query we need to force a disconnect
631650
// on the socket - otherwise a hung query could block end forever
632651
this.connection.stream.destroy()

0 commit comments

Comments
 (0)