Created
December 4, 2020 15:49
-
-
Save Willmo36/d0a4bbfbd82971db71dc2397edc8fcc0 to your computer and use it in GitHub Desktop.
Microtask batching from https://dev.to/thepassle/litelement-a-deepdive-into-batched-updates-3hh
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
class Batching { | |
updateRequested = false; | |
async scheduleUpdate(id: number) { | |
if (!this.updateRequested) { | |
this.updateRequested = true; | |
this.updateRequested = await false; | |
this.update(id); | |
} | |
} | |
update(id: number) { | |
console.log("updating!", id); | |
} | |
} | |
const batching = new Batching(); | |
/* We call scheduleUpdate in quick succession */ | |
/** will only log once though */ | |
batching.scheduleUpdate(1); | |
batching.scheduleUpdate(2); | |
batching.scheduleUpdate(3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment