Last active
July 6, 2017 14:15
-
-
Save amatiasq/d4f8b715c419fe5729efee4143ea0a0e 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
class ReadableStream<T> { | |
// ... | |
debounce(milliseconds: number): ReadableStream<T[]>; | |
debounce<TOut>(milliseconds: number, join?: (list: T[]) => TOut): ReadableStream<TOut> { | |
let lastTime = Date.now(); | |
let buffer: T[] = []; | |
return new ReadableStream<TOut>((push: (value: TOut) => void) => { | |
return this.subscribe(value => { | |
const now = Date.now(); | |
buffer.push(value); | |
if (now - lastTime < milliseconds) { | |
return null; | |
} | |
const joined = join ? join(buffer) : buffer; | |
// This line doesn't compile: | |
// Argument of type 'T[] | TOut' is not assignable to parameter of type 'TOut'. | |
// Type 'T[]' is not assignable to type 'TOut'. | |
push(joined); | |
buffer = []; | |
lastTime = now; | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment