forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction-executor.ts
53 lines (46 loc) · 1.7 KB
/
action-executor.ts
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import JestWorker from 'jest-worker';
import * as os from 'os';
export class ActionExecutor<Input extends { size: number }, Output> {
private largeWorker: JestWorker;
private smallWorker: JestWorker;
private smallThreshold = 32 * 1024;
constructor(actionFile: string, private readonly actionName: string) {
// larger files are processed in a separate process to limit memory usage in the main process
this.largeWorker = new JestWorker(actionFile, {
exposedMethods: [actionName],
});
// small files are processed in a limited number of threads to improve speed
// The limited number also prevents a large increase in memory usage for an otherwise short operation
this.smallWorker = new JestWorker(actionFile, {
exposedMethods: [actionName],
numWorkers: os.cpus().length < 2 ? 1 : 2,
// Will automatically fallback to processes if not supported
enableWorkerThreads: true,
});
}
execute(options: Input): Promise<Output> {
if (options.size > this.smallThreshold) {
return ((this.largeWorker as unknown) as Record<string, (options: Input) => Promise<Output>>)[
this.actionName
](options);
} else {
return ((this.smallWorker as unknown) as Record<string, (options: Input) => Promise<Output>>)[
this.actionName
](options);
}
}
executeAll(options: Input[]): Promise<Output[]> {
return Promise.all(options.map(o => this.execute(o)));
}
stop() {
this.largeWorker.end();
this.smallWorker.end();
}
}