Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Last active May 12, 2016 15:57
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 designfrontier/1c40b523cafda4350d86e03b3190636c to your computer and use it in GitHub Desktop.
Save designfrontier/1c40b523cafda4350d86e03b3190636c to your computer and use it in GitHub Desktop.
pseudo-ish code change queue
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;
}
}
@dandorman
Copy link

Seems pretty legit!

@designfrontier
Copy link
Author

a couple other things I am thinking about... the particular item in the queue should probably only exist once. So If I save a new thing it should replace the existing instance of the thing that is in the queue. Since we are doing PUTs instead of PATCHs.

It should probably notify the user in some way that things are less then optimal... and have a smarter retry based around the response codes.

@designfrontier
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment