import type { Context } from '../context/context.js'; const BATCH_SIZE = 20; type QueuedRun = { run: () => Promise; resolve: (value: any) => void; reject: (reason?: any) => void; }; class QueryBatcher { private queue: QueuedRun[] = []; private scheduled = false; enqueue(run: () => Promise): Promise { const promise = new Promise((resolve, reject) => { this.queue.push({ run, resolve, reject }); }); if (!this.scheduled) { this.scheduled = true; queueMicrotask(this.flush); } return promise; } private flush = () => { const batch = this.queue.splice(0, BATCH_SIZE); for (const { run, resolve, reject } of batch) { run().then(resolve, reject); } if (this.queue.length) { setTimeout(this.flush, 0); } else { this.scheduled = false; } }; } const batchers = new WeakMap(); export function enqueueQueryRun( context: Context, run: () => Promise, ): Promise { let batcher = batchers.get(context); if (!batcher) { batcher = new QueryBatcher(); batchers.set(context, batcher); } return batcher.enqueue(run); }