debounce-stream
Advanced tools
Comparing version 1.0.1 to 2.0.0
88
index.js
@@ -1,81 +0,17 @@ | ||
var Transform = require("stream").Transform; | ||
var through = require('through') | ||
, duplexer = require('duplexer') | ||
, debounce = require('debounce') | ||
module.exports = DebounceStream; | ||
module.exports = debounceStream | ||
/** | ||
* Creates a stream that only outputs data once in a given interval | ||
* @param {Number} interval The interval in milliseconds between data output | ||
* @param {Object} options Optional options to pass to the stream constructor | ||
*/ | ||
function DebounceStream(interval, options) { | ||
if (!interval || !(interval > 0)) // Ensure interval is a number that is > 0 | ||
throw new Error("Must specify an interval greater than 0 milliseconds") | ||
options = options || {}; // Specify empty object for options by default | ||
function debounceStream(_ms, immediate) { | ||
var ms = _ms || 100 | ||
, input = through(debounce(write, ms, immediate)) | ||
, output = through() | ||
// Construct the transform stream | ||
var stream = new Transform(options); | ||
return duplexer(input, output) | ||
// Track the reference returned by setTimeout | ||
var timeoutId = null; | ||
// Whether the state is paused or not | ||
var paused = false; | ||
// The last value sent down the stream | ||
var last_value = null; | ||
// Whether there was new data since the last time something was emitted | ||
var has_value = false; | ||
// Define the transform function | ||
stream._transform = function (data, encoding, callback) { | ||
// Save the new chunk of data as the last value | ||
last_value = data; | ||
// Flag that there is a value to output | ||
has_value = true; | ||
// If paused just ignore outputting it for now | ||
if (paused) return callback(); | ||
// If we aren't paused, say that there is no value | ||
has_value = false; | ||
// Pause | ||
pause(); | ||
// Pass the data down the stream | ||
callback(null, last_value); | ||
} | ||
// When the stream ends, clear any pause timeout and pass the last value | ||
stream._flush = function (callback) { | ||
clearTimeout(timeoutId); | ||
if (has_value) this.push(last_value); | ||
callback(); | ||
} | ||
// Function for pausing output | ||
function pause() { | ||
// Set a flag | ||
paused = true; | ||
// Clear any previous timeout, just in case | ||
clearTimeout(timeoutId); | ||
// Set a new timeout to unpause after the given interval | ||
timeoutId = setTimeout(unpause, interval); | ||
} | ||
// Function for unpausing and outputting the last value if it exists | ||
function unpause() { | ||
// Unpause | ||
paused = false; | ||
// If a new value has come in since we've paused | ||
if (has_value) { | ||
// Push that value down the stream | ||
has_value = false; | ||
stream.push(last_value); | ||
// Pause again | ||
pause(); | ||
} | ||
} | ||
return stream | ||
function write(data) { | ||
output.queue(data) | ||
} | ||
} |
{ | ||
"name": "debounce-stream", | ||
"version": "1.0.1", | ||
"description": "A stream that only forwards input once in a given interval", | ||
"version": "2.0.0", | ||
"description": "Debounces a stream.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "tape test.js && browserify test.js | jsdom-eval" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/RangerMauve/debounce-stream.git" | ||
"url": "https://github.com/fardog/debounce-stream.git" | ||
}, | ||
"keywords": [ | ||
"stream", | ||
"debounce", | ||
"rate", | ||
"flow", | ||
"limit", | ||
"time" | ||
"debounce" | ||
], | ||
"author": "RangerMauve", | ||
"author": "Nathan Wittstock <nate@milkandtang.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/RangerMauve/debounce-stream/issues" | ||
"url": "https://github.com/fardog/debounce-stream/issues" | ||
}, | ||
"homepage": "https://github.com/RangerMauve/debounce-stream", | ||
"homepage": "https://github.com/fardog/debounce-stream", | ||
"dependencies": { | ||
"debounce": "^1.0.0", | ||
"duplexer": "^0.1.1", | ||
"through": "^2.3.6" | ||
}, | ||
"devDependencies": { | ||
"interval-stream": "^0.2.0", | ||
"stdout": "0.0.3", | ||
"stream-array": "^1.0.1" | ||
"browserify": "^8.1.3", | ||
"jsdom-eval": "^0.2.5", | ||
"tape": "^3.4.0" | ||
} | ||
} |
# debounce-stream | ||
This stream takes in events of data and only passes the event on once in a given interval. | ||
Once the interval is over, it outputs the last value that was passed in | ||
Debounces a stream. | ||
## Installing | ||
[![Build Status](http://img.shields.io/travis/fardog/debounce-stream/master.svg?style=flat)](https://travis-ci.org/fardog/debounce-stream) | ||
[![npm install](http://img.shields.io/npm/dm/debounce-stream.svg?style=flat)](https://www.npmjs.org/package/debounce-stream) | ||
``` bash | ||
npm install --save debounce-stream | ||
## Example | ||
```javascript | ||
var events = require('dom-delegation-stream') | ||
, values = require('dom-value-object-stream') | ||
, debounce = require('debounce-stream') | ||
events(document.querySelector('[rel=inputs]'), 'input') | ||
.pipe(values()) // values is a stream that progressively updates on user input | ||
.pipe(debounce(500)) // emit only after a pause of 500 ms | ||
.on('data', function(data) { | ||
console.log(data) // the data after a user has stopped typing for 500 ms | ||
}) | ||
``` | ||
@@ -14,29 +26,22 @@ | ||
### `DebounceStream(interval,[options])` | ||
- `debounce([milliseconds] [, immediate])` - Create a new debounce duplex stream | ||
- `milliseconds` - Integer. The number of milliseconds to debounce the | ||
stream. Defaults to `100` | ||
- `immediate` - Boolean. If `true`, the debounced function will call | ||
immediately, rather than at the end of input. Default is `false`. | ||
#### Arguments | ||
* `interval` (Number): Interval for how frequently to output data | ||
* `[options]` (Object): Options that get passed to a stream constructor (In case you want `objectMode`) | ||
Returns a duplex stream that accepts values on one end, and emits the debounced | ||
values on the other. | ||
#### Returns | ||
(DuplexStream): Pipe stuff into it, and it'll come out debounced | ||
## Notes | ||
## Example | ||
This module which was previously known as `stream-debounce` is now known as | ||
`debounce-stream`. Thanks to [RangerMauve][rangermauve] for allowing this | ||
module to take over that name! The version was bumped to v2.0.0 for the name | ||
change, and to ensure compatibility for users of the old version. | ||
``` javascript | ||
var DebounceStream = require("debounce-stream"); | ||
var IntervalStream = require("interval-stream"); | ||
var StreamArray = require("stream-array"); | ||
var stdout = require("stdout")(); | ||
## License | ||
// Data to output | ||
var data = ["foo\n", "bar\n", "bazz\n", "fizz\n"]; | ||
MIT. See [LICENSE](./LICENSE) for details. | ||
// Duplicate it twice so we have more data to send | ||
data = data.concat(data).concat(data); | ||
StreamArray(data) // Should take all the data in the array, | ||
.pipe(IntervalStream(1000)) // Send out one item every second, | ||
.pipe(DebounceStream(2000)) // Ignore about every other chunk | ||
.pipe(stdout); // Output to the console | ||
``` | ||
[rangermauve]: https://github.com/RangerMauve |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
5592
8
72
0
47
3
1
+ Addeddebounce@^1.0.0
+ Addedduplexer@^0.1.1
+ Addedthrough@^2.3.6
+ Addeddebounce@1.2.1(transitive)
+ Addedduplexer@0.1.2(transitive)
+ Addedthrough@2.3.8(transitive)