plimit.js

Simple promise concurrency limiter
git clone git://git.finwo.net/lib/plimit.js
Log | Files | Refs | README

commit 8490f64ef14154e42b03896e356bf576d90ea9d3
Author: Robin Bron <finwo@pm.me>
Date:   Fri, 10 Dec 2021 10:46:23 +0100

Project init

Diffstat:
AREADME.html | 38++++++++++++++++++++++++++++++++++++++
AREADME.md | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Aindex.js | 26++++++++++++++++++++++++++
Apackage.json | 12++++++++++++
4 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/README.html b/README.html @@ -0,0 +1,38 @@ +<h1>plimit</h1> +<p>Simple promise concurrency limiter</p> +<h2>Installation</h2> +<pre><code class="language-sh">npm install --save plimit +</code></pre> +<pre><code class="language-js"># node +import { plimit } from 'plimit'; +</code></pre> +<pre><code class="language-html">&lt;!-- browser --&gt; +&lt;script src=&quot;/path/to/plimit/index.js&quot;&gt;&lt;/script&gt; +&lt;script&gt; + // use window.plimit +&lt;/script&gt; +</code></pre> +<h2>Usage</h2> +<p>The package assumes native promises are available</p> +<p>This example shows iterating over accounts without loading all into memory</p> +<pre><code class="language-js">import { MoreThan } from 'typeorm'; +import { Account } from './model/account'; // typescript model + +const runner = plimit(4); // runner that allows 4 concurrent tasks +let accountId = ''; + +while(true) { + const account = await Account.findOne({ uuid: MoreThan(accountId) }); + if (!account) break; + accountId = account.uuid; + + // resolves instantly it's queue is not full + // waits for a previous task if it's queue is full + await runner.push(async () =&gt; { + // some lengthy process on the account + }); +} + +// Process remainder +await runner.flush(); +</code></pre> diff --git a/README.md b/README.md @@ -0,0 +1,51 @@ +# plimit + +Simple promise concurrency limiter + +## Installation + +```sh +npm install --save @finwo/p-limit +``` + +```js +# node +import { plimit } from 'plimit'; +``` + +```html +<!-- browser --> +<script src="/path/to/plimit/index.js"></script> +<script> + // use window.plimit +</script> +``` + +## Usage + +The package assumes native promises are available + +This example shows iterating over accounts without loading all into memory + +```js +import { MoreThan } from 'typeorm'; +import { Account } from './model/account'; // typescript model + +const runner = plimit(4); // runner that allows 4 concurrent tasks +let accountId = ''; + +while(true) { + const account = await Account.findOne({ uuid: MoreThan(accountId) }); + if (!account) break; + accountId = account.uuid; + + // resolves instantly it's queue is not full + // waits for a previous task if it's queue is full + await runner.push(async () => { + // some lengthy process on the account + }); +} + +// Process remainder +await runner.flush(); +``` diff --git a/index.js b/index.js @@ -0,0 +1,26 @@ +(factory => { + if ('object' === typeof module) { + module.exports = factory(); + } else if ('object' === typeof window) { + Object.assign(window, factory()); + } else { + throw new Error("Neither 'module.exports' nor 'window' exist"); + } +})(() => ({ + plimit(concurrency) { + const queue = []; + return { + push(task) { + queue.push(task); + if (queue.length >= concurrency) { + return queue.shift(); + } else { + return Promise.resolve(); + } + }, + async flush() { + while(queue.length) await queue.shift(); + }, + }; + } +})); diff --git a/package.json b/package.json @@ -0,0 +1,12 @@ +{ + "name": "@finwo/p-limit", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +}