truss/emitters/index.js
2019-03-05 19:21:09 -06:00

34 lines
869 B
JavaScript

// 2nd.
const {sendActionToService} = require('@truss/comm');
const debug = require('debug')('truss:emitters');
exports.ActionEmitter = class ActionEmitter {
constructor(executors, listeners) {
this.executors = executors;
this.listeners = listeners;
}
onExecutorResponse(res, eres) {
}
onNoExecutor(res) {
}
processAction(action, res) {
debug(`Processing ${action.type}...`);
// listeners...
for (service of this.listeners[action.type] || []) {
sendActionToService(action, service).done();
}
// executor
if (!(action.type in this.executors)) {
debug(`No executor for ${action.type}!`);
this.onNoExecutor(action, res);
return;
}
sendActionToService(action, this.executors[action.type]).then((eres) => {
this.onExecutorResponse(action, res, eres);
}).catch(console.error);
}
}