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

fifolock

v1.0.0

Published

Not-quite-barebones serial queue / lock.

Downloads

40,702

Readme

fifolock

Very simple FIFO task runner (usable as a lock or serial queue), plus a callback-wrapping helper.

Installation

npm install fifolock

Example

var q = require('fifolock')();

q.acquire(function (release) {
    setTimeout(function () {
        console.log("This task started first.");
        release();
    }, 1e3);
});

attemptSomething(function (e, d) {
    if (e) console.warn("Attempt happened to fail.", e);
    else console.log("Attempt happened to succeed,", d);
});

attemptSeveralSomethings(42, function (e) {
    if (e) console.warn("Got the inevitable error.", e);
    else console.log("Wow, that was improbable…");
});

q.acquire(function (done) {
    console.log("This task was started last.");
    done();
});


// callers of q.TRANSACTION_WRAPPER wait to acquire the queue just like any direct users
function attemptSomething(cb, _nested) { cb = q.TRANSACTION_WRAPPER(cb, function () {
    if (Math.random() > 0.75) process.nextTick(function () {
        cb(new Error("Chaos monkey struck!"));
    }); else setTimeout(function () {
        cb(null, "here's the data.");
    }, 0);
    
}, _nested); }

// advanced trick: sharing a single acquisition with nested/nestable helper routines
function attemptSeveralSomethings(n, cb) { cb = q.TRANSACTION_WRAPPER(cb, function () {
    function recursiveAttempt(i) {
        if (i < n) attemptSomething(function (e) {
            if (e) cb(new Error("Only succeeded at "+i+" of "+n+" attempts"));
            else recursiveAttempt(i+1);
        }, true);       // note how, unlike normal callers, we use our internal _nested parameter
        else cb(null);
    }
    recursiveAttempt(0);
}); }

This code will output something like:

This task started first. Attempt happened to succeed, here's the data. Got the inevitable error. [Error: Only succeeded at 8 of 42 attempts] This task was started last.

API

  • var q = fifolock(); — the module exports a factory function, call it to get a queue instance
  • q.acquire(task) — enqueues task to start when all previously-queued tasks have finished (or in the next tick if q was empty). When it is started, task will receive one argument e.g. (finish) which is a function it must call to release the lock and allow any next task to proceed.
  • cb = q.TRANSACTION_WRAPPER(cb, fn, [skipAcquisition]) — this method helps you ensure "normal" async logic happens in a consistent fashion. Before executing fn it waits to aquire a lock, but immediately returns a wrapper around cb which will forward the call while making sure the lock is properly released. The skipAcquisition argument is optional, intended to keep code that must work both within its own transactions, and under the auspices of another caller's already-acquired lock. (See example above.)
  • var CUSTOM_WRAPPER = q.TRANSACTION_WRAPPER.bind({postAcquire:preFn, preRelease:postFn}); — the wrapper method also allows you to provide postAcquire and/or preRelease hooks to bracket a transaction with additional custom logic. The postAcquire hook preFn(proceed) should do any addition setup require and then must call proceed. Note that while the hook must proceed it could signal an error condition by passing custom arguments to proceed; these will be forwarded to the main function(s) which would then need to participate in any such failure-handling protocol. The preRelease hook postFn(finish, [args, ctx]) should do any cleanup necessary and then must release the queue by calling finish. Note that the preRelease hook gets an array copy of the arguments which will be passed to the wrapped callback in case it affects cleanup, but cannot control what the callback receives. (See "examples/custom_wrapper.js" for a sample custom wrapper.)

License

© 2014 Nathan Vander Wilt. Funding for this work was provided in part by Technical Machine, Inc.

Reuse under your choice of: