mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
major refactoring
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user