@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.
@@ -0,0 +1,63 @@
1
+ import type { IScheduleOptions } from "../models/options.model.js";
2
+ import type { IAhkoStats } from "../models/stats.model.js";
3
+ import { TaskRunner } from "./task-runner.js";
4
+ /**
5
+ * Memory-safe FIFO task queue managing concurrency allocation,
6
+ * delayed scheduling, and task lifecycle counters.
7
+ */
8
+ export declare class TaskQueue {
9
+ /** Maximum concurrent active tasks */
10
+ readonly concurrency: number;
11
+ /** Queue of pending task runners waiting for a concurrency slot */
12
+ private readonly queue;
13
+ /** Set of task runners currently executing */
14
+ private readonly activeRunners;
15
+ /** Set of tasks currently in delay phase */
16
+ private readonly delayedEntries;
17
+ /** Cumulative completed tasks counter */
18
+ private completedTasks;
19
+ /** Cumulative failed tasks counter */
20
+ private failedTasks;
21
+ /** Cumulative cancelled tasks counter */
22
+ private cancelledTasks;
23
+ /** Cumulative timed out tasks counter */
24
+ private timedOutTasks;
25
+ /**
26
+ * Creates a new TaskQueue.
27
+ *
28
+ * @param concurrency - Maximum concurrent tasks (defaults to Infinity).
29
+ * @throws {AhkoConfigurationError} If concurrency is less than 1 or not a valid number.
30
+ */
31
+ constructor(concurrency?: number);
32
+ /**
33
+ * Enqueues a task runner according to the specified schedule options.
34
+ *
35
+ * @template T - The return type produced by the task.
36
+ * @param runner - The task runner instance.
37
+ * @param options - Scheduling options.
38
+ * @returns The deferred promise associated with the task runner.
39
+ * @throws {AhkoConfigurationError} If scheduling options are invalid.
40
+ */
41
+ enqueue<T>(runner: TaskRunner<T>, options?: IScheduleOptions): Promise<T>;
42
+ /**
43
+ * Schedules a task to be placed into the queue after a delay,
44
+ * handling early cancellation safely.
45
+ */
46
+ private scheduleDelayed;
47
+ /**
48
+ * Pumps the queue by picking pending tasks and executing them
49
+ * as long as concurrency capacity is available.
50
+ */
51
+ private pump;
52
+ /**
53
+ * Internal execution of an active task runner.
54
+ * Settle caller promise strictly after stats and active status are updated.
55
+ */
56
+ private executeRunner;
57
+ /**
58
+ * Returns telemetry snapshot for the scheduler.
59
+ *
60
+ * @returns Frozen snapshot of current task metrics.
61
+ */
62
+ getStats(): IAhkoStats;
63
+ }
@@ -0,0 +1,73 @@
1
+ import { ETaskState } from "../models/state.model.js";
2
+ import type { ITask } from "../models/task.model.js";
3
+ /**
4
+ * Internal task lifecycle manager responsible for execution, state transitions,
5
+ * AbortSignal coordination, and deterministic resource cleanup.
6
+ *
7
+ * @template T - The return type produced by the underlying task.
8
+ */
9
+ export declare class TaskRunner<T> {
10
+ /** Unique task identifier */
11
+ readonly taskId: string;
12
+ /** Current lifecycle state */
13
+ private _state;
14
+ /** Internal AbortController whose signal is passed to the task context */
15
+ private readonly abortController;
16
+ /** The user task function to execute */
17
+ private readonly task;
18
+ /** User-supplied AbortSignal for external cancellation */
19
+ private readonly externalSignal?;
20
+ /** Abort event listener reference for clean detachment */
21
+ private readonly abortListener?;
22
+ /** Promise resolve handler */
23
+ private resolvePromise;
24
+ /** Promise reject handler */
25
+ private rejectPromise;
26
+ /** Deferred promise exposed to the caller */
27
+ readonly promise: Promise<T>;
28
+ /** Callback invoked when runner is cancelled while pending */
29
+ onCancel?: (runner: TaskRunner<T>) => void;
30
+ /**
31
+ * Creates a new TaskRunner instance.
32
+ *
33
+ * @param task - The asynchronous work unit to run.
34
+ * @param externalSignal - Optional external AbortSignal to propagate.
35
+ */
36
+ constructor(task: ITask<T>, externalSignal?: AbortSignal);
37
+ /**
38
+ * Gets the current lifecycle state of the task.
39
+ */
40
+ get state(): ETaskState;
41
+ /**
42
+ * Resolves the deferred promise.
43
+ *
44
+ * @param value - Value to resolve with.
45
+ */
46
+ resolve(value: T): void;
47
+ /**
48
+ * Rejects the deferred promise.
49
+ *
50
+ * @param reason - Reason to reject with.
51
+ */
52
+ reject(reason: unknown): void;
53
+ /**
54
+ * Executes the task within an allocated concurrency slot.
55
+ *
56
+ * @returns A promise resolving to the task result or rejecting on failure/cancellation.
57
+ */
58
+ run(): Promise<T>;
59
+ /**
60
+ * Cancels the task, aborting pending or running execution.
61
+ *
62
+ * @param reason - Optional cancellation reason.
63
+ */
64
+ cancel(reason?: unknown): void;
65
+ /**
66
+ * Handles external AbortSignal trigger.
67
+ */
68
+ private handleExternalAbort;
69
+ /**
70
+ * Detaches event listeners from external signal to guarantee memory safety.
71
+ */
72
+ cleanup(): void;
73
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Current version of @mrjacket/ahko package.
3
+ */
4
+ export declare const VERSION = "0.1.0";
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@mrjacket/ahko",
3
+ "version": "0.1.0",
4
+ "description": "A low-energy task scheduler for JavaScript and TypeScript. Let your code chill.",
5
+ "type": "module",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md",
20
+ "CHANGELOG.md"
21
+ ],
22
+ "scripts": {
23
+ "build": "npm run build:js && npm run build:types",
24
+ "build:js": "tsup",
25
+ "build:types": "tsc --declaration --emitDeclarationOnly --outDir dist",
26
+ "prepublishOnly": "npm run build && npm test",
27
+ "test": "vitest run",
28
+ "test:package": "node scripts/package-smoke-test.mjs",
29
+ "test:watch": "vitest",
30
+ "typecheck": "tsc --noEmit"
31
+ },
32
+ "engines": {
33
+ "node": ">=22.12.0"
34
+ },
35
+ "license": "GPL-3.0-only",
36
+ "author": {
37
+ "name": "x-name15",
38
+ "url": "https://github.com/x-name15"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/x-name15/ahko.git"
43
+ },
44
+ "homepage": "https://github.com/x-name15/ahko#readme",
45
+ "bugs": {
46
+ "url": "https://github.com/x-name15/ahko/issues"
47
+ },
48
+ "keywords": [
49
+ "scheduler",
50
+ "queue",
51
+ "concurrency",
52
+ "delay",
53
+ "abortsignal",
54
+ "async"
55
+ ],
56
+ "devDependencies": {
57
+ "@types/node": "^22.13.0",
58
+ "tsup": "^8.3.6",
59
+ "typescript": "^5.7.3",
60
+ "vitest": "^3.0.5"
61
+ }
62
+ }