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
- Host: GitHub
- URL: https://github.com/ido-pluto/class-pull
- Owner: ido-pluto
- License: mit
- Created: 2023-08-16T11:54:04.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-10T11:50:28.000Z (over 2 years ago)
- Last Synced: 2025-02-22T10:17:30.311Z (over 1 year ago)
- Language: TypeScript
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Class Pull
[](https://badge.fury.io/js/catai)
[](https://www.npmjs.com/package/catai)
[](./LICENSE)
[](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;
}
```