Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active March 21, 2019 18:19
Show Gist options
  • Save jonchurch/aaa7d76531672e239a41112dfd12993d to your computer and use it in GitHub Desktop.
Save jonchurch/aaa7d76531672e239a41112dfd12993d to your computer and use it in GitHub Desktop.
const fs = require("fs");
const { Tail } = require("tail");
const { EventEmitter } = require("events");
const JSON_FILE = "./theFile.json";
const controller = new EventEmitter();
const Services = {};
// controller.on("test", payload => console.log(`This is a test: ${payload}`));
// controller.emit("test", "is this thing on?");
// Register Trigger
controller.on("jsonFile:newLine", function(payload) {
const found = recipeList.filter(
({ trigger: { service, name } }) =>
service === "jsonFile" && name === "newLine"
);
// console.log({ found });
// Find all matching recipes
found.forEach(({ action, trigger }) => {
const actionService = Services[action.service];
const actionMethod = actionService[action.method];
actionMethod({ action, trigger, payload });
});
});
const recipeList = [
{
user: "sure!",
trigger: {
service: "jsonFile",
name: "newLine"
},
action: {
service: "console",
method: "log",
payload: { message: "line" }
}
}
];
// Trigger setup, ingest
// could be a server route
const theFile = new Tail(JSON_FILE);
theFile.on("line", data => {
console.log("yo update");
controller.emit("jsonFile:newLine", { line: data });
});
theFile.on("error", err => console.log(err));
// Services.jsonFile = theFile;
// Action setup
const consoleService = {
log: ({ action, trigger, payload }) => {
// prolly shouldnt interpolate so late in the pipeline
const interpolatedMessage = payload[action.payload.message];
// logging to console is the final action
console.log(interpolatedMessage);
}
};
Services.console = consoleService;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment