UNPKG

@showbridge/lib

Version:

Main library for showbridge protocol router

68 lines (67 loc) 2.26 kB
import { cloneDeep, has } from 'lodash-es'; import { EventEmitter } from 'node:events'; import { TransformTypeClassMap } from '../transforms/index.js'; import { Templating, disabled } from '../utils/index.js'; class Action extends EventEmitter { constructor(actionObj) { super(); this.obj = actionObj; this.transforms = []; this.loadTransforms(); } loadTransforms() { if (this.obj.transforms) { // NOTE(jwetzell): turn transform JSON into class instances this.transforms = this.obj.transforms .filter((transform) => has(TransformTypeClassMap, transform.type)) .map((transform) => new TransformTypeClassMap[transform.type](transform)); } } resolveTemplatedParams(data) { return Templating.resolveAllKeys(this.params, data); } get type() { return this.obj.type; } get params() { return this.obj.params; } get enabled() { return this.obj.enabled && !disabled.actions.has(this.type); } get comment() { return this.obj.comment; } getTransformedMessage(msg, vars) { // NOTE(jwetzell): short circuit if there is no transforms to do if (this.transforms.length === 0) { return msg; } // NOTE(jwetzell): we don't want to alter the original msg if we are going to transform const msgCopy = cloneDeep(msg); this.transforms.forEach((transform, transformIndex) => { transform.transform(msgCopy, vars); this.emit('transform', `transforms/${transformIndex}`, transform.enabled); }); return msgCopy; } // eslint-disable-next-line no-underscore-dangle, no-unused-vars _run(msg, vars, protocols) { } run(msg, vars, protocols) { if (!this.enabled) { this.emit('finished'); return; } this._run(msg, vars, protocols); } toJSON() { return { comment: this.comment, type: this.type, params: this.params, transforms: this.transforms.map((transform) => transform.toJSON()), enabled: this.enabled, }; } } export default Action;