[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
| 10 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
| 11 | };
|
---|
| 12 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 13 | exports.BundleActionExecutor = void 0;
|
---|
| 14 | const piscina_1 = __importDefault(require("piscina"));
|
---|
| 15 | const action_cache_1 = require("./action-cache");
|
---|
| 16 | const environment_options_1 = require("./environment-options");
|
---|
| 17 | const workerFile = require.resolve('./process-bundle');
|
---|
| 18 | class BundleActionExecutor {
|
---|
| 19 | constructor(workerOptions, integrityAlgorithm) {
|
---|
| 20 | this.workerOptions = workerOptions;
|
---|
| 21 | if (workerOptions.cachePath) {
|
---|
| 22 | this.cache = new action_cache_1.BundleActionCache(workerOptions.cachePath, integrityAlgorithm);
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 | ensureWorkerPool() {
|
---|
| 26 | if (this.workerPool) {
|
---|
| 27 | return this.workerPool;
|
---|
| 28 | }
|
---|
| 29 | this.workerPool = new piscina_1.default({
|
---|
| 30 | filename: workerFile,
|
---|
| 31 | name: 'process',
|
---|
| 32 | workerData: this.workerOptions,
|
---|
| 33 | maxThreads: environment_options_1.maxWorkers,
|
---|
| 34 | });
|
---|
| 35 | return this.workerPool;
|
---|
| 36 | }
|
---|
| 37 | async process(action) {
|
---|
| 38 | if (this.cache) {
|
---|
| 39 | const cacheKeys = this.cache.generateCacheKeys(action);
|
---|
| 40 | action.cacheKeys = cacheKeys;
|
---|
| 41 | // Try to get cached data, if it fails fallback to processing
|
---|
| 42 | try {
|
---|
| 43 | const cachedResult = await this.cache.getCachedBundleResult(action);
|
---|
| 44 | if (cachedResult) {
|
---|
| 45 | return cachedResult;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | catch { }
|
---|
| 49 | }
|
---|
| 50 | return this.ensureWorkerPool().run(action, { name: 'process' });
|
---|
| 51 | }
|
---|
| 52 | processAll(actions) {
|
---|
| 53 | return BundleActionExecutor.executeAll(actions, (action) => this.process(action));
|
---|
| 54 | }
|
---|
| 55 | async inline(action) {
|
---|
| 56 | return this.ensureWorkerPool().run(action, { name: 'inlineLocales' });
|
---|
| 57 | }
|
---|
| 58 | inlineAll(actions) {
|
---|
| 59 | return BundleActionExecutor.executeAll(actions, (action) => this.inline(action));
|
---|
| 60 | }
|
---|
| 61 | static async *executeAll(actions, executor) {
|
---|
| 62 | const executions = new Map();
|
---|
| 63 | for (const action of actions) {
|
---|
| 64 | const execution = executor(action);
|
---|
[e29cc2e] | 65 | executions.set(execution, execution.then((result) => [execution, result]));
|
---|
[6a3a178] | 66 | }
|
---|
| 67 | while (executions.size > 0) {
|
---|
[e29cc2e] | 68 | const [execution, result] = await Promise.race(executions.values());
|
---|
| 69 | executions.delete(execution);
|
---|
| 70 | yield result;
|
---|
[6a3a178] | 71 | }
|
---|
| 72 | }
|
---|
| 73 | stop() {
|
---|
| 74 | var _a;
|
---|
| 75 | void ((_a = this.workerPool) === null || _a === void 0 ? void 0 : _a.destroy());
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | exports.BundleActionExecutor = BundleActionExecutor;
|
---|