fix(docs): Update @push.rocks/tapbundle dependency and refine AsyncExecutionStack documentation examples
This commit is contained in:
51
readme.md
51
readme.md
@@ -17,22 +17,53 @@ This will download `@push.rocks/lik` and add it to your project's `package.json`
|
||||
|
||||
### AsyncExecutionStack
|
||||
|
||||
`AsyncExecutionStack` allows for managing asynchronous task execution with exclusive and non-exclusive slots, ensuring effective handling of resource-intensive operations.
|
||||
`AsyncExecutionStack` provides controlled execution of asynchronous tasks in two modes:
|
||||
|
||||
- **Exclusive tasks**: run one at a time in insertion order, blocking all other tasks until complete.
|
||||
- **Non-exclusive tasks**: run in parallel, up to an optional concurrency limit.
|
||||
|
||||
Create a stack:
|
||||
|
||||
```typescript
|
||||
import { AsyncExecutionStack } from '@push.rocks/lik';
|
||||
|
||||
const myAsyncStack = new AsyncExecutionStack();
|
||||
const stack = new AsyncExecutionStack();
|
||||
```
|
||||
|
||||
// Exclusive execution ensures this task doesn't run in parallel with others
|
||||
await myAsyncStack.getExclusiveExecutionSlot(async () => {
|
||||
// some asynchronous operation
|
||||
}, 2500);
|
||||
Exclusive tasks execute sequentially and block subsequent tasks (with an optional timeout):
|
||||
|
||||
// Non-exclusive slots can run in parallel
|
||||
myAsyncStack.getNonExclusiveExecutionSlot(async () => {
|
||||
// another asynchronous operation
|
||||
}, 1500);
|
||||
```typescript
|
||||
await stack.getExclusiveExecutionSlot(
|
||||
async () => {
|
||||
// exclusive work
|
||||
},
|
||||
5000 // optional timeout in milliseconds
|
||||
);
|
||||
```
|
||||
|
||||
Non-exclusive tasks run in parallel up to the concurrency limit (default: unlimited). Each call returns a promise you can await:
|
||||
|
||||
```typescript
|
||||
const p1 = stack.getNonExclusiveExecutionSlot(async () => {
|
||||
// work 1
|
||||
}, 2000);
|
||||
|
||||
const p2 = stack.getNonExclusiveExecutionSlot(async () => {
|
||||
// work 2
|
||||
}, 2000);
|
||||
|
||||
await Promise.all([p1, p2]);
|
||||
```
|
||||
|
||||
Control concurrency and inspect status:
|
||||
|
||||
```typescript
|
||||
// Set maximum concurrent non-exclusive tasks (minimum 1)
|
||||
stack.setNonExclusiveMaxConcurrency(3);
|
||||
|
||||
console.log(stack.getNonExclusiveMaxConcurrency()); // 3
|
||||
console.log(stack.getActiveNonExclusiveCount()); // currently running tasks
|
||||
console.log(stack.getPendingNonExclusiveCount()); // tasks waiting for slots
|
||||
```
|
||||
|
||||
### FastMap
|
||||
|
||||
Reference in New Issue
Block a user