@mrjacket/ahko 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +674 -0
- package/README.md +157 -0
- package/dist/ahko.d.ts +73 -0
- package/dist/errors/ahko.error.d.ts +12 -0
- package/dist/errors/cancellation.error.d.ts +13 -0
- package/dist/errors/configuration.error.d.ts +13 -0
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/queue.error.d.ts +13 -0
- package/dist/errors/timeout.error.d.ts +13 -0
- package/dist/index.cjs +517 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +482 -0
- package/dist/index.js.map +1 -0
- package/dist/models/context.model.d.ts +14 -0
- package/dist/models/index.d.ts +6 -0
- package/dist/models/options.model.d.ts +33 -0
- package/dist/models/state.model.d.ts +17 -0
- package/dist/models/stats.model.d.ts +19 -0
- package/dist/models/strategy.model.d.ts +13 -0
- package/dist/models/task.model.d.ts +9 -0
- package/dist/scheduler/index.d.ts +2 -0
- package/dist/scheduler/task-queue.d.ts +63 -0
- package/dist/scheduler/task-runner.d.ts +73 -0
- package/dist/version.d.ts +4 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
<h1><img src=".github/images/ahko.png" width="80" height="80"> ahko</h1>
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@mrjacket/ahko)
|
|
4
|
+
[](https://www.npmjs.com/package/@mrjacket/ahko)
|
|
5
|
+
[](https://www.npmjs.com/package/@mrjacket/ahko)
|
|
6
|
+
[](https://github.com/x-name15/ahko/actions/workflows/ci.yml)
|
|
7
|
+
[](https://www.npmjs.com/package/@mrjacket/ahko)
|
|
8
|
+
[](https://github.com/x-name15/ahko/blob/main/LICENSE)
|
|
9
|
+
|
|
10
|
+
> Let your code chill.
|
|
11
|
+
|
|
12
|
+
`@mrjacket/ahko` is a low-energy, production-grade task scheduler for JavaScript and TypeScript.
|
|
13
|
+
|
|
14
|
+
Inspired by Aashii Kedarui / Ahko from *"The 100 Girlfriends Who Really, Really, Really, Really, Really Love You"*, AHKO brings calm, controlled execution to asynchronous workflows without rush, bursts, or unnecessary complexity.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 📦 Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @mrjacket/ahko
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Requires **Node.js >= 22.12.0** or a modern browser environment. Zero external dependencies.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## ⚡ Quick Start
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Ahko } from "@mrjacket/ahko";
|
|
32
|
+
|
|
33
|
+
const ahko = new Ahko({ concurrency: 2 });
|
|
34
|
+
|
|
35
|
+
// Return types are inferred automatically
|
|
36
|
+
const data = await ahko.schedule(async ({ signal, taskId }) => {
|
|
37
|
+
const response = await fetch("https://api.example.com/data", { signal });
|
|
38
|
+
return response.json();
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 🧘 Core Capabilities
|
|
45
|
+
|
|
46
|
+
### 1. Concurrency Control
|
|
47
|
+
|
|
48
|
+
Prevent bursts by capping the number of concurrently running asynchronous tasks:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const ahko = new Ahko({ concurrency: 3 });
|
|
52
|
+
|
|
53
|
+
// 20 operations scheduled at once -> 3 running, 17 queued in FIFO order
|
|
54
|
+
const promises = Array.from({ length: 20 }, (_, i) =>
|
|
55
|
+
ahko.schedule(async () => {
|
|
56
|
+
await performWork(i);
|
|
57
|
+
return i;
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const results = await Promise.all(promises);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. Delayed Execution
|
|
65
|
+
|
|
66
|
+
Wait calmly for a specified time before task execution begins:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
await ahko.schedule(
|
|
70
|
+
async () => {
|
|
71
|
+
console.log("Executed after chill window");
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
strategy: "delay",
|
|
75
|
+
delay: 1500, // milliseconds
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 3. First-Class Cancellation (`AbortSignal`)
|
|
81
|
+
|
|
82
|
+
AHKO provides native, cooperative cancellation:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const controller = new AbortController();
|
|
86
|
+
|
|
87
|
+
const taskPromise = ahko.schedule(
|
|
88
|
+
async ({ signal }) => {
|
|
89
|
+
return doHeavyOperation({ signal });
|
|
90
|
+
},
|
|
91
|
+
{ signal: controller.signal }
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// Cancel while pending: immediately dequeued and rejected with AhkoCancellationError
|
|
95
|
+
// Cancel while running: signal triggers abort on the task context
|
|
96
|
+
controller.abort();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 4. Telemetry (`stats`)
|
|
100
|
+
|
|
101
|
+
Inspect real-time scheduler state without synthetic metrics:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const stats = ahko.stats();
|
|
105
|
+
|
|
106
|
+
console.log(stats);
|
|
107
|
+
// {
|
|
108
|
+
// activeTasks: 2,
|
|
109
|
+
// pendingTasks: 5,
|
|
110
|
+
// completedTasks: 42,
|
|
111
|
+
// failedTasks: 1,
|
|
112
|
+
// cancelledTasks: 2,
|
|
113
|
+
// timedOutTasks: 0,
|
|
114
|
+
// capacity: 3
|
|
115
|
+
// }
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## 📖 API Reference
|
|
121
|
+
|
|
122
|
+
### `new Ahko(options?: IAhkoOptions)`
|
|
123
|
+
|
|
124
|
+
Creates an AHKO scheduler instance.
|
|
125
|
+
|
|
126
|
+
| Option | Type | Default | Description |
|
|
127
|
+
|---|---|---|---|
|
|
128
|
+
| `concurrency` | `number` | `Infinity` | Maximum concurrent tasks allowed to run simultaneously. Must be $\ge 1$. |
|
|
129
|
+
|
|
130
|
+
### `ahko.schedule<T>(task: ITask<T>, options?: IScheduleOptions): Promise<T>`
|
|
131
|
+
|
|
132
|
+
Schedules an asynchronous task with full return type inference.
|
|
133
|
+
|
|
134
|
+
- `task`: `(context: ITaskContext) => Promise<T> | T`
|
|
135
|
+
- `options.strategy`: `"immediate"` (default) or `"delay"`.
|
|
136
|
+
- `options.delay`: Delay in milliseconds when strategy is `"delay"`.
|
|
137
|
+
- `options.signal`: Optional `AbortSignal` for cancellation.
|
|
138
|
+
|
|
139
|
+
### `ahko.stats(): IAhkoStats`
|
|
140
|
+
|
|
141
|
+
Returns a snapshot of current task counters and capacity.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## 🛡️ Errors
|
|
146
|
+
|
|
147
|
+
- `AhkoError`: Base class for all scheduler errors.
|
|
148
|
+
- `AhkoCancellationError`: Thrown when a task is aborted.
|
|
149
|
+
- `AhkoConfigurationError`: Thrown when invalid options are provided.
|
|
150
|
+
- `AhkoQueueError`: Thrown when queue constraints are violated.
|
|
151
|
+
- `AhkoTimeoutError`: Thrown when a task exceeds its configured duration.
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## 📜 License
|
|
156
|
+
|
|
157
|
+
[GNU General Public License v3.0 (GPL-3.0-only)](LICENSE) © [x-name15](https://github.com/x-name15)
|
package/dist/ahko.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { IAhkoOptions, IScheduleOptions } from "./models/options.model.js";
|
|
2
|
+
import type { IAhkoStats } from "./models/stats.model.js";
|
|
3
|
+
import type { ITask } from "./models/task.model.js";
|
|
4
|
+
/**
|
|
5
|
+
* Ahko — Low-energy, production-grade asynchronous task scheduler.
|
|
6
|
+
*
|
|
7
|
+
* Coordinates execution timing, enforces concurrency limits, and cooperates
|
|
8
|
+
* natively with AbortSignal cancellation.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Ahko } from "@mrjacket/ahko";
|
|
13
|
+
*
|
|
14
|
+
* const ahko = new Ahko({ concurrency: 2 });
|
|
15
|
+
*
|
|
16
|
+
* const result = await ahko.schedule(async ({ signal, taskId }) => {
|
|
17
|
+
* const res = await fetch("https://api.example.com", { signal });
|
|
18
|
+
* return res.json();
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare class Ahko {
|
|
23
|
+
/** Internal queue and concurrency manager */
|
|
24
|
+
private readonly queue;
|
|
25
|
+
/**
|
|
26
|
+
* Initializes a new Ahko scheduler instance.
|
|
27
|
+
*
|
|
28
|
+
* @param options - Optional scheduler configuration.
|
|
29
|
+
* @throws {AhkoConfigurationError} If concurrency is invalid (less than 1 or NaN).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const ahko = new Ahko({ concurrency: 4 });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
constructor(options?: IAhkoOptions);
|
|
37
|
+
/**
|
|
38
|
+
* Schedules a task for execution with full return type inference.
|
|
39
|
+
*
|
|
40
|
+
* @template T - Inferred return type of the task.
|
|
41
|
+
* @param task - Asynchronous or synchronous task function accepting an {@link ITaskContext}.
|
|
42
|
+
* @param options - Task-specific scheduling options such as strategy, delay, and cancellation signal.
|
|
43
|
+
* @returns A promise that resolves with the task's return value.
|
|
44
|
+
*
|
|
45
|
+
* @throws {AhkoConfigurationError} If the task is not a function or options are invalid.
|
|
46
|
+
* @throws {AhkoCancellationError} If the task is cancelled prior to or during execution.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Immediate execution (subject to concurrency)
|
|
51
|
+
* const count = await ahko.schedule(async () => 42);
|
|
52
|
+
*
|
|
53
|
+
* // Delayed execution
|
|
54
|
+
* await ahko.schedule(
|
|
55
|
+
* async ({ signal }) => doWork({ signal }),
|
|
56
|
+
* { strategy: "delay", delay: 1000 }
|
|
57
|
+
* );
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
schedule<T>(task: ITask<T>, options?: IScheduleOptions): Promise<T>;
|
|
61
|
+
/**
|
|
62
|
+
* Retrieves real-time telemetry metrics from the scheduler.
|
|
63
|
+
*
|
|
64
|
+
* @returns An {@link IAhkoStats} snapshot of active, pending, completed, failed, cancelled, and timed out tasks.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const stats = ahko.stats();
|
|
69
|
+
* console.log(`Active: ${stats.activeTasks}, Pending: ${stats.pendingTasks}`);
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
stats(): IAhkoStats;
|
|
73
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all errors originating from the Ahko scheduler.
|
|
3
|
+
*/
|
|
4
|
+
export declare class AhkoError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new AhkoError instance.
|
|
7
|
+
*
|
|
8
|
+
* @param message - Descriptive error message.
|
|
9
|
+
* @param options - Standard Error options including cause.
|
|
10
|
+
*/
|
|
11
|
+
constructor(message: string, options?: ErrorOptions);
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AhkoError } from "./ahko.error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a task is cancelled before or during execution.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AhkoCancellationError extends AhkoError {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new AhkoCancellationError.
|
|
8
|
+
*
|
|
9
|
+
* @param message - Reason for cancellation.
|
|
10
|
+
* @param options - Standard Error options including cause.
|
|
11
|
+
*/
|
|
12
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AhkoError } from "./ahko.error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when invalid configuration or scheduling options are provided.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AhkoConfigurationError extends AhkoError {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new AhkoConfigurationError.
|
|
8
|
+
*
|
|
9
|
+
* @param message - Explanation of the invalid configuration parameter.
|
|
10
|
+
* @param options - Standard Error options including cause.
|
|
11
|
+
*/
|
|
12
|
+
constructor(message: string, options?: ErrorOptions);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AhkoError } from "./ahko.error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when an internal queue invariant is violated or queue limits are breached.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AhkoQueueError extends AhkoError {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new AhkoQueueError.
|
|
8
|
+
*
|
|
9
|
+
* @param message - Explanation of the queue failure.
|
|
10
|
+
* @param options - Standard Error options including cause.
|
|
11
|
+
*/
|
|
12
|
+
constructor(message: string, options?: ErrorOptions);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AhkoError } from "./ahko.error.js";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a task exceeds its allotted timeout duration.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AhkoTimeoutError extends AhkoError {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new AhkoTimeoutError.
|
|
8
|
+
*
|
|
9
|
+
* @param message - Explanation of timeout expiry.
|
|
10
|
+
* @param options - Standard Error options including cause.
|
|
11
|
+
*/
|
|
12
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
13
|
+
}
|