Skip to content

Instantly share code, notes, and snippets.

@mikesurowiec
Created July 23, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesurowiec/9eedef4e90fd61ca2895d8614f79a9dd to your computer and use it in GitHub Desktop.
Save mikesurowiec/9eedef4e90fd61ca2895d8614f79a9dd to your computer and use it in GitHub Desktop.
DC Metro Pro synchronization logic
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