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);