class-pull
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Class Pull

npm version npm downloads GitHub license semantic-release: node

This package enables you to manage a class pull.

With this package, you can:

  • Make sure only limited uses of that class can happen simultaneously
  • Do heavy tasks that require a state
  • Manage pull & get statistics

Use example

import {ClassPull} from 'class-pull';

function createState() {
    return {
        counter: 0,
        sleep(ms = 1000) {
            return new Promise(resolve => setTimeout(resolve, ms));
        }
    };
}

const pull = new ClassPull(createState, {limit: 2});

async function countRef(instance: ReturnType<typeof createState>) {
    await instance.sleep(1000);
    console.log('counter', ++instance.counter);
}

function main() {
    pull.run(countRef, 5);
} main();


/**
 * Output:
 * counter 1
 * counter 1
 * counter 2
 * counter 2
 * counter 3
 */

API

class ClassPull<State> {
    new (createInstance: () => State | Promise<State>, options: ClassPullOptions);
    run<Response>(callback: (instance: State) => Promise<Returns> | Returns, count = 1): Promise<Response[]>

    lockInstance(): Promise<lockInstanceItem>;
    loadingCount: number;
    freeCount: number;
}

type ClassPullOptions= {
    limit?: number;
    createOnInit?: number;
}

type lockInstanceItem = {
    instance: T;
    unlock: () => void;
}

Package Sidebar

Install

npm i class-pull

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

11.1 kB

Total Files

9

Last publish

Collaborators

  • ido.pluto