Open jimf opened 8 years ago
Is what you're requesting similar to #78 by @ccorcos?
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);
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?
Also debounce
, delay
. Those things are massively useful for tuning asynchronous flows.
Couldn't find delay, and debounce won't do anything at all while stuff is happening.
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
I meant something that starts immediately: s: 1111 d: 1-1-
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:
My questions:
throttle
from RxJS et al (of course, sans schedulers and what not)?