Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active July 6, 2017 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amatiasq/d4f8b715c419fe5729efee4143ea0a0e to your computer and use it in GitHub Desktop.
Save amatiasq/d4f8b715c419fe5729efee4143ea0a0e to your computer and use it in GitHub Desktop.
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