Skip to content

Instantly share code, notes, and snippets.

@christianscott
Created January 7, 2019 23:30
Show Gist options
  • Save christianscott/7704e5fd174f837a4a3c88c03992eb06 to your computer and use it in GitHub Desktop.
Save christianscott/7704e5fd174f837a4a3c88c03992eb06 to your computer and use it in GitHub Desktop.
export function bufferStream(
handler: (events: Event[]) => void,
timeMs: number,
): (event: Event) => void {
let timeout: number = null;
let events: Event[] = [];
return (event: Event) => {
if (timeout != null) {
clearTimeout(timeout);
}
events.push(event);
timeout = setTimeout(() => {
handler(events);
events = [];
}, timeMs);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment