1
- var Promise = require ( './Promise' ) ;
1
+ var { Promise} = require ( './Promise' ) ;
2
2
var WorkerHandler = require ( './WorkerHandler' ) ;
3
3
var environment = require ( './environment' ) ;
4
4
var DebugPortAllocator = require ( './debug-port-allocator' ) ;
5
5
var DEBUG_PORT_ALLOCATOR = new DebugPortAllocator ( ) ;
6
6
/**
7
- * A pool to manage workers
7
+ * A pool to manage workers, which can be created using the function workerpool.pool.
8
+ *
8
9
* @param {String } [script] Optional worker script
9
- * @param {WorkerPoolOptions } [options] See docs
10
+ * @param {import('./types.js'). WorkerPoolOptions } [options] See docs
10
11
* @constructor
11
12
*/
12
13
function Pool ( script , options ) {
13
14
if ( typeof script === 'string' ) {
15
+ /** @readonly */
14
16
this . script = script || null ;
15
17
}
16
18
else {
17
19
this . script = null ;
18
20
options = script ;
19
21
}
20
22
23
+ /** @private */
21
24
this . workers = [ ] ; // queue with all workers
25
+ /** @private */
22
26
this . tasks = [ ] ; // queue with tasks awaiting execution
23
27
24
28
options = options || { } ;
25
29
30
+ /** @readonly */
26
31
this . forkArgs = Object . freeze ( options . forkArgs || [ ] ) ;
32
+ /** @readonly */
27
33
this . forkOpts = Object . freeze ( options . forkOpts || { } ) ;
34
+ /** @readonly */
28
35
this . workerOpts = Object . freeze ( options . workerOpts || { } ) ;
36
+ /** @readonly */
29
37
this . workerThreadOpts = Object . freeze ( options . workerThreadOpts || { } )
38
+ /** @private */
30
39
this . debugPortStart = ( options . debugPortStart || 43210 ) ;
40
+ /** @readonly @deprecated */
31
41
this . nodeWorker = options . nodeWorker ;
42
+ /** @readonly
43
+ * @type {'auto' | 'web' | 'process' | 'thread' }
44
+ */
32
45
this . workerType = options . workerType || options . nodeWorker || 'auto'
46
+ /** @readonly */
33
47
this . maxQueueSize = options . maxQueueSize || Infinity ;
48
+ /** @readonly */
34
49
this . workerTerminateTimeout = options . workerTerminateTimeout || 1000 ;
35
50
51
+ /** @readonly */
36
52
this . onCreateWorker = options . onCreateWorker || ( ( ) => null ) ;
53
+ /** @readonly */
37
54
this . onTerminateWorker = options . onTerminateWorker || ( ( ) => null ) ;
38
55
39
56
// configuration
40
57
if ( options && 'maxWorkers' in options ) {
41
58
validateMaxWorkers ( options . maxWorkers ) ;
59
+ /** @readonly */
42
60
this . maxWorkers = options . maxWorkers ;
43
61
}
44
62
else {
@@ -47,6 +65,7 @@ function Pool(script, options) {
47
65
48
66
if ( options && 'minWorkers' in options ) {
49
67
if ( options . minWorkers === 'max' ) {
68
+ /** @readonly */
50
69
this . minWorkers = this . maxWorkers ;
51
70
} else {
52
71
validateMinWorkers ( options . minWorkers ) ;
@@ -56,6 +75,7 @@ function Pool(script, options) {
56
75
this . _ensureMinWorkers ( ) ;
57
76
}
58
77
78
+ /** @private */
59
79
this . _boundNext = this . _next . bind ( this ) ;
60
80
61
81
@@ -86,16 +106,16 @@ function Pool(script, options) {
86
106
* .catch(function(error) {
87
107
* console.log(error);
88
108
* });
89
- *
90
- * @param {String | Function } method Function name or function.
109
+ * @template { (...args: any[]) => any } T
110
+ * @param {String | T } method Function name or function.
91
111
* If `method` is a string, the corresponding
92
112
* method on the worker will be executed
93
113
* If `method` is a Function, the function
94
114
* will be stringified and executed via the
95
115
* workers built-in function `run(fn, args)`.
96
- * @param {Array } [params] Function arguments applied when calling the function
97
- * @param {ExecOptions } [options] Options object
98
- * @return {Promise.<*, Error> } result
116
+ * @param {Parameters<T> | null } [params] Function arguments applied when calling the function
117
+ * @param {import('./types.js'). ExecOptions } [options] Options
118
+ * @return {Promise<ReturnType<T>> }
99
119
*/
100
120
Pool . prototype . exec = function ( method , params , options ) {
101
121
// validate type of arguments
@@ -152,9 +172,9 @@ Pool.prototype.exec = function (method, params, options) {
152
172
153
173
/**
154
174
* Create a proxy for current worker. Returns an object containing all
155
- * methods available on the worker. The methods always return a promise .
156
- *
157
- * @return {Promise.<Object , Error> } proxy
175
+ * methods available on the worker. All methods return promises resolving the methods result .
176
+ * @template { { [k: string]: (...args: any[]) => any } } T
177
+ * @return {Promise<import('./types.js').Proxy<T> , Error> } Returns a promise which resolves with a proxy object
158
178
*/
159
179
Pool . prototype . proxy = function ( ) {
160
180
if ( arguments . length > 0 ) {
@@ -194,7 +214,7 @@ Pool.prototype.map = function (array, callback) {
194
214
/**
195
215
* Grab the first task from the queue, find a free worker, and assign the
196
216
* worker to the task.
197
- * @protected
217
+ * @private
198
218
*/
199
219
Pool . prototype . _next = function ( ) {
200
220
if ( this . tasks . length > 0 ) {
@@ -268,7 +288,7 @@ Pool.prototype._getWorker = function() {
268
288
* pool size is met.
269
289
* @param {WorkerHandler } worker
270
290
* @return {Promise<WorkerHandler> }
271
- * @protected
291
+ * @private
272
292
*/
273
293
Pool . prototype . _removeWorker = function ( worker ) {
274
294
var me = this ;
@@ -299,7 +319,7 @@ Pool.prototype._removeWorker = function(worker) {
299
319
/**
300
320
* Remove a worker from the pool list.
301
321
* @param {WorkerHandler } worker
302
- * @protected
322
+ * @private
303
323
*/
304
324
Pool . prototype . _removeWorkerFromList = function ( worker ) {
305
325
// remove from the list with workers
@@ -374,7 +394,7 @@ Pool.prototype.stats = function () {
374
394
375
395
/**
376
396
* Ensures that a minimum of minWorkers is up and running
377
- * @protected
397
+ * @private
378
398
*/
379
399
Pool . prototype . _ensureMinWorkers = function ( ) {
380
400
if ( this . minWorkers ) {
0 commit comments