UNPKG

asynciterator

Version:

An asynchronous iterator library for advanced object pipelines.

39 lines (38 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTaskScheduler = void 0; const resolved = Promise.resolve(undefined); // Returns a function that asynchronously schedules a task function createTaskScheduler() { // Use or create a microtask scheduler const scheduleMicrotask = typeof queueMicrotask === 'function' ? queueMicrotask : (task) => resolved.then(task); // Use or create a macrotask scheduler const scheduleMacrotask = typeof setImmediate === 'function' ? setImmediate : (task) => setTimeout(task, 0); // Interrupt with a macrotask every once in a while to avoid freezing let i = 0; let queue = null; return (task) => { // Tasks are currently being queued to avoid freezing if (queue !== null) queue.push(task); // Tasks are being scheduled normally as microtasks else if (++i < 100) scheduleMicrotask(task); // A macrotask interruption is needed else { // Hold all tasks in a queue, and reschedule them after a macrotask queue = [task]; scheduleMacrotask(() => { // Work through the queue for (const queued of queue) scheduleMicrotask(queued); queue = null; // Reset the interruption schedule i = 0; }); } }; } exports.createTaskScheduler = createTaskScheduler;