CoolFace
Apppublic

legends810/testingnew

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
buffer.ts30 linesDownload Raw Back to utils
1export function bufferWatchEvents<T extends unknown[]>(timeInMs: number, cb: (events: T[]) => unknown) {2  let timeoutId: number | undefined;3  let events: T[] = [];4 5  // keep track of the processing of the previous batch so we can wait for it6  let processing: Promise<unknown> = Promise.resolve();7 8  const scheduleBufferTick = () => {9    timeoutId = self.setTimeout(async () => {10      // we wait until the previous batch is entirely processed so events are processed in order11      await processing;12 13      if (events.length > 0) {14        processing = Promise.resolve(cb(events));15      }16 17      timeoutId = undefined;18      events = [];19    }, timeInMs);20  };21 22  return (...args: T) => {23    events.push(args);24 25    if (!timeoutId) {26      scheduleBufferTick();27    }28  };29}30