From ba2343dd1be7886ece8a05e99f5cb74baeb1e379 Mon Sep 17 00:00:00 2001 From: Christopher Pardy Date: Tue, 22 May 2018 11:19:54 -0400 Subject: [PATCH 1/2] Don't start a timer When the queue has hit the maximum length don't start a timer. If a timer exists flush will cancel it. --- index.js | 1 + test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/index.js b/index.js index 4ee49173..f86c4755 100644 --- a/index.js +++ b/index.js @@ -211,6 +211,7 @@ class Analytics { if (this.queue.length >= this.flushAt) { this.flush() + return } if (this.flushInterval && !this.timer) { diff --git a/test.js b/test.js index 31b224cd..dc7f2bc7 100644 --- a/test.js +++ b/test.js @@ -254,6 +254,22 @@ test('enqueue - don\'t reset an existing timer', async t => { t.true(client.flush.calledOnce) }) +test('enqueue - cancel timer when flushing at max length', async t => { + const client = createClient({ flushAt: 2, flushInterval: 10 }) + client.flushed = false + spy(client, 'flush') + + client.enqueue('type', {}) + t.true(client.flush.calledOnce) + + client.enqueue('type', {}) + client.enqueue('type', {}) + t.true(client.flush.calledTwice) + + await delay(10) + t.true(client.flush.calledTwice) +}) + test('enqueue - extend context', t => { const client = createClient() From cdc7f27e3a0fd6eeb334982d1d8e00fee4408fd8 Mon Sep 17 00:00:00 2001 From: "Shane L. Duvall" Date: Mon, 16 Aug 2021 10:11:00 -0500 Subject: [PATCH 2/2] Update test.js Co-authored-by: Patrick Bassut --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index 53b43d0c..6bdd7992 100644 --- a/test.js +++ b/test.js @@ -267,7 +267,7 @@ test('enqueue - don\'t reset an existing timer', async t => { t.true(client.flush.calledOnce) }) -test('enqueue - cancel timer when flushing at max length', async t => { +test('enqueue - prevent flushing through time interval when already flushed by flushAt', async t => { const client = createClient({ flushAt: 2, flushInterval: 10 }) client.flushed = false spy(client, 'flush')