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

procrastinate-queue

v1.0.2

Published

Ever wanted to do things later and one only thing at a time? Well now you can! You can even not do the thing if you are already doing one thing.

Downloads

4

Readme

What is procrastinate.js?

Build Status Coverage Status

Ever wanted to do things later and one only thing at a time? Well now you can! You can even not do the thing if you are already doing one thing.

Usage

Basic usage

var p = new procrastinate({
	'events': {
		'beforeSave': 1, // No async
		'save': 2, // A little async
		'afterSave': 100 // Very async
	}
});

var log = [];

p.on('beforeSave', function() { log.push('Preparing to save') });
p.on('beforeSave', function() {
	var d = deferred();
	setTimeout(function() {
		// Wait a little before we start...
		log.push('Done waiting');
		d.resolve();
	}, 1000);
	return d.promise;
});
var save = function() {
	var d = deferred();
	setTimeout(function() {
		log.push('Save half-way');
		setTimeout(function() {
			log.push('Save completed');
			d.resolve();
		}, 1000);
	}, 1000);
	return d.promise;
};
p.on('save', save);
p.on('save', save);
p.on('save', save);
p.on('afterSave', function() {  log.push('Done saving, phew') });

// Call it
p.doNow();

log should contain:

Preparing to save
Done waiting
Save half-way
Save half-way
Save completed
Save completed
Save half-way
Save completed
Done saving, phew

Use case: autosave form to server without spamming the server

var p = new procrastinate(/* your events */);
p.on(/* your listeners */);

$('input, select, textarea').on('change', function() {
	p.doLater(3000); // Only do after 3 sec of inactivity
	// If 3 sec delay happens to expire when it is already saving it will be ignored
	// since enqueue is not set to true
});
$('.saveButton').on('click', function() {
	p.doNow(true); // Do now, enqueue if necessary.
    // Enqueue means that if something is running it will do after current task is done
});

Install

node

npm install --save procrastinate-queue

(The name "procrastinate" was actually taken...)

browser

<script src="build/browser/procrastinate.min.js"></script>

Methods

new procrastinate(customOptions)

Initialize with options that declare your event flow/order and max concurrency for each event.

Example:

var p = new procrastinate({
	'events': {
    	'validate': 1, 'beforeSave': 1, 'save': 2, 'afterSave': 10
    }
});

The events will be executed in the specified order. For example, beforeSave will run after all validate events have finished. Additionally, the listeneres to both validate and beforeSave will run sequentially. save will allow 2 listners to run simultaneously while afterSave will allow 10.

on(listener)

Add event listeners to your events. The listeners will be executed in the order they are added.

Both regular functions and deferred functions are supported.

Example:

p.on('save', function() {  /* Do something syncronously */ });
p.on('save', function() {  
	var d = deferred(); // Use: https://www.npmjs.com/package/deferred
    setTimeout(function() {
    	d.resolve();
    }, 1000);
    return d.promise();
});

doNow(enqueue = false)

Execute the event chain now. This method returns a promise which will be resolved once the entire event chain has completed.

If enqueue is false or undefined, and if there is already another event chain ongoing, this will do nothing, and the returned promise will resolve immediately.

Set enqueue to true to execute the event chain once the ongoing event chain has completed.

doLater(timeout, enqueue = false)

Execute the event chain when the timeout has expired. However, if doLater is called before the previous timeout has expired, it will cancel the previous doLater. This method returns a promise that resolves when the deferred event chain completes. (It will not resolve if it cleared. This behaviour might change in a future version.)

Set enqueue to true to execute the doLater after any ongoing event chain has completed.

A doNow clears any unenqueued doLater. To prevent your doLater from being cleared by a doNow set enqueue to true.

isDoing()

Returns true if an event chain is currently executing.

getDoing()

Get the promise for the ongoing event chain. If there is no ongoing event chain this will return an immediately resolved promise.

abort()

Calling this method while an event chain is ongoing will prevent the next event in the chain from executing. It will not abort the ongoing event because events might be running in parallell.