UNPKG

@studiometa/js-toolkit

Version:

A set of useful little bits of JavaScript to boost your project! 🚀

42 lines (41 loc) • 814 B
import { Queue } from "./Queue.js"; import { nextTick } from "./nextTick.js"; const LONG_TASK_DURATION = 40; class SmartQueue extends Queue { /** * A function to schedule the next batch. */ waiter = nextTick; /** * Class constructor. */ constructor() { super(Number.POSITIVE_INFINITY, nextTick); } /** * Flush current batch. */ flush() { this.run(this.tasks); this.isScheduled = false; if (this.tasks.length > 0) { this.scheduleFlush(); } } /** * Run the queue. */ run(tasks) { let task; const start = performance.now(); let now = start; while (now - start < LONG_TASK_DURATION && (task = tasks.shift())) { task(); now = performance.now(); } } } export { SmartQueue }; //# sourceMappingURL=SmartQueue.js.map