From 3a0c50fb440fe7c1217b3b8d39434195523f7e50 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 25 Oct 2019 11:48:28 -0700 Subject: [PATCH] Batcher: Enable faster shutdown by flushing immediately --- ts/util/batcher.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ts/util/batcher.ts b/ts/util/batcher.ts index 55c711d9a4..f4e620dd87 100644 --- a/ts/util/batcher.ts +++ b/ts/util/batcher.ts @@ -6,7 +6,7 @@ window.batchers = []; // @ts-ignore window.waitForAllBatchers = async () => { // @ts-ignore - await Promise.all(window.batchers.map(item => item.onIdle())); + await Promise.all(window.batchers.map(item => item.flushAndWait())); }; type BatcherOptionsType = { @@ -19,6 +19,7 @@ type BatcherType = { add: (item: ItemType) => void; anyPending: () => boolean; onIdle: () => Promise; + flushAndWait: () => Promise; unregister: () => void; }; @@ -83,10 +84,23 @@ export function createBatcher( window.batchers = window.batchers.filter((item: any) => item !== batcher); } + async function flushAndWait() { + if (timeout) { + clearTimeout(timeout); + timeout = null; + } + if (items.length) { + _kickBatchOff(); + } + + return onIdle(); + } + batcher = { add, anyPending, onIdle, + flushAndWait, unregister, };