Skip to content

Commit 3a01f67

Browse files
committed
Make sure querystream "close" event is the last event emitted
1 parent 86c1214 commit 3a01f67

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

examples/refcursortoquerystream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ oracledb.getConnection(
7171
doRelease(connection);
7272
});
7373

74-
queryStream.on('end', function () {
74+
queryStream.on('close', function () {
7575
doRelease(connection);
7676
});
7777
}

examples/resultsettoquerystream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ oracledb.getConnection(
6464
doRelease(connection);
6565
});
6666

67-
queryStream.on('end', function() {
67+
queryStream.on('close', function() {
6868
doRelease(connection);
6969
});
7070
}

lib/querystream.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ function QueryStream(resultSet, oracledb) {
6464
}
6565
});
6666
}
67+
68+
self.on('end', function() {
69+
// Using setImmediate to ensure that end event handlers are processed
70+
// before the destroy logic is invoked.
71+
setImmediate(function() {
72+
self.destroy();
73+
});
74+
});
6775
}
6876

6977
util.inherits(QueryStream, Readable);
@@ -143,24 +151,9 @@ QueryStream.prototype._read = function() {
143151
if (row) {
144152
self.push(row);
145153
} else {
146-
self._close(function(err) {
147-
if (err) {
148-
self.emit('error', err);
149-
150-
return;
151-
}
152-
153-
// Pushing null will signal the end of the stream and emit the 'end'
154-
// event. This is done in the _close callback to ensure that no errors
155-
// will occur after the 'end' event.
156-
self.push(null);
157-
158-
// Using setImmediate to ensure that the 'end' event is emitted before
159-
// 'close'.
160-
setImmediate(function() {
161-
self.emit('close');
162-
});
163-
});
154+
// Pushing null will signal the end of the stream. The 'end' event will
155+
// be emitted when the streams internal buffer is flushed out.
156+
self.push(null);
164157
}
165158
});
166159
};

0 commit comments

Comments
 (0)