-
-
Notifications
You must be signed in to change notification settings - Fork 0
ListQueue
ListQueue is a list-based queue of microtasks that extends the MicroTaskQueue class. It provides concrete implementations for managing tasks using a list data structure, allowing for efficient task scheduling, execution, pausing, and resuming.
Usually ListQueue is used as a base class for other classes that implement specific task processing logic.
- List-Based Task Management: Uses a list data structure to store and manage microtasks.
- Queue Control: Provides methods to pause, resume, and clear the queue.
- Extensible Design: Designed to be extended by subclasses for specialized queue implementations.
ListQueue is available as a named and default export.
const queue = new ListQueue(paused);-
paused: A boolean indicating whether the queue should start in a paused state.
-
paused: A boolean indicating whether the queue is currently paused (inherited fromMicroTaskQueue). -
stopQueue: A function ornullthat is used to stop the queue when paused. It's set by thestartQueuemethod. This property is used internally bypause()andresume().
-
isEmpty: A boolean indicating whether the queue is empty (overrides the getter fromMicroTaskQueue).
-
pause(): Pauses the queue, preventing tasks from being executed. If the queue is running, it calls thestopQueuefunction to halt execution. Returns the queue instance. -
resume(): Resumes the queue, allowing tasks to be executed again. If the queue is not empty, it calls thestartQueuemethod to begin execution. Returns the queue instance. -
enqueue(fn): Adds a new task to the queue, wrapping the provided function in aMicroTaskinstance and adding it to the list. If the queue is not paused and not already running, it starts the queue. Returns the task instance. -
dequeue(task): Removes a specified task from the queue by removing it from the list. If the queue becomes empty and is not paused, it stops the queue. Returns the queue instance.-
task.cancel()is called when the task is removed from the queue.
-
-
schedule(fn): Similar toenqueue(), but returns a task instance with a non-nullpromiseproperty that resolves when the task is executed.- When the task is executed, the promise is resolved with the return value of
fn(), or rejected with the error thrown byfn(), if any.
- When the task is executed, the promise is resolved with the return value of
-
clear(): Clears all tasks from the queue by clearing the list. If the queue was not paused, it pauses it before clearing and resumes it afterward. Returns the queue instance.-
task.cancel()is called for all tasks in the queue.
-
-
startQueue(): A method meant to be overridden by subclasses to implement queue execution logic. Returns a function that stops the queue ornull.
fn is a function that will be executed when the task is scheduled. It will be called with queue-specific arguments, and its return value will be ignored
by enqueue(), but it can be used by schedule() to resolve the task's promise.
task is an instance of the MicroTask class. It is created by the enqueue() method when a new task is added to the queue.
To use the ListQueue class, import it into your project and create an instance:
import ListQueue from 'time-queues/ListQueue.js';
const queue = new ListQueue();
queue.enqueue(() => console.log('Task 1 executed'));
queue.enqueue(() => console.log('Task 2 executed'));
queue.pause(); // Pause the queue
queue.resume(); // Resume the queue
queue.clear(); // Clear all tasksNote that ListQueue is designed to be a base class for other list-based queues. The startQueue() method should be overridden in subclasses to implement specific queue execution logic.
-
Queue Not Starting: Ensure that the
startQueue()method is properly implemented in your subclass if you're extendingListQueue. -
Tasks Not Executing: Check if the queue is paused. If it is, call
resume()to start task execution. -
Unexpected Queue Behavior: Verify that the
stopQueuefunction is properly cleaning up resources when the queue is paused or cleared.
- MicroTaskQueue - Parent class
- IdleQueue - Browser idle queue (extends ListQueue)
- FrameQueue - Animation frame queue (extends ListQueue)
- Scheduler - Time-based scheduler (extends MicroTaskQueue)
- LimitedQueue - Concurrency-controlled queue (extends ListQueue)