https://github.com/beyondjs/execution-control
A lightweight library for managing single-function execution and cancellation tokens in TypeScript. Prevents redundant function calls and provides control over async operation cancellation.
https://github.com/beyondjs/execution-control
Last synced: 8 months ago
JSON representation
A lightweight library for managing single-function execution and cancellation tokens in TypeScript. Prevents redundant function calls and provides control over async operation cancellation.
- Host: GitHub
- URL: https://github.com/beyondjs/execution-control
- Owner: beyondjs
- Created: 2024-08-18T23:51:02.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-18T23:51:04.000Z (almost 2 years ago)
- Last Synced: 2025-02-14T03:34:41.227Z (over 1 year ago)
- Language: TypeScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Execution Control
This package contains two independent modules designed to enhance control over function execution in
JavaScript/TypeScript projects. It includes a decorator for managing single execution of functions and a class for
handling cancellation tokens.
## Installation
Install via npm:
```bash
npm install execution-control
```
## Modules
### 1. SingleCall Decorator
This decorator ensures that a function can only be called once at a time for a given instance. If the function is
already in progress, the same promise is returned instead of invoking the function again.
#### Usage
```typescript
import { SingleCall } from 'execution-control/single-call';
class Example {
@SingleCall
async fetchData() {
// Your logic here
}
}
```
In this example, multiple calls to `fetchData()` while a promise is pending will all return the same promise.
### 2. CancellationToken Class
The `CancellationToken` class provides a mechanism to manage and verify cancellation tokens in asynchronous processes.
#### Usage
```typescript
import { CancellationToken } from 'execution-control/cancelation-token';
const token = new CancellationToken();
async function fetchData() {
const id = token.reset();
// Perform some operation
if (!token.check(id)) {
// Handle cancellation
}
}
```
The `reset()` method generates a new token, while `check(id)` verifies if the current operation is still valid based on
the last token.
## Module Overview
- **SingleCall**: Prevents simultaneous calls to the same function, returning the first in-progress promise for
subsequent calls.
- **CancellationToken**: Provides a way to control and cancel long-running or asynchronous operations.
## License
This project is licensed under the MIT License.