Last active
March 27, 2024 11:46
-
-
Save noamr/88d5c1e33e9558986a9da075ba623d82 to your computer and use it in GitHub Desktop.
Use of `fetchLater` in an async way
This file contains 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 events = []; | |
let current_dequeue_end = 0; | |
function dequeue() { | |
// Ready to process, will send all current events. | |
current_dequeue_end = events.length; | |
fetchLater(makeURL(events)).then(({activated}) => { | |
if (activated) { | |
// Success, remove processed sub-array from the array and reset the index. | |
events.splice(0, current_deque_end); | |
current_dequeue_end = 0; | |
} else { | |
// abort the previous fetchLater | |
} | |
// If more events were added while processing, process them now. | |
if (current_dequeue_end != events.length) { | |
dequeue(); | |
} | |
}).catch(error => { | |
// Quota error? handle | |
}); | |
} | |
function newEvent(event) { | |
events.push(event); | |
if (!current_dequeue_end) | |
dequeue(); | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment