npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chiffchaff-multi

v0.4.2

Published

Multi-task execution for chiffchaff.

Downloads

9

Readme

chiffchaff-multi

npm Dependencies Build Status Coverage Status JavaScript Standard Style

Multi-task execution for chiffchaff.

Example

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!

Maintainer

Tim De Pauw

License

MIT