Skip to content

Commit

Permalink
add functionality for once
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhelmer committed Feb 13, 2016
1 parent 1d46bc7 commit 0c79e42
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ Uses [through2](https://github.com/rvagg/through2) as opposed to `node` core's [
## Usage

### wait(timeout)
### wait(timeout[, options])

#### timeout

> _{number}_ The delay in `ms` to pass to [setTimeout](https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_arg).
### options

#### once

> _{boolean}_ The delay will only happen for the first chunk to enter the stream; all other chunks will pass through immediately.
## Example

```js
Expand Down
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@
*
* @name wait
* @param {number} timeout - The duration to wait, in milliseconds.
* @param {object} [options] - The options to modify the behavior of wait().
* @param {boolean} [options.once] - Only wait for the very first chunk into the pipe.
* @return {stream} - The node stream for piping.
*/
function wait(timeout) {
function wait(timeout, options) {
options = options || {};
var waited = false;

return through2.obj(function(chunk, enc, callback) {
var stream = this;

setTimeout(function() {
if (!options.once || !waited) {
setTimeout(function() {
stream.push(chunk);
callback();
}, timeout || 0);

waited = true;
} else {
stream.push(chunk);
callback();
}, timeout || 0);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wait-stream",
"version": "1.1.0",
"version": "1.2.0",
"description": "Simple wait module for streams3 in node",
"repository": "justinhelmer/wait-stream",
"keywords": [
Expand Down

0 comments on commit 0c79e42

Please sign in to comment.