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

fake-progress

v1.0.4

Published

Fake a progress bar using an exponential progress function

Downloads

5,980

Readme

Travis build status

Fakeprogress

Simulate smooth progression easily and combine real progression and fake progression.

Install

npm install fake-progress

Basic example

var FakeProgress = require("fake-progress");

// Create the fake progress with a timeConstant of 10 seconds
// it means that :
//  after 10 seconds, progress will be 0.6321 ( = 1-Math.exp(-1) )
//  after 20 seconds, progress will be 0.8646 ( = 1-Math.exp(-2) )
//  and so one
var p = new FakeProgress({
	timeConstant : 10000,
	autoStart : true
});

var exampleAsyncFunction = function(callback){
	setTimeout(function(){
		callback()
	},30000)
};

var onEachSecond = function(){
	console.log("Progress is "+(p.progress*100).toFixed(1)+" %");
};

var interval = setInterval(onEachSecond, 1000);

var onEnd = function(){
	p.end();
	clearInterval(interval);
	console.log("Ended. Progress is "+(p.progress*100).toFixed(1)+" %")
};

exampleAsyncFunction(onEnd);

will print

Progress is 8.6 %
Progress is 17.3 %
Progress is 25.2 %
Progress is 32.3 %
...

The chart of progression over time.

Chart of progress

Until the end is triggered, the progression is following the exponential curve, once "end" is triggered, progression goes to 100%.

More complex

In this example we will mix 3 functions, A and C are classical async functions, B is an async function with a 'real' callback.

Create 2 async function a, c

a and c are 2 basic async functions without progress.

const a = function (cb) {
	setTimeout(() => {
		cb();
	}, 1000);
};

const c = function (cb) {
	setTimeout(() => {
		cb();
	}, 3000);
};

Create a class for event emitter with linear progress

b will be an instance of an event emmiter that has a progress event

const B = function () {
	EventEmitter.call(this);

	let count = 0;
	const self = this;
	const totalCount = 30;
	self.emit('start', count / totalCount);
	self._intervalId = setInterval(() => {
		count++;
		if (count >= totalCount) {
			self.emit('end', count / totalCount);
			clearInterval(self._intervalId);
		} else {
			self.emit('progress', count / totalCount);
		}
	}, 100);
};

util.inherits(B, EventEmitter);

Create a fake progress and log his value over time

const p = new FakeProgress({});

const onEachDeciSecond = function () {
	console.log('Progress is ' + (p.progress * 100).toFixed(1) + ' %');
};

onEachDeciSecond();

const interval = setInterval(onEachDeciSecond, 100);

Create sub progress bar of p, for a progress

A has no progress so we fake his progress. A succeed in 1000 ms, so we can consider 500 ms is a good timeConstant.

const aProgress = p.createSubProgress({
	timeConstant: 500,
	end: 0.3,
	autoStart: true
});

Call async chain

Each time on the async chain, subProgress.stop() then call createSubProgress() to create a new subProgress.

a(err => {
	if (err) {
		throw (err);
	}
	aProgress.stop();
	const bProgress = p.createSubProgress({
		end: 0.8
	});
	const b = new B();

	b.on('progress', progress => {
		bProgress.setProgress(progress);
	});

	b.on('end', () => {
		bProgress.stop();
		const cProgress = p.createSubProgress({
			timeConstant: 1000,
			autoStart: true
		});
		c(() => {
			cProgress.end();
			onEachDeciSecond();
			clearInterval(interval);
		});
	});
});

Call everything

After each call, stop previous sub, and create a new subProgress for next request.

a(function(){
	aProgress.stop();
	var bProgress = p.createSubProgress({
		end : 0.8
	});
	var b = new B();

	b.on('progress', function(progress){
		bProgress.setProgress(progress);
	});

	b.on('end', function(){
		bProgress.stop();
		var cProgress = p.createSubProgress({
			timeConstant : 1000,
			autoStart : true
		});
		c(function(){
			cProgress.end()
			onEachDeciSecond()
			clearInterval(interval);
		})
	});
});

All together

see source

Results

Chart of progress

Documentation

See inside the code, documentated using JSDoc