Skip to content

Commit b0f7958

Browse files
committed
Fix hanging listener when error occured
1 parent 1cdad4d commit b0f7958

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

index.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,23 @@ function Cursor (text, values, config) {
1919
this._cb = null
2020
this._rows = null
2121
this._portal = null
22+
this._ifNoData = this._ifNoData.bind(this)
23+
this._rowDescription = this._rowDescription.bind(this)
2224
}
2325

2426
util.inherits(Cursor, EventEmitter)
2527

28+
Cursor.prototype._ifNoData = function () {
29+
this.state = 'idle'
30+
this._shiftQueue()
31+
}
32+
33+
Cursor.prototype._rowDescription = function () {
34+
if (this.connection) {
35+
this.connection.removeListener('noData', this._ifNoData)
36+
}
37+
}
38+
2639
Cursor.prototype.submit = function (connection) {
2740
this.connection = connection
2841
this._portal = 'C_' + (nextUniqueID++)
@@ -45,19 +58,12 @@ Cursor.prototype.submit = function (connection) {
4558

4659
con.flush()
4760

48-
const ifNoData = () => {
49-
this.state = 'idle'
50-
this._shiftQueue()
51-
}
52-
5361
if (this._conf.types) {
5462
this._result._getTypeParser = this._conf.types.getTypeParser
5563
}
5664

57-
con.once('noData', ifNoData)
58-
con.once('rowDescription', () => {
59-
con.removeListener('noData', ifNoData)
60-
})
65+
con.once('noData', this._ifNoData)
66+
con.once('rowDescription', this._rowDescription)
6167
}
6268

6369
Cursor.prototype._shiftQueue = function () {
@@ -114,6 +120,8 @@ Cursor.prototype.handleEmptyQuery = function () {
114120
}
115121

116122
Cursor.prototype.handleError = function (msg) {
123+
this.connection.removeListener('noData', this._ifNoData)
124+
this.connection.removeListener('rowDescription', this._rowDescription)
117125
this.state = 'error'
118126
this._error = msg
119127
// satisfy any waiting callback

0 commit comments

Comments
 (0)