Task Queue
Not to be confused with the task queue in Aurelia 1, the TaskQueue in Aurelia is an advanced scheduler designed to handle synchronous and asynchronous tasks efficiently. It provides a robust solution to common issues like timing problems, memory leaks, and race conditions often arising from traditional JavaScript timing functions like setTimeout
, setInterval
, and unmanaged promises.
Benefits of Using the Task Queue
Improved Performance: By managing tasks more efficiently, the TaskQueue enhances the performance of applications.
Synchronous and Asynchronous Support: It supports both synchronous and asynchronous tasks, providing greater flexibility in handling tasks.
Deterministic Task Execution: Facilitates testing by providing deterministic ways to wait for task completion, reducing test flakiness.
Avoids Common Pitfalls: Helps avoid common issues associated with
setTimeout
andsetInterval
, such as memory leaks and race conditions.
We highly recommend using the task queue to replace existing uses of setTimeout
and setInterval
for better-performing applications.
Examples and Use Cases
Replacing setTimeout
(Synchronous)
setTimeout
(Synchronous)Instead of `setTimeout, ' the TaskQueue offers a more reliable way to queue tasks without delay.
If you were to use a native setTimout
, it would look like this:
Advantages Over setTimeout
setTimeout
*Testability: You can await
PLATFORM.taskQueue.yield()
or usePLATFORM.taskQueue.flush()
in tests for predictable task execution.Improved Reliability: Reduces the chances of intermittent and hard-to-debug failures.
setTimeout (asynchronous)
For asynchronous operations, the TaskQueue can handle tasks without the issues of floating promises.
Implementing setInterval
The TaskQueue can mimic setInterval
functionality, offering more control and reliability.
Replacing requestAnimationFrame
For tasks that need to synchronize with the browser's repaint, domWriteQueue
is a safer alternative to requestAnimationFrame
.
Animation Loop with requestAnimationFrame
For continuous animations, the TaskQueue can be used to create a loop, similar to requestAnimationFrame
.
Last updated