MCPcopy Create free account
hub / github.com/stackblitz/bolt.new / bufferWatchEvents

Function bufferWatchEvents

app/utils/buffer.ts:1–29  ·  view source on GitHub ↗
(timeInMs: number, cb: (events: T[]) => unknown)

Source from the content-addressed store, hash-verified

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 it
6 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 order
11 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}

Callers 1

#initMethod · 0.90

Calls 1

scheduleBufferTickFunction · 0.85

Tested by

no test coverage detected