Last active
August 29, 2015 14:25
-
-
Save yelouafi/4f92d43d56039281c5ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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