Last active
May 12, 2016 15:57
-
-
Save designfrontier/1c40b523cafda4350d86e03b3190636c to your computer and use it in GitHub Desktop.
pseudo-ish code change queue
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
const saveQueue = [ | |
slide, | |
slide1, | |
slide2, | |
slide | |
]; | |
const attemptedSlides = {}; | |
const running = false; | |
addToQueue (slide) { | |
saveQueue.push(slide); | |
if (!running) { | |
processQueue(); | |
} | |
} | |
processQueue() { | |
const itemToSave = saveQueue.shift(); | |
if (typeof itemToSave !== 'undefined') { | |
running = true; | |
if (attemptedSlides[itemToSave.id]){ | |
attemptedSlides[itemToSave.id] ++; | |
} else { | |
attemptedSlides[itemToSave.id] = 1; | |
} | |
saveFunction(itemToSave).then(() => { | |
attemptedSlides[itemToSave.id] = undefined; | |
setTimeout(processQueue, 0); // setTimeout to release the thread | |
}, () => { | |
if (attemptedSlides[itemToSave.id] < 3) { | |
saveQueue.unshift(itemToSave); | |
setTimeout(processQueue, 100); // wait 100 ms to see if things get better | |
} else { | |
setTimeout(processQueue, 0); // exceeded attempts... sorry man | |
} | |
}); | |
} else { | |
running = false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am also trying to decide between a deep clone when the item is added or just letting it be passed by reference. Pass by reference makes the dedupe easy because you just check to see if that thing is already enqueued and then bail on adding it if it is. Where as the deep clone would allow you to do gradual updates of the object.
I am leaning towards no deep clone... seems a little more elegant to me.