major refactoring

This commit is contained in:
Dave Horton
2020-01-25 11:47:33 -05:00
parent 621ea8c0f5
commit 4a1ea4e091
25 changed files with 947 additions and 933 deletions

View File

@@ -1,8 +1,8 @@
const Emitter = require('events');
const debug = require('debug')('jambonz:feature-server');
const assert = require('assert');
const resourcesMixin = require('../utils/resources');
const {TaskPreconditions} = require('../utils/constants');
const hooks = require('../utils/notifiers');
const specs = new Map();
const _specData = require('./specs');
for (const key in _specData) {specs.set(key, _specData[key]);}
@@ -15,12 +15,24 @@ class Task extends Emitter {
this.data = data;
this._killInProgress = false;
this._completionPromise = new Promise((resolve) => this._completionResolver = resolve);
}
get killed() {
return this._killInProgress;
}
get callSession() {
return this.cs;
}
async exec(cs) {
this.cs = cs;
const {actionHook, notifyHook} = hooks(this.logger, cs.callInfo);
this.actionHook = actionHook;
this.notifyHook = notifyHook;
}
/**
* called to kill (/stop) a running task
* what to do is up to each type of task
@@ -31,6 +43,24 @@ class Task extends Emitter {
// no-op
}
notifyTaskDone() {
this._completionResolver();
}
awaitTaskDone() {
return this._completionPromise;
}
async performAction(method, auth, results) {
if (this.action) {
const tasks = await this.actionHook(this.action, method, auth, results);
if (tasks && Array.isArray(tasks)) {
this.logger.debug(`${this.name} replacing application with ${tasks.length} tasks`);
this.callSession.replaceApplication(tasks);
}
}
}
static validate(name, data) {
debug(`validating ${name} with data ${JSON.stringify(data)}`);
// validate the instruction is supported
@@ -88,7 +118,5 @@ class Task extends Emitter {
}
}
Object.assign(Task.prototype, resourcesMixin);
module.exports = Task;