Created
June 8, 2016 08:17
-
-
Save poshaughnessy/4588164376544ea1a168cb0026ed27c0 to your computer and use it in GitHub Desktop.
Thanks @export-mike
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
function sync(queue) { | |
// do some async operation | |
} | |
function syncFail(failedEvents, queue) { | |
failedEvents.forEach(e => queue.enqueue(e)); | |
queue.set([...failedEvents, queue]); | |
} | |
function syncSuccess() { | |
// we don't need to do anything all good! | |
} | |
// queue.js | |
let _queue = []; | |
export const enqueue = (event) => { | |
_queue.push(event); | |
}; | |
export const dequeue = () => { | |
return _queue.pop(); | |
} | |
export const dequeueAll = () => { | |
const oldQueue = _queue; | |
_queue = []; | |
return oldQueue; | |
} | |
// main.js | |
import * as queue from './queue' | |
main() { | |
setTimeout(() => { | |
const batchOfEvents = queue.dequeueAll(); | |
sync(batchOfEvents) | |
.then(() => syncSuccess()) | |
.catch(() => syncFail(batchOfEvents, queue)); | |
}, TIMEOUT); | |
} | |
onEvent(event) { | |
queue.enqueue(event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment