'use strict'; // 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); } };