Skip to content

Instantly share code, notes, and snippets.

@antonioaguilar
Created October 31, 2019 20:40
Show Gist options
  • Save antonioaguilar/79a641bdd875c59b76c8404ccd6db610 to your computer and use it in GitHub Desktop.
Save antonioaguilar/79a641bdd875c59b76c8404ccd6db610 to your computer and use it in GitHub Desktop.
Simple event aggregator using Lodash
const _ = require('lodash');
const colors = require('colors/safe');
const EventEmitter = require('events').EventEmitter;
const ev = new EventEmitter();
const predicate = ['q3', 'q6', 'q9'];
let agg = {};
ev.on('query.event', (event) => {
agg[event.id] = event;
const resolved = _.every(predicate, _.partial(_.has, agg));
console.log(colors.gray(event));
if (resolved) {
const results = _.values(_.pick(agg, predicate));
const condition = _.every(results, o => { return o.value >= 5 && o.value <= 8; });
if (condition) {
console.log(colors.yellow(`predicate condition resolved`));
console.log(colors.green(JSON.stringify(results, null, 2)));
// agg = {};
}
}
});
setInterval(() => {
ev.emit('query.event', { id: 'q' + _.random(1, 10), value: _.random(1, 10), timestamp: new Date().toISOString() });
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment