@@ -6,19 +6,18 @@ import { type WorkerTaskInformation } from './types.js';
6
6
import * as errors from './errors.js' ;
7
7
import WorkerPool from './WorkerPool.js' ;
8
8
9
+ // TODO: events
9
10
@CreateDestroy ( )
10
11
class WorkerManager < Manifest extends WorkerManifest > {
11
12
/**
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.
22
21
*/
23
22
public static async createWorkerManager < Manifest extends WorkerManifest > ( {
24
23
workerFactory,
@@ -44,8 +43,21 @@ class WorkerManager<Manifest extends WorkerManifest> {
44
43
45
44
protected pool : WorkerPool ;
46
45
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
+ */
47
50
public methods : Manifest ;
48
51
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
+ */
49
61
public constructor ( {
50
62
workerFactory,
51
63
manifest,
@@ -74,6 +86,13 @@ class WorkerManager<Manifest extends WorkerManifest> {
74
86
} ) ;
75
87
}
76
88
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
+ */
77
96
public async destroy ( {
78
97
force = false ,
79
98
} : { force ?: boolean } = { } ) : Promise < void > {
@@ -82,11 +101,23 @@ class WorkerManager<Manifest extends WorkerManifest> {
82
101
this . logger . info ( 'Destroyed WorkerManager' ) ;
83
102
}
84
103
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
+ */
85
110
@ready ( new errors . ErrorWorkerManagerDestroyed ( ) )
86
111
public async call ( task : WorkerTaskInformation ) : Promise < WorkerResult > {
87
112
return await this . queue ( task ) ;
88
113
}
89
114
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
+ */
90
121
@ready ( new errors . ErrorWorkerManagerDestroyed ( ) )
91
122
public queue ( task : WorkerTaskInformation ) : Promise < WorkerResult > {
92
123
return new Promise ( ( resolve , reject ) => {
@@ -97,11 +128,24 @@ class WorkerManager<Manifest extends WorkerManifest> {
97
128
} ) ;
98
129
}
99
130
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
+ */
100
137
@ready ( new errors . ErrorWorkerManagerDestroyed ( ) )
101
138
public async completed ( ) : Promise < void > {
102
139
return await this . pool . completed ( ) ;
103
140
}
104
141
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
+ */
105
149
@ready ( new errors . ErrorWorkerManagerDestroyed ( ) )
106
150
public async settled ( ) {
107
151
return await this . pool . settled ( ) ;
0 commit comments