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

guvna

v0.0.5-a

Published

A simple bit of code to manage the concurrency of a function. Very useful in limiting the number of asynch calls in a loop.

Downloads

19

Readme

Guvna

Guvna allows for easily managing the number of times a function is run concurrently.

I ran into a problem where I neede to execute a resource intensive function iteratively and quickly killed the box.

To address the problem, I wrote the Guvna to govern the volume and execution of this function.

Version 0.0.5

Added the this.percDone metric to make it easy to show the percentage done in the callback.

Usage

Since Guvna now uses integer variables to signal when your callback has completed a run, you MUST include this.next(); in your function, though if you're running an asynch function, you'll likely have to create a reference to this as seen in the example below that gets tucked into the asynch function. In the code below, I call create self so that I can get to the .next() function.

In this example, I use setTimeout to simulate an asynch call.

var	response = [],
		Guvna = require('guvna');

var guv = new Guvna({
	max: 2,
	list: [ { name: 'blargh' }, { name: 'honk' }, { name: 'toot' } ],
	callback: function(l) {
		var rand = Math.floor((Math.random()*10)+1) * 1000,
				self = this;
		setTimeout(function() {
			response.push(l.name+'-'+rand);
			console.log(parseInt(self.percDone*100)+'% complete');
			self.next(); // CHANGE FROM v0.0.1
		}, rand);
	},
	done: function() { console.log(response); }
});

guv.start();

Options

When declaring your guv variable, you need to pass it several options:

  • list: This is a simple array. Each item in the array is passed to the callback function one at a time.
  • callback: This is the function that you want to govern. It is passed each item from the list.
  • done: This is the function to run when all your callbacks are complete.
  • max: (optional) This is an integer for how many concurrent callbacks you want to have running. Default = list.length

Versions

  • 0.0.5 - Added percDone metric.
  • 0.0.4 - Added error handling.
  • 0.0.3 - Tweaked calculation of max if it's omitted.
  • 0.0.2 - Refactored to use tracking variables as using events created unnecessary comnplexity especially when nesting guvnas.
  • 0.0.1 - Initial drop, used events to manage concurrency.