Last active
May 3, 2017 01:42
-
-
Save johnfoderaro/7467c21608fc4b75f1ff7238eb0e2fbc to your computer and use it in GitHub Desktop.
857-bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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