Last active
June 15, 2017 07:41
-
-
Save hendrikswan/1721a51fac39f585cbb8a8afdac533f1 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 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