An open API service indexing awesome lists of open source software.

https://github.com/ido-pluto/class-pull

Limited uses simultaneously execation
https://github.com/ido-pluto/class-pull

Last synced: over 1 year ago
JSON representation

Limited uses simultaneously execation

Awesome Lists containing this project

README

          

# Class Pull

[![npm version](https://badge.fury.io/js/class-pull.svg)](https://badge.fury.io/js/catai)
[![npm downloads](https://img.shields.io/npm/dt/class-pull.svg)](https://www.npmjs.com/package/catai)
[![GitHub license](https://img.shields.io/github/license/ido-pluto/class-pull)](./LICENSE)
[![semantic-release: node](https://img.shields.io/badge/semantic--release-node-5dae47?logo=semantic-release)](https://github.com/semantic-release/semantic-release)

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

```javascript
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) {
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
```typescript
class ClassPull {
new (createInstance: () => State | Promise, options: ClassPullOptions);
run(callback: (instance: State) => Promise | Returns, count = 1): Promise

lockInstance(): Promise;
loadingCount: number;
freeCount: number;
}

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

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