Created
June 15, 2017 09:26
-
-
Save hendrikswan/c8013d06e3331ae3320700fa7bb105c4 to your computer and use it in GitHub Desktop.
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 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