Permalink
Browse files

Timeout convenience method

  • Loading branch information...
1 parent 22453f6 commit 3d8bc682dd1f4587a7ea401ea6620007830c8d5c @peeinears committed Sep 24, 2012
Showing with 10 additions and 2 deletions.
  1. +1 −1 README.md
  2. +1 −1 examples/mexican_cuisine.js
  3. +8 −0 index.js
View
2 README.md
@@ -171,7 +171,7 @@ tp.stream('statuses/filter', params, false, function (stream) {
});
// stop the stream after 60 seconds
- setTimeout(function () { stream.end(); }, 60*1000);
+ stream.timeout(60 * 1000);
}).pipe(process.stdout); // tweet text piped to stdout
```
View
2 examples/mexican_cuisine.js
@@ -38,5 +38,5 @@ tp.stream('statuses/filter', params, false, function (stream) {
});
// stop the stream after 60 seconds
- setTimeout(function () { stream.end(); }, 60*1000);
+ stream.timeout(60 * 1000);
}).pipe(process.stdout); // tweet text piped to stdout
View
8 index.js
@@ -175,6 +175,7 @@ TweetPipe.prototype.stream = function (method, params, data_events, callback) {
var _end = filter.end;
filter.end = function (data) {
+ if (filter.timeout) clearTimeout(filter.timeout);
req.abort();
process.nextTick(function () {
if (data)
@@ -184,6 +185,13 @@ TweetPipe.prototype.stream = function (method, params, data_events, callback) {
});
};
+ // convenience method for stopping streams after duration
+ filter.timeout = function (ms) {
+ filter.timer = setTimeout(function () {
+ filter.end();
+ }, ms);
+ };
+
req.on('error', function (error) {
filter.emit('error', error);
});

0 comments on commit 3d8bc68

Please sign in to comment.