UNPKG

@rushstack/operation-graph

Version:

Library for managing and executing operations in a directed acyclic graph.

62 lines 2.15 kB
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import { InternalError } from '@rushstack/node-core-library'; import { OperationStatus } from './OperationStatus'; import { Stopwatch } from './Stopwatch'; /** * Meta-entity that tracks information about a group of related operations. * * @beta */ export class OperationGroupRecord { get duration() { return this._groupStopwatch ? this._groupStopwatch.duration : 0; } get finished() { return this._remainingOperations.size === 0; } get hasCancellations() { return this._hasCancellations; } get hasFailures() { return this._hasFailures; } constructor(name, metadata = {}) { this._operations = new Set(); this._remainingOperations = new Set(); this._groupStopwatch = new Stopwatch(); this._hasCancellations = false; this._hasFailures = false; this.name = name; this.metadata = metadata; } addOperation(operation) { this._operations.add(operation); } startTimer() { // Keep this undefined until needed, then start to avoid subsequent calls to startTimer() this._groupStopwatch.start(); } setOperationAsComplete(operation, state) { if (!this._remainingOperations.has(operation)) { throw new InternalError(`Operation ${operation.name} is not in the group ${this.name}`); } if (state.status === OperationStatus.Aborted) { this._hasCancellations = true; } else if (state.status === OperationStatus.Failure) { this._hasFailures = true; } this._remainingOperations.delete(operation); if (this._remainingOperations.size === 0) { this._groupStopwatch.stop(); } } reset() { this._remainingOperations = new Set(this._operations); this._groupStopwatch.reset(); this._hasCancellations = false; this._hasFailures = false; } } //# sourceMappingURL=OperationGroupRecord.js.map