
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
chiffchaff-multi
Advanced tools
Multi-task execution for chiffchaff.
Note: Like chiffchaff itself, this example is written in ES2015. Please familiarize yourself with ES2015 before tackling it.
Let's download a couple of files in parallel. Assuming we already have a
DownloadTask
class, let's
create a few of those:
const urls = [
'http://media.w3.org/2010/05/sintel/trailer.mp4',
'http://media.w3.org/2010/05/bunny/trailer.mp4',
'http://media.w3.org/2010/05/bunny/movie.mp4',
'http://media.w3.org/2010/05/video/movie_300.webm'
]
const downloadTasks = urls.map((url) => new DownloadTask(url))
Now, we could start()
those tasks individually, but that's not what we're
after. Using MultiTask
, we can create a task that downloads all four files,
with varying settings.
By default, MultiTask
will execute the provided tasks one by one. Thus, the
following task will download the files sequentially:
const sequentialDownloadTask = new MultiTask(downloadTasks)
The concurrency
option lets us run multiple tasks in parallel. If we pass
Infinity
, all the tasks will run simultaneously. Let's limit the number of
simultaneous downloads to two though:
const twoInParallelDownloadTask = new MultiTask(downloadTasks, {
concurrency: 2
})
One of chiffchaff's core features is cancellation. Because all the tasks we're
providing are of the type DownloadTask
and therefore cancellable, we can also
cancel the MultiTask
, which will automatically cancel all the downloads.
To enable this behavior, we enable the cancel
option:
const cancellableTwoInParallelDownloadTask = new MultiTask(downloadTasks, {
concurrency: 2,
cancel: true
})
There are a few other options, but let's actually download some files first.
Like any other chiffchaff Task
, a MultiTask
is started using the start()
function. The return value is a promise that will be resolved when all the
subtasks have been completed.
cancellableTwoInParallelDownloadTask.start()
.then((result) => console.info('All downloads completed'))
.catch((err) => console.error('Error: %s', err))
Another one of chiffchaff's strengths is its built-in progress reporting.
MultiTask
is no different. Just like with an individual DownloadTask
, we can
subscribe to updates from the compound download task by listening for the
progress
event.
cancellableTwoInParallelDownloadTask.on('progress',
(compl, total) => console.info('Progress: %d/%d', compl, total))
But how is the progress calculated? By default, all tasks will be treated equally. For example, if three out of four files have finished downloading and the fourth one hasn't started, progress will be at 75%. There are two options that affect the calculation.
The first option is weights
. We can optionally supply an array of numbers that
correspond to the weights of the supplied tasks. In our example, if we wanted
to make the first task contribute twice the weight of the other three, we
would pass weights
of [2, 1, 1, 1]
.
For downloads, however, it makes more sense to just add up the byte sizes.
MultiTask
lets us do that as well. If we pass the ignoreWeights
option,
the tasks' weights will be determined by their total sizes as passed when
reporting progress. In the case of DownloadTask
, the total size is the size of
the file that is being downloaded, which is exactly what we need. Hence, we can
get more accurate progress reporting by initializing our MultiTask
as follows
instead:
const accurateDownloadTask = new MultiTask(downloadTasks, {
concurrency: 2,
cancel: true,
ignoreWeights: true
})
This concludes our brief tour of MultiTask
. We've only just scratched the
surface though. Full API documentation is on its way!
MIT
FAQs
Multi-task execution for chiffchaff.
The npm package chiffchaff-multi receives a total of 13 weekly downloads. As such, chiffchaff-multi popularity was classified as not popular.
We found that chiffchaff-multi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.