@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
35 lines • 763 B
JavaScript
/**
* Streams the latest chunk in intervals of `ms`.
*
* @group Transformers
* @example
* ```
* --1--T40----2--T20--|
* sampleTime(20)
* -T20-1-T20-1-T20-2--|
* ```
*/
export function sampleTime(ms) {
let buffer;
let interval;
let hasSample = false;
return new TransformStream({
start(controller) {
interval = setInterval(() => {
if (hasSample)
controller.enqueue(buffer);
}, ms);
},
transform(chunk) {
hasSample = true;
buffer = chunk;
},
flush() {
clearInterval(interval);
},
cancel() {
clearInterval(interval);
},
});
}
//# sourceMappingURL=sampleTime.js.map