UNPKG

@boost/core

Version:

Robust pipeline for creating dev tools that separate logic into routines and tasks.

70 lines (69 loc) 2.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const kebabCase_1 = __importDefault(require("lodash/kebabCase")); const common_1 = require("@boost/common"); const debug_1 = require("@boost/debug"); class Executor { constructor(tool, context, options) { this.parallel = false; /** * Execute a routine with the provided value. */ this.executeRoutine = async (routine, value) => { this.tool.console.emit('routine', [routine, value, this.parallel]); this.tool.console.onRoutine.emit([routine, value, this.parallel]); return routine.run(this.context, value); }; /** * Execute a task with the provided value. */ this.executeTask = async (task, value) => { this.tool.console.emit('task', [task, value, this.parallel]); this.tool.console.onTask.emit([task, value, this.parallel]); return task.run(this.context, value); }; this.context = context; this.debug = debug_1.createDebugger(kebabCase_1.default(this.constructor.name)); this.tool = tool; // @ts-ignore Allow spread this.options = Object.assign({}, options); this.debug('Instantiating task executor'); } /** * Aggregate and partition errors and results into separate collections. */ aggregateResponse(responses) { const results = []; const errors = []; this.debug('Aggregating results'); responses.forEach((response) => { if (common_1.instanceOf(response, Error)) { errors.push(response); } else { results.push(response); } }); return { errors, results }; } /** * Run all routines with the defined executor. */ runRoutines(routines, value) { this.tool.console.emit(this.parallel ? 'routines.parallel' : 'routines', [routines, value]); this.tool.console.onRoutines.emit([routines, value, this.parallel]); return this.run(this.executeRoutine, routines, value); } /** * Run all tasks with the defined executor. */ runTasks(tasks, value) { this.tool.console.emit(this.parallel ? 'tasks.parallel' : 'tasks', [tasks, value]); this.tool.console.onTasks.emit([tasks, value, this.parallel]); return this.run(this.executeTask, tasks, value); } } exports.default = Executor;