flyd icon indicating copy to clipboard operation
flyd copied to clipboard

Throttle module

Open jimf opened this issue 9 years ago • 7 comments

Howdy. I'd be interested in adding a throttle module, but had a few questions before doing so. As for implementation, I was thinking something along the lines of:

var flyd = require('flyd');
var _throttle = require('lodash.throttle');

module.exports = flyd.curryN(2, throttle(ms, s) {
  return flyd.combine(_throttle(function(s, self) {
    self(s());
  }, ms), [s]);
});

My questions:

  1. Can this more or less already be achieved with what's here, and I'm just missing it?
  2. I'm fairly new to this reactive stuff. Is this implementation more or less analogous with throttle from RxJS et al (of course, sans schedulers and what not)?
  3. This would add a dependency on lodash.throttle. As such, do you have a preference on this module living here vs a dedicated repo?

jimf avatar Jan 18 '16 21:01 jimf

Is what you're requesting similar to #78 by @ccorcos?

paldepind avatar Jan 23 '16 09:01 paldepind

No, it'd be just like the throttle from RxJS or Bacon: basically, take a stream, and return a new stream that throttles results from the stream by a given delay. Useful for scroll events and such, that are far more noisy than is typically needed.

Example:

var throttledScrollStream = flyd.throttle(250, scrollStream);

jimf avatar Jan 23 '16 14:01 jimf

The only suspect feeling I have is this is strictly using an external library wrapping a combine -- I understand wanting to present modules that provide functionality like Kefir or Bacon or Rx's APIs do (sampleBy, debounce, throttle, skipWhile, bufferWhile, ...), but does it make sense to have modules like this one as-is?

c-dante avatar Jan 25 '16 18:01 c-dante

Also debounce, delay. Those things are massively useful for tuning asynchronous flows.

garkin avatar Nov 15 '16 03:11 garkin

Couldn't find delay, and debounce won't do anything at all while stuff is happening.

janat08 avatar Feb 28 '19 22:02 janat08

Both are fairly easily implemented. Feel free to take these and use them.

export const debounce = ms => s => {
  var timeout
  return flyd.combine((s,self)=> {
    clearTimeout(timeout)
    timeout = setTimeout(()=> self(s.val), ms)
  }, [s])
}

export const delay = ms => s => {
  return flyd.combine((s, self)=> {
    setTimeout(()=> self(s.val), ms)
  }, [s])
}

Usage:

const s = stream()

const d = s.pipe(delay(2))

s(1)

// s: 1---
// d: --1-

const s = stream()
const d = s.pipe(debounce(2))
s(1)
s(2)

// s: 12---
// d: ----2

nordfjord avatar Mar 06 '19 13:03 nordfjord

I meant something that starts immediately: s: 1111 d: 1-1-

janat08 avatar Mar 08 '19 05:03 janat08