-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
/
WorkerPool.js
102 lines (61 loc) · 1.67 KB
/
WorkerPool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @author Deepkolos / https://github.com/deepkolos
*/
export class WorkerPool {
constructor( pool = 4 ) {
this.pool = pool;
this.queue = [];
this.workers = [];
this.workersResolve = [];
this.workerStatus = 0;
}
_initWorker( workerId ) {
if ( ! this.workers[ workerId ] ) {
const worker = this.workerCreator();
worker.addEventListener( 'message', this._onMessage.bind( this, workerId ) );
this.workers[ workerId ] = worker;
}
}
_getIdleWorker() {
for ( let i = 0; i < this.pool; i ++ )
if ( ! ( this.workerStatus & ( 1 << i ) ) ) return i;
return - 1;
}
_onMessage( workerId, msg ) {
const resolve = this.workersResolve[ workerId ];
resolve && resolve( msg );
if ( this.queue.length ) {
const { resolve, msg, transfer } = this.queue.shift();
this.workersResolve[ workerId ] = resolve;
this.workers[ workerId ].postMessage( msg, transfer );
} else {
this.workerStatus ^= 1 << workerId;
}
}
setWorkerCreator( workerCreator ) {
this.workerCreator = workerCreator;
}
setWorkerLimit( pool ) {
this.pool = pool;
}
postMessage( msg, transfer ) {
return new Promise( ( resolve ) => {
const workerId = this._getIdleWorker();
if ( workerId !== - 1 ) {
this._initWorker( workerId );
this.workerStatus |= 1 << workerId;
this.workersResolve[ workerId ] = resolve;
this.workers[ workerId ].postMessage( msg, transfer );
} else {
this.queue.push( { resolve, msg, transfer } );
}
} );
}
dispose() {
this.workers.forEach( ( worker ) => worker.terminate() );
this.workersResolve.length = 0;
this.workers.length = 0;
this.queue.length = 0;
this.workerStatus = 0;
}
}