Skip to content

Commit 4fa43a5

Browse files
committed
wip: expanded docs
1 parent 43f345e commit 4fa43a5

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

Diff for: src/WorkerManager.ts

+54-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import { type WorkerTaskInformation } from './types.js';
66
import * as errors from './errors.js';
77
import WorkerPool from './WorkerPool.js';
88

9+
// TODO: events
910
@CreateDestroy()
1011
class WorkerManager<Manifest extends WorkerManifest> {
1112
/**
12-
* Creates the WorkerManager
13-
* The workerFactory needs to be a callback:
14-
* `() => spawn(new Worker(workerPath))`
15-
* The `spawn` and `Worker` can be imported from `threads`
16-
* The `workerPath` must point to a worker script
17-
* The `workerPath` can be either an absolute path or relative path
18-
* If it is a relative path, it has to be relative to the file location where
19-
* the function expression is defined
20-
* If `cores` is set to 0, this creates a useless worker pool
21-
* Use `undefined` to mean using all cores
13+
* Creates and initializes a new instance of WorkerManager.
14+
*
15+
* @param options - The configuration options for the WorkerManager.
16+
* @param options.workerFactory - The factory for creating workers.
17+
* @param options.manifest - The manifest defining the worker capabilities and operations.
18+
* @param [options.cores=1] - The number of cores to allocate for workers. Defaults to 1.
19+
* @param [options.logger=new Logger(this.name)] - An optional logger instance for logging activities. Defaults to a new logger.
20+
* @return {Promise<WorkerManager<Manifest>>} A promise that resolves to a new WorkerManager instance.
2221
*/
2322
public static async createWorkerManager<Manifest extends WorkerManifest>({
2423
workerFactory,
@@ -44,8 +43,21 @@ class WorkerManager<Manifest extends WorkerManifest> {
4443

4544
protected pool: WorkerPool;
4645
protected logger: Logger;
46+
/**
47+
* Methods exposes a fully typed interface for making calls using workers.
48+
* It provides all the available methods provided by the manifest with proper types applied.
49+
*/
4750
public methods: Manifest;
4851

52+
/**
53+
* Constructs a new instance of the class using the provided parameters.
54+
*
55+
* @param config - The configuration for the constructor.
56+
* @param config.workerFactory - The factory for creating worker instances.
57+
* @param config.manifest - The manifest containing method definitions.
58+
* @param config.cores - The number of cores to allocate for the worker pool.
59+
* @param config.logger - The logger instance for logging messages.
60+
*/
4961
public constructor({
5062
workerFactory,
5163
manifest,
@@ -74,6 +86,13 @@ class WorkerManager<Manifest extends WorkerManifest> {
7486
});
7587
}
7688

89+
/**
90+
* Destroys the WorkerManager instance and terminates its associated pool.
91+
*
92+
* @param [params={}] - An optional configuration object.
93+
* @param [params.force=false] - Indicates whether to forcefully terminate the pool.
94+
* @return A promise that resolves when the destruction process is complete.
95+
*/
7796
public async destroy({
7897
force = false,
7998
}: { force?: boolean } = {}): Promise<void> {
@@ -82,11 +101,23 @@ class WorkerManager<Manifest extends WorkerManifest> {
82101
this.logger.info('Destroyed WorkerManager');
83102
}
84103

104+
/**
105+
* Processes a worker task by enqueuing it for execution.
106+
*
107+
* @param task - The information about the worker task to be executed.
108+
* @return A promise that resolves with the result of the worker task execution.
109+
*/
85110
@ready(new errors.ErrorWorkerManagerDestroyed())
86111
public async call(task: WorkerTaskInformation): Promise<WorkerResult> {
87112
return await this.queue(task);
88113
}
89114

115+
/**
116+
* Enqueues a task to be processed by the worker pool.
117+
*
118+
* @param task - The task to be processed by the worker pool.
119+
* @return A promise that resolves with the result of the task or rejects with an error if the task fails or cannot be processed.
120+
*/
90121
@ready(new errors.ErrorWorkerManagerDestroyed())
91122
public queue(task: WorkerTaskInformation): Promise<WorkerResult> {
92123
return new Promise((resolve, reject) => {
@@ -97,11 +128,24 @@ class WorkerManager<Manifest extends WorkerManifest> {
97128
});
98129
}
99130

131+
/**
132+
* Returns a promise that resolves when the pool status becomes 'idle',
133+
* or rejects if the pool status changes to 'terminated' or an error occurs.
134+
*
135+
* @return A promise that resolves when the pool is idle or rejects with an error.
136+
*/
100137
@ready(new errors.ErrorWorkerManagerDestroyed())
101138
public async completed(): Promise<void> {
102139
return await this.pool.completed();
103140
}
104141

142+
/**
143+
* Returns a promise that resolves when the pool status becomes 'idle',
144+
* or rejects if the pool status becomes 'terminated'.
145+
*
146+
* @return A promise that resolves once the pool status is 'idle',
147+
* or rejects if the pool status becomes 'terminated'.
148+
*/
105149
@ready(new errors.ErrorWorkerManagerDestroyed())
106150
public async settled() {
107151
return await this.pool.settled();

Diff for: src/expose.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { isMainThread, parentPort } from 'node:worker_threads';
77
import * as workerErrors from './errors.js';
88

99
/**
10-
* Call inside a worker to set it up and expose the supported worker functions.
11-
* Calling this within the main thread will do nothing
12-
* @param workerManifest
10+
* Exposes a worker's handlers for processing tasks received from the parent thread.
11+
* This can be called within the main thread however it will do nothing.
12+
*
13+
* @param workerManifest - An object containing the methods that the worker can handle.
1314
*/
1415
function expose(workerManifest: WorkerManifest) {
1516
// We don't want to run this in the main thread since it's not acting as a worker

Diff for: tests/WorkerPool.test.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ describe('WorkerPool', () => {
8989
return resolve(result);
9090
});
9191
});
92-
await expect(taskP).rejects.toThrow(errors.ErrorWorkerPoolWorkerCreationFailed);
93-
await expect(pool.settled()).rejects.toThrow(errors.ErrorWorkerPoolWorkerCreationFailed);
94-
await expect(pool.completed()).rejects.toThrow(errors.ErrorWorkerPoolWorkerCreationFailed);
92+
await expect(taskP).rejects.toThrow(
93+
errors.ErrorWorkerPoolWorkerCreationFailed,
94+
);
95+
await expect(pool.settled()).rejects.toThrow(
96+
errors.ErrorWorkerPoolWorkerCreationFailed,
97+
);
98+
await expect(pool.completed()).rejects.toThrow(
99+
errors.ErrorWorkerPoolWorkerCreationFailed,
100+
);
95101
await pool.terminate(false);
96102
});
97103
});

0 commit comments

Comments
 (0)