Skip to content

Instantly share code, notes, and snippets.

@johnfoderaro
Last active May 3, 2017 01:42
Show Gist options
  • Save johnfoderaro/7467c21608fc4b75f1ff7238eb0e2fbc to your computer and use it in GitHub Desktop.
Save johnfoderaro/7467c21608fc4b75f1ff7238eb0e2fbc to your computer and use it in GitHub Desktop.
857-bytes
'use strict';
const activeBuilds = [];
function socketEmitter(res, type, data) {
// emit the event
res.app.get('io').sockets.emit(type, data);
// store the last event per project for future connections
if (activeBuilds.length === 0) {
activeBuilds.push({ name: data.name, data });
} else {
// loop through array of active projects and update/add as necessary
for (let i = 0; i < activeBuilds.length; i++) {
const match = activeBuilds[i].name === data.name;
const done = i + 1 === activeBuilds.length;
if (match) {
activeBuilds[i].data = data;
return res.app.set('io-last-event', activeBuilds);
}
if (done && !match) {
activeBuilds.push({ name: data.name, data });
return res.app.set('io-last-event', activeBuilds);
}
}
}
}
module.exports = socketEmitter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment