Skip to content

Commit

Permalink
Fix hanging listener when error occured
Browse files Browse the repository at this point in the history
  • Loading branch information
juneidy committed Jun 27, 2019
1 parent 1cdad4d commit b0f7958
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@ function Cursor (text, values, config) {
this._cb = null
this._rows = null
this._portal = null
this._ifNoData = this._ifNoData.bind(this)
this._rowDescription = this._rowDescription.bind(this)
}

util.inherits(Cursor, EventEmitter)

Cursor.prototype._ifNoData = function () {
this.state = 'idle'
this._shiftQueue()
}

Cursor.prototype._rowDescription = function () {
if (this.connection) {
this.connection.removeListener('noData', this._ifNoData)
}
}

Cursor.prototype.submit = function (connection) {
this.connection = connection
this._portal = 'C_' + (nextUniqueID++)
Expand All @@ -45,19 +58,12 @@ Cursor.prototype.submit = function (connection) {

con.flush()

const ifNoData = () => {
this.state = 'idle'
this._shiftQueue()
}

if (this._conf.types) {
this._result._getTypeParser = this._conf.types.getTypeParser
}

con.once('noData', ifNoData)
con.once('rowDescription', () => {
con.removeListener('noData', ifNoData)
})
con.once('noData', this._ifNoData)
con.once('rowDescription', this._rowDescription)
}

Cursor.prototype._shiftQueue = function () {
Expand Down Expand Up @@ -114,6 +120,8 @@ Cursor.prototype.handleEmptyQuery = function () {
}

Cursor.prototype.handleError = function (msg) {
this.connection.removeListener('noData', this._ifNoData)
this.connection.removeListener('rowDescription', this._rowDescription)
this.state = 'error'
this._error = msg
// satisfy any waiting callback
Expand Down

0 comments on commit b0f7958

Please sign in to comment.