Slowing stream down to one chunk per second

387 Views Asked by At

I have a highland stream that is reading a file line by line, and I want to slow it down to one chunk per second. I have looked through the docs, and the only functions I found were throttle() and debounce(). Both of those drop values. I need to keep all my values and just slow down the rate.

2

There are 2 best solutions below

0
Caolan On BEST ANSWER

I'd suggest mapping the chunks to delayed streams and sequencing them:

var _ = require('highland');

function delay(x) {
    return _(function (push, next) {
        setTimeout(function () {
            push(null, x);
            push(null, _.nil);
        }, 1000);
    });
}

_([1, 2, 3, 4]).map(delay).series().each(_.log);

The delay function used here seems fairly easy to generalise, so if you're interested in sending a pull request on this I'd be happy to review it :)

0
Michael Connor On

This is the same version of the function from Caolan but configurable. I made another version for myself that skips the delay on the first element.

var _ = require('highland');

function delay(delayMs) {
  return function(x) {
    return _(function(push, next) {
      return setTimeout((function() {
        push(null, x);
        return push(null, _.nil);
      }), delayMs);
    });
  };
}

_([1, 2, 3, 4]).map(delay(1000)).series().each(_.log);