Skip to content

Instantly share code, notes, and snippets.

@yelouafi
Last active August 29, 2015 14:25
Show Gist options
  • Save yelouafi/4f92d43d56039281c5ee to your computer and use it in GitHub Desktop.
Save yelouafi/4f92d43d56039281c5ee to your computer and use it in GitHub Desktop.
// undefined equivalent for streams
const undef = {}
// debounce : (Stream a, () => Promise) => Stream a
Stream.prototype.debounce = function(event, last=undef) {
return this.isEmpty || this.isAbort ?
(last !== undef ? Stream.Cons(last, this) : this)
: this.isCons ?
this.tail.debounce(event, this.head)
: ( last === undef ?
Stream.Future(this.promise.then( s => s.debounce(event), Stream.Abort ))
: Stream.Future(
Promise.race([
event().then(
_ => () => Stream.Cons(last, this.debounce(event, undef)),
Stream.Abort),
this.promise.then( s => () => s.debounce(event, last), Stream.Abort )
]).then( lazy => lazy())))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment