@@ -6,19 +6,18 @@ import { type WorkerTaskInformation } from './types.js';
66import * as errors from './errors.js' ;
77import WorkerPool from './WorkerPool.js' ;
88
9+ // TODO: events
910@CreateDestroy ( )
1011class 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 ( ) ;
0 commit comments