Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 1x 1x 8x 8x 8x 36x 36x 12x 11x 15x 15x 15x 1x 1x 15x 11x 12x 12x 16x 36x 8x 1x 1x 1x 8x 8x | import { captureStack } from '../exception.js';
/**
* Type definition for a batch handler function.
* @returns {void}
*/
export type BatchHandler = () => void;
/**
* Type definition for a batch scheduler function.
* @param {BatchHandler} fn - The function to be scheduled for batch execution.
* @returns {void}
*/
export type BatchScheduler = (fn: BatchHandler) => void;
/**
* Type definition for a batch resetter function.
* @returns {void}
*/
export type BatchResetter = () => void;
export type MicroBatch = [BatchScheduler, BatchResetter];
/**
* Creates a micro-batch scheduler that executes functions in batches after a specified delay.
*
* @param {number} delay - The delay in milliseconds before executing the batch. Defaults to 10ms.
* @returns {MicroBatch} A tuple containing the scheduler and resetter functions.
*/
export function microbatch(delay: number = 10): MicroBatch {
const BATCHES = new Set<() => void>();
let activeId: number | undefined = undefined;
const schedule = (fn: () => void) => {
if (BATCHES.has(fn)) return;
if (!BATCHES.size) {
activeId = setTimeout(() => {
for (const handler of BATCHES) {
try {
handler();
} catch (error) {
captureStack.error.external('Batch execution failed.', error as Error);
}
}
BATCHES.clear();
}, delay) as never;
}
BATCHES.add(fn);
};
const reset = () => {
BATCHES.clear();
clearTimeout(activeId);
};
return [schedule, reset];
}
|