Socket
Book a DemoInstallSign in
Socket

jscoop

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscoop

Cooperative Multitasking Lock and Jobs Processing

1.1.0
latest
Source
npmnpm
Version published
Weekly downloads
6
-14.29%
Maintainers
1
Weekly downloads
 
Created
Source

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
})();

Keywords

lock

FAQs

Package last updated on 26 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.