GPTScript / AiScript

A Minimal, Full-Stack, Tool-Assisted Language. Native to Browsers and Bun. Strictly & Strongly-Typed.
https://github.com/GPTScript/AiScript
Mozilla Public License 2.0
9 stars 1 forks source link

Promise._queue: keep an ongoing queue of a fixed size #47

Open coolaj86 opened 1 year ago

coolaj86 commented 1 year ago

Like Promise._parallel, but for an ongoing queue of potentially different functions, which tasks are enqueued at different times.

'use strict';

Promise._queue = function (limit) {
    let index = 0;
    let actives = [];
    let waiting = [];

    async function launch() {
        // start tasks in parallel, up to limit
        if (actives.length >= limit) {
            return;
        }
        if (waiting.length === 0) {
            return;
        }

        let _index = index;
        index += 1;

        let fn = waiting.shift();
        let p = fn(_index);

        // some tasks may be synchronous
        // so we must push before removing
        actives.push(p);

        p.then(function _resolve() {
            let i = actives.indexOf(p);
            actives.splice(i, 1);
        });

        // keep the active queue full, up to the limit
        launch();

        // let the caller know when *their* task is done
        return await p;
    }

    async function push(fn) {
        waiting.push(fn);
        await launch();
    }

    return { push };
};