@boost/core
Version:
Robust pipeline for creating dev tools that separate logic into routines and tasks.
79 lines (78 loc) • 3.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 = (routine, value) => __awaiter(this, void 0, void 0, function* () {
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 = (task, value) => __awaiter(this, void 0, void 0, function* () {
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;