-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathaction-executor.ts
101 lines (84 loc) · 2.79 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
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
/**
* @license
* Copyright Google LLC 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 Piscina from 'piscina';
import { BundleActionCache } from './action-cache';
import { maxWorkers } from './environment-options';
import { I18nOptions } from './i18n-options';
import { InlineOptions, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
const workerFile = require.resolve('./process-bundle');
export class BundleActionExecutor {
private workerPool?: Piscina;
private cache?: BundleActionCache;
constructor(
private workerOptions: { cachePath?: string; i18n: I18nOptions },
integrityAlgorithm?: string,
) {
if (workerOptions.cachePath) {
this.cache = new BundleActionCache(workerOptions.cachePath, integrityAlgorithm);
}
}
private ensureWorkerPool(): Piscina {
if (this.workerPool) {
return this.workerPool;
}
this.workerPool = new Piscina({
filename: workerFile,
name: 'process',
workerData: this.workerOptions,
maxThreads: maxWorkers,
});
return this.workerPool;
}
async process(action: ProcessBundleOptions): Promise<ProcessBundleResult> {
if (this.cache) {
const cacheKeys = this.cache.generateCacheKeys(action);
action.cacheKeys = cacheKeys;
// Try to get cached data, if it fails fallback to processing
try {
const cachedResult = await this.cache.getCachedBundleResult(action);
if (cachedResult) {
return cachedResult;
}
} catch {}
}
return this.ensureWorkerPool().run(action, { name: 'process' });
}
processAll(actions: Iterable<ProcessBundleOptions>): AsyncIterable<ProcessBundleResult> {
return BundleActionExecutor.executeAll(actions, (action) => this.process(action));
}
async inline(
action: InlineOptions,
): Promise<{ file: string; diagnostics: { type: string; message: string }[]; count: number }> {
return this.ensureWorkerPool().run(action, { name: 'inlineLocales' });
}
inlineAll(actions: Iterable<InlineOptions>) {
return BundleActionExecutor.executeAll(actions, (action) => this.inline(action));
}
private static async *executeAll<I, O>(
actions: Iterable<I>,
executor: (action: I) => Promise<O>,
): AsyncIterable<O> {
const executions = new Map<Promise<O>, Promise<O>>();
for (const action of actions) {
const execution = executor(action);
executions.set(
execution,
execution.then((result) => {
executions.delete(execution);
return result;
}),
);
}
while (executions.size > 0) {
yield Promise.race(executions.values());
}
}
stop(): void {
void this.workerPool?.destroy();
}
}