Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save hendrikswan/c8013d06e3331ae3320700fa7bb105c4 to your computer and use it in GitHub Desktop.
const pendingItems = new Rx.Subject();
const failedItems = new Rx.Subject();
const syncedItems = 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) {
failedItems.next(workingItem);
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 => syncedItems.next(itemWithMetaData))
.catch((reason) => {
queue(itemWithMetaData);
});
});
syncedItems.subscribe(x => console.log('successfully synced a mouse event ', x));
failedItems.subscribe(x => console.error('failed to sync mouse event ', x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment