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

jscoop

v1.1.0

Published

Cooperative Multitasking Lock and Jobs Processing

Downloads

35

Readme

jscoop

JavaScript Cooperative Multitasking Locks and Jobs Processing

NPM Docs

This is port of Python's asyncio locks, futures and queues modules along with batch processing routines.

Compatibility

  • Browsers >= Good
  • NodeJS >= 14 (some versions of 13 too)

Usage

NodeJS

To use this code in NodeJS, import the jscoop module to get all the available functionality, or you can selectively import the sub modules, such as jscoop/locks, jscoop/queues, etc. See the src directory or src/coop.js for details.

import * as coop from 'jscoop';

(async function() {
    const sem = new coop.Semaphore(2);
    await sem.acquire();
    await sem.acquire();
    await sem.acquire(); // blocks
})();

Browser

For a browser you need to place the files somewhere that your web server can find them and import the full path to the coop.js file, or one of the other submodules if you only want some of the functionality.

<script type="module">
    import * as coop from 'jscoop/src/coop.js';

    (async function() {
        const sem = new coop.Semaphore(2);
        await sem.acquire();
        await sem.acquire();
        await sem.acquire(); // blocks
    })();
</script>

Examples

jobs.UnorderedWorkQueue - Node

import * as jobs from 'jscoop/jobs';

const bufWork = new jobs.UnorderedWorkQueue({maxPending: 10});

async function sleep(ms) {
    await new Promise(resolve => setTimeout(resolve, ms));
}

async function producer() {
    for (let i = 0; i < 200; i++) {
        // Enqueue a random sleep that returns it's start order.
        await bufWork.enqueue(sleep(Math.random() * 1000).then(() => i));
        console.info('Added to queue', i);
    }
}

async function consumer() {
    for await (const i of bufWork) {
        console.info('Consumed finished result', i);
    }
}

(async () => {
    await Promise.all([producer(), consumer()]);
    console.info("Job compete");
})();

locks.Lock - Browser

import * as locks from 'jscoop/src/locks.js';

(async () => {
    const lock = new locks.Lock();
    await lock.acquire();
    lock.release();
    await lock.acquire();
    await lock.acquire(); // blocks
})();

queues.Queue - Node

import * as queues from 'jscoop/queues';

(async () => {
    const q = new queues.Queue();
    q.put(1);
    q.put(2);
    await q.get(); // 1
    await q.get(); // 2
    await q.get(); // blocks
})();