UNPKG

@mdf.js/tasks

Version:

MMS - API Core - Tasks

55 lines 1.84 kB
"use strict"; /** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Sequence = void 0; const crash_1 = require("@mdf.js/crash"); const TaskHandler_1 = require("./TaskHandler"); class Sequence extends TaskHandler_1.TaskHandler { /** * Create a new task handler for a sequence of tasks * @param pattern - The pattern for the sequence * @param options - The options for the task */ constructor(pattern, options) { super(options); this.pattern = pattern; } /** Execute the task */ async _execute() { try { await this.executePhase(this.pattern.pre, 'pre'); const result = await this.pattern.task.execute(); this._$meta.push(this.pattern.task.metadata); await this.executePhase(this.pattern.post, 'post'); return result; } catch (error) { throw crash_1.Crash.from(error); } finally { await this.executePhase(this.pattern.finally, 'finally'); } } /** Execute a phase of the sequence */ async executePhase(tasks = [], phase) { for (const task of tasks) { try { await task.execute(); } catch (rawError) { const cause = crash_1.Crash.from(rawError); throw new crash_1.Crash(`Error executing the [${phase}] phase: ${cause.message}`, { cause }); } finally { this._$meta.push(task.metadata); } } } } exports.Sequence = Sequence; //# sourceMappingURL=Sequence.js.map