Skip to content

Commit

Permalink
Merge pull request #762 from stephenplusplus/spp--streamrouter-early-…
Browse files Browse the repository at this point in the history
…exit

core: use is-stream-ended
  • Loading branch information
callmehiphop committed Jul 30, 2015
2 parents e4fdb2d + 0f7c0ca commit e6da167
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/common/stream-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'use strict';

var concat = require('concat-stream');
var isStreamEnded = require('is-stream-ended');
var streamEvents = require('stream-events');
var through = require('through2');

Expand Down Expand Up @@ -181,17 +182,9 @@ streamRouter.runAsStream_ = function(parsedArguments, originalMethod) {

var stream = streamEvents(through.obj());

// Results from the API are split apart for the user. If 50 results are
// returned, we emit 50 data events. While the user is consuming these, they
// might choose to end the stream early by calling ".end()". We keep track of
// this state to prevent pushing more results to the stream, ending it again,
// or making unnecessary API calls.
var streamEnded = false;
var _end = stream.end;
stream.end = function() {
streamEnded = true;
_end.apply(this, arguments);
};
function shouldPushResult() {
return resultsToSend !== 0 && !isStreamEnded(stream);
}

function onResultSet(err, results, nextQuery) {
if (err) {
Expand All @@ -201,12 +194,12 @@ streamRouter.runAsStream_ = function(parsedArguments, originalMethod) {
}

var result;
while ((result = results.shift()) && resultsToSend !== 0 && !streamEnded) {
while ((result = results.shift()) && shouldPushResult()) {
stream.push(result);
resultsToSend--;
}

if (streamEnded) {
if (isStreamEnded(stream)) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"duplexify": "^3.2.0",
"extend": "^2.0.0",
"google-auth-library": "^0.9.4",
"is-stream-ended": "^0.1.0",
"mime-types": "^2.0.8",
"node-uuid": "^1.4.2",
"once": "^1.3.1",
Expand Down

0 comments on commit e6da167

Please sign in to comment.