@mrjacket/ahko 0.4.0 → 0.5.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 +14 -0
- package/README.md +38 -2
- package/dist/ahko.d.ts +23 -7
- package/dist/index.cjs +452 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +452 -30
- package/dist/index.js.map +1 -1
- package/dist/models/options.model.d.ts +17 -0
- package/dist/models/strategy.model.d.ts +6 -2
- package/dist/scheduler/debounce-coordinator.d.ts +41 -0
- package/dist/scheduler/task-queue.d.ts +16 -3
- package/dist/scheduler/throttle-coordinator.d.ts +43 -0
- package/dist/version.d.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.5.0] - 2026-09-22 — Throttle, Debounce & Rate Limiting
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Debounce scheduling strategy (`EScheduleStrategy.DEBOUNCE`) with quiet window timer resets.
|
|
12
|
+
- Throttle scheduling strategy (`EScheduleStrategy.THROTTLE`) with immediate leading execution and coalesced trailing run.
|
|
13
|
+
- Promise coalescing by explicit identity key (`key: string | symbol`): all concurrent callers awaiting the same key receive the exact same Promise resolution without artificial cancellation rejections.
|
|
14
|
+
- Task start interval rate limiting via `minIntervalMs` on scheduler constructor (`IAhkoOptions`).
|
|
15
|
+
- Automatic key cleanup and timer detachment upon settlement guaranteeing zero memory leaks.
|
|
16
|
+
- Convenience API methods `ahko.debounce()` and `ahko.throttle()`.
|
|
17
|
+
- Validation for keys, quiet windows, throttle periods, and rate limit intervals.
|
|
18
|
+
- Comprehensive unit test suites for debounce coalescing, throttle leading/trailing runs, and interval rate limiting.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
8
22
|
## [0.4.0] - 2026-09-22 — Timeout & Robust Cancellation
|
|
9
23
|
|
|
10
24
|
### Added
|
package/README.md
CHANGED
|
@@ -179,7 +179,32 @@ try {
|
|
|
179
179
|
}
|
|
180
180
|
```
|
|
181
181
|
|
|
182
|
-
### 7.
|
|
182
|
+
### 7. Debounce & Throttle with Promise Coalescing
|
|
183
|
+
|
|
184
|
+
Coalesce repeated invocations into shared executions by explicit `key`. Callers share the exact same returned Promise:
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
// Debounce: waits for 300ms of quiet before running
|
|
188
|
+
const results = await ahko.debounce("search_box", async () => {
|
|
189
|
+
return queryApi(text);
|
|
190
|
+
}, 300);
|
|
191
|
+
|
|
192
|
+
// Throttle: runs leading edge immediately, coalesces trailing calls
|
|
193
|
+
await ahko.throttle("window_resize", async () => {
|
|
194
|
+
recalculateLayout();
|
|
195
|
+
}, 100);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 8. Paced Execution (`minIntervalMs`)
|
|
199
|
+
|
|
200
|
+
Prevent burst spikes by ensuring a minimum interval elapses between consecutive task starts:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
// At most 2 concurrent tasks, paced at least 50ms apart
|
|
204
|
+
const ahko = new Ahko({ concurrency: 2, minIntervalMs: 50 });
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### 9. Telemetry (`stats`)
|
|
183
208
|
|
|
184
209
|
Inspect real-time scheduler state without synthetic metrics:
|
|
185
210
|
|
|
@@ -222,6 +247,7 @@ Creates an AHKO scheduler instance.
|
|
|
222
247
|
| Option | Type | Default | Description |
|
|
223
248
|
|---|---|---|---|
|
|
224
249
|
| `concurrency` | `number` | `Infinity` | Maximum concurrent tasks allowed to run simultaneously. Must be $\ge 1$. |
|
|
250
|
+
| `minIntervalMs` | `number` | `0` | Minimum interval in milliseconds between consecutive task starts. Must be $\ge 0$. |
|
|
225
251
|
|
|
226
252
|
### `ahko.schedule<T>(task: ITask<T>, options?: IScheduleOptions): Promise<T>`
|
|
227
253
|
|
|
@@ -229,13 +255,23 @@ Schedules an asynchronous task with full return type inference.
|
|
|
229
255
|
|
|
230
256
|
| Option | Type | Default | Description |
|
|
231
257
|
|---|---|---|---|
|
|
232
|
-
| `strategy` | `"immediate" \| "delay" \| "idle"` | `"immediate"` | Scheduling execution strategy. |
|
|
258
|
+
| `strategy` | `"immediate" \| "delay" \| "idle" \| "throttle" \| "debounce"` | `"immediate"` | Scheduling execution strategy. |
|
|
233
259
|
| `delay` | `number` | `0` | Delay in milliseconds when strategy is `"delay"`. |
|
|
260
|
+
| `key` | `string \| symbol` | `undefined` | Explicit identity key for `"debounce"` and `"throttle"`. |
|
|
261
|
+
| `waitMs` | `number` | `undefined` | Window duration in ms for debounce quiet period or throttle interval. |
|
|
234
262
|
| `idleTimeout` | `number` | `undefined` | Maximum time to wait for idle window before forcing queue entry. |
|
|
235
263
|
| `retry` | `IRetryOptions` | `undefined` | Automatic retry policy (attempts, backoff, jitter, predicate). |
|
|
236
264
|
| `timeoutMs` | `number` | `undefined` | Maximum execution duration in milliseconds per attempt before aborting with `AhkoTimeoutError`. |
|
|
237
265
|
| `signal` | `AbortSignal` | `undefined` | Optional external `AbortSignal` for cooperative cancellation. |
|
|
238
266
|
|
|
267
|
+
### `ahko.debounce<T>(key: string | symbol, task: ITask<T>, waitMs: number, options?: IScheduleOptions): Promise<T>`
|
|
268
|
+
|
|
269
|
+
Convenience method scheduling a debounced task with key-based Promise coalescing.
|
|
270
|
+
|
|
271
|
+
### `ahko.throttle<T>(key: string | symbol, task: ITask<T>, waitMs: number, options?: IScheduleOptions): Promise<T>`
|
|
272
|
+
|
|
273
|
+
Convenience method scheduling a throttled task with leading execution and coalesced trailing run.
|
|
274
|
+
|
|
239
275
|
### `ahko.idle<T>(task: ITask<T>, options?: Omit<IScheduleOptions, "strategy">): Promise<T>`
|
|
240
276
|
|
|
241
277
|
Convenience method scheduling a task under `strategy: "idle"`.
|
package/dist/ahko.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ export declare class Ahko {
|
|
|
44
44
|
*
|
|
45
45
|
* @throws {AhkoConfigurationError} If the task is not a function or options are invalid.
|
|
46
46
|
* @throws {AhkoCancellationError} If the task is cancelled prior to or during execution.
|
|
47
|
+
* @throws {AhkoTimeoutError} If task execution exceeds timeoutMs.
|
|
47
48
|
*
|
|
48
49
|
* @example
|
|
49
50
|
* ```typescript
|
|
@@ -73,15 +74,30 @@ export declare class Ahko {
|
|
|
73
74
|
*
|
|
74
75
|
* @throws {AhkoConfigurationError} If the task is not a function or options are invalid.
|
|
75
76
|
* @throws {AhkoCancellationError} If the task is cancelled prior to or during execution.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* const result = await ahko.idle(async ({ signal }) => {
|
|
80
|
-
* return computeAnalytics();
|
|
81
|
-
* });
|
|
82
|
-
* ```
|
|
83
77
|
*/
|
|
84
78
|
idle<T>(task: ITask<T>, options?: Omit<IScheduleOptions, "strategy">): Promise<T>;
|
|
79
|
+
/**
|
|
80
|
+
* Convenience method to schedule a debounced task with key-based Promise coalescing.
|
|
81
|
+
*
|
|
82
|
+
* @template T - Inferred return type of the task.
|
|
83
|
+
* @param key - Explicit identity key.
|
|
84
|
+
* @param task - Work to execute once calls stop arriving.
|
|
85
|
+
* @param waitMs - Quiet window duration in milliseconds.
|
|
86
|
+
* @param options - Additional schedule options.
|
|
87
|
+
* @returns Shared promise resolving with the final execution outcome.
|
|
88
|
+
*/
|
|
89
|
+
debounce<T>(key: string | symbol, task: ITask<T>, waitMs: number, options?: Omit<IScheduleOptions, "strategy" | "key" | "waitMs">): Promise<T>;
|
|
90
|
+
/**
|
|
91
|
+
* Convenience method to schedule a throttled task with leading execution and coalesced trailing run.
|
|
92
|
+
*
|
|
93
|
+
* @template T - Inferred return type of the task.
|
|
94
|
+
* @param key - Explicit identity key.
|
|
95
|
+
* @param task - Work to execute.
|
|
96
|
+
* @param waitMs - Throttle interval duration in milliseconds.
|
|
97
|
+
* @param options - Additional schedule options.
|
|
98
|
+
* @returns Promise resolving with the leading or coalesced trailing result.
|
|
99
|
+
*/
|
|
100
|
+
throttle<T>(key: string | symbol, task: ITask<T>, waitMs: number, options?: Omit<IScheduleOptions, "strategy" | "key" | "waitMs">): Promise<T>;
|
|
85
101
|
/**
|
|
86
102
|
* Retrieves real-time telemetry metrics from the scheduler.
|
|
87
103
|
*
|