class-pull
v1.1.0
Published
Make a pull of classes
Downloads
17
Maintainers
Readme
Class Pull
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;
}