Skip to content

Commit

Permalink
Fixed end event incompatibility with any-db spec
Browse files Browse the repository at this point in the history
Now `end` is not emitted if there was no callback or `data` event
handler set on query. This makes adapter pass 56/58 any-db adapter spec
tests. The last two are about missing `pause()` and `resume()` methods,
which should come from `stream.Readable`, but Tedious Request is not a
stream, so there's no clean way to implement that (except for
pseudo-stream emulation, which would be kinda ugly and could confuse
developers into thinking that query can be paused, without taking up
memory for all rows).
  • Loading branch information
Hypermediaisobar-admin committed Sep 9, 2014
1 parent 7acd990 commit a2c981e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,17 @@ var execQuery = function(query, parameters, callback) {
query.callback(err, query._resultSet);
}

query.emit('end');
// Do not emit `end` event if there was no one listening.
// TODO: this is strange requirement, but it comes from emulating stream.Readable,
// which does not emit `end` untill all data is consumed. If data was not consumed,
// i.e., no callback and no `data` event are used, there should be no `end`.
// This is most probably wrong way, because we're still closing the query
// (remember: Tedious query is not a stream waiting for read - it finishes anyway).
// We should change this once Tedious query is instance of `stream.Readable`, or
// can be paused and resumed.
if (query.callback || EventEmitter.listenerCount(query, 'data')) {
query.emit('end');
}
});

if (query.values) {
Expand Down Expand Up @@ -539,13 +549,13 @@ var makeQueryable = function(target) {

if (query) {
target._waitingForQueryToFinish = true;
target.emit('query', query);

query.once('end', function(){
query.once('close', function(){
target._waitingForQueryToFinish = false;
target.execNextInQueue();
});

target.emit('query', query);
target.execSql(query._request);
}
};
Expand Down

0 comments on commit a2c981e

Please sign in to comment.