angular-docs-cn/packages/compiler-cli/ngcc/src/execution/single_process_executor.ts
Pete Bacon Darwin 39d4016fe9 refactor(ngcc): abstract onTaskCompleted out of executors (#36083)
Moving the definition of the `onTaskCompleted` callback into `mainNgcc()`
allows it to be configured based on options passed in there more easily.
This will be the case when we want to configure whether to log or throw
an error for tasks that failed to be processed successfully.

This commit also creates two new folders and moves the code around a bit
to make it easier to navigate the code§:

* `execution/tasks`: specific helpers such as task completion handlers
* `execution/tasks/queues`: the `TaskQueue` implementations and helpers

PR Close #36083
2020-03-18 15:56:21 -07:00

71 lines
2.5 KiB
TypeScript

/**
* @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 {AsyncLocker} from '../locking/async_locker';
import {SyncLocker} from '../locking/sync_locker';
import {Logger} from '../logging/logger';
import {AnalyzeEntryPointsFn, CreateCompileFn, Executor} from './api';
import {CreateTaskCompletedCallback} from './tasks/api';
export abstract class SingleProcessorExecutorBase {
constructor(
private logger: Logger, private createTaskCompletedCallback: CreateTaskCompletedCallback) {}
doExecute(analyzeEntryPoints: AnalyzeEntryPointsFn, createCompileFn: CreateCompileFn):
void|Promise<void> {
this.logger.debug(`Running ngcc on ${this.constructor.name}.`);
const taskQueue = analyzeEntryPoints();
const onTaskCompleted = this.createTaskCompletedCallback(taskQueue);
const compile = createCompileFn(onTaskCompleted);
// Process all tasks.
this.logger.debug('Processing tasks...');
const startTime = Date.now();
while (!taskQueue.allTasksCompleted) {
const task = taskQueue.getNextTask() !;
compile(task);
taskQueue.markTaskCompleted(task);
}
const duration = Math.round((Date.now() - startTime) / 1000);
this.logger.debug(`Processed tasks in ${duration}s.`);
}
}
/**
* An `Executor` that processes all tasks serially and completes synchronously.
*/
export class SingleProcessExecutorSync extends SingleProcessorExecutorBase implements Executor {
constructor(
logger: Logger, private lockFile: SyncLocker,
createTaskCompletedCallback: CreateTaskCompletedCallback) {
super(logger, createTaskCompletedCallback);
}
execute(analyzeEntryPoints: AnalyzeEntryPointsFn, createCompileFn: CreateCompileFn): void {
this.lockFile.lock(() => this.doExecute(analyzeEntryPoints, createCompileFn));
}
}
/**
* An `Executor` that processes all tasks serially, but still completes asynchronously.
*/
export class SingleProcessExecutorAsync extends SingleProcessorExecutorBase implements Executor {
constructor(
logger: Logger, private lockFile: AsyncLocker,
createTaskCompletedCallback: CreateTaskCompletedCallback) {
super(logger, createTaskCompletedCallback);
}
async execute(analyzeEntryPoints: AnalyzeEntryPointsFn, createCompileFn: CreateCompileFn):
Promise<void> {
await this.lockFile.lock(async() => this.doExecute(analyzeEntryPoints, createCompileFn));
}
}