Skip to content

Commit

Permalink
Make sure querystream "close" event is the last event emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbj committed Feb 6, 2018
1 parent 86c1214 commit 3a01f67
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/refcursortoquerystream.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ oracledb.getConnection(
doRelease(connection);
});

queryStream.on('end', function () {
queryStream.on('close', function () {
doRelease(connection);
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/resultsettoquerystream.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ oracledb.getConnection(
doRelease(connection);
});

queryStream.on('end', function() {
queryStream.on('close', function() {
doRelease(connection);
});
}
Expand Down
29 changes: 11 additions & 18 deletions lib/querystream.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ function QueryStream(resultSet, oracledb) {
}
});
}

self.on('end', function() {
// Using setImmediate to ensure that end event handlers are processed
// before the destroy logic is invoked.
setImmediate(function() {
self.destroy();
});
});
}

util.inherits(QueryStream, Readable);
Expand Down Expand Up @@ -143,24 +151,9 @@ QueryStream.prototype._read = function() {
if (row) {
self.push(row);
} else {
self._close(function(err) {
if (err) {
self.emit('error', err);

return;
}

// Pushing null will signal the end of the stream and emit the 'end'
// event. This is done in the _close callback to ensure that no errors
// will occur after the 'end' event.
self.push(null);

// Using setImmediate to ensure that the 'end' event is emitted before
// 'close'.
setImmediate(function() {
self.emit('close');
});
});
// Pushing null will signal the end of the stream. The 'end' event will
// be emitted when the streams internal buffer is flushed out.
self.push(null);
}
});
};
Expand Down

0 comments on commit 3a01f67

Please sign in to comment.