Created
July 23, 2017 13:45
-
-
Save mikesurowiec/9eedef4e90fd61ca2895d8614f79a9dd to your computer and use it in GitHub Desktop.
DC Metro Pro synchronization logic
This file contains 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
import { EventEmitter } from 'events'; | |
import realtimeAction from './realtime'; | |
import incidentsAction from './incidents'; | |
import advisoriesAction from './advisories'; | |
const eventEmitter = new EventEmitter(); | |
const syncers = [ | |
{ name: 'realtime', action: realtimeAction, interval: 2.5 * 1000 }, | |
{ name: 'incidcents', action: incidentsAction, interval: 60 * 1000 }, | |
{ name: 'advisories', action: advisoriesAction, interval: 60 * 1000 }, | |
]; | |
const loopWithInterval = (syncer) => { | |
syncer.action().then((data) => { | |
eventEmitter.emit(`change:${syncer.name}`, data); | |
setTimeout(() => loopWithInterval(syncer), syncer.interval); | |
}); | |
}; | |
syncers.forEach(loopWithInterval); | |
export default eventEmitter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment