Skip to content

Instantly share code, notes, and snippets.

@hendrikswan
Last active June 15, 2017 07:41
Show Gist options
  • Select an option

  • Save hendrikswan/1721a51fac39f585cbb8a8afdac533f1 to your computer and use it in GitHub Desktop.

Select an option

Save hendrikswan/1721a51fac39f585cbb8a8afdac533f1 to your computer and use it in GitHub Desktop.
const pendingItems = new Rx.Subject();
const maxRetries = 3;
const queue = (item) => {
let workingItem = item;
if (!workingItem.sync) {
workingItem = {
sync: { counter: 0 },
item,
};
}
if (item.sync.counter > 0 && item.sync.counter >= maxRetries) {
console.error('Failed to execute the sync logic for an item: ', workingItem.item);
return;
}
const workingItem = Object.assign({}, item, {
sync: {
counter: item.sync.counter + 1,
lastTry: new Date(),
},
});
pendingItems.next(workingItem);
}
pendingItems.subscribe((itemWithMetaData) => {
api.syncEvent(itemWithMetaData.item)
.then((result) => {
console.log('successfully synced a mouse event');
})
.catch((reason) => {
console.error('got an error while trying to sync an event, going to try again');
queue(itemWithMetaData);
});
});
function syncMouseEvent(event) {
queue({
x: event.clientX,
y: event.clientY
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment