Task Queue
Not to be confused with task queue in Aurelia 1, the TaskQueue is a sophisticated scheduler designed to prevent a variety of timing issues, memory leaks, race conditions and more bad things that tend to result from setTimeout
, setInterval
, floating promises, etc.
The benefit of using the task queue is both synchronous and asynchronous tasks are supported.
setTimeout (synchronous)
In the following example, we use the delay
configuration property to configure a task with a timeout of 100 milliseconds. This would replace using setTimeout
in your applications.
If you were to use a native setTimout
it would look like this:
Now, in your unit/integration/e2e tests or other components, you can await PLATFORM.taskQueue.yield()
to deterministically wait for the task to be done (and not a millisecond longer than needed) or even PLATFORM.taskQueue.flush()
to immediately run all queued tasks.
Result: no more flaky tests or flaky code in general. No more intermittent and hard-to-debug failures.
setTimeout (asynchronous)
We performed a synchronous equivalent of a setTimeout. Now we can go one step further and do an asynchronous setTimeout, minus the floating promises and memory leaks.
setInterval
By supply the persistent
configuration value, we can specify a task will remain and not be cleared by the task queue. This gives us setInterval
functionality.
requestAnimationFrame
By leveraging the domWriteQueue
we can also replace requestAnimationFrame
with a safer alternative.
requestAnimationFrame (loop)
In situations where requestAnimationFrame
is being used in a loop capacity (such as high frame rate animations), we can loop. The queue greatly simplifies this again with the persistent
configuration option we saw above.
Last updated
Was this helpful?