From 5f96d9fcc56f642c98ee241e54969a2e0d0246b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20Pe=C3=B1a?= Date: Fri, 4 Nov 2022 14:59:21 -0300 Subject: [PATCH 1/5] flush: ensure previous flush completion --- index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 66a8427f..1da08396 100644 --- a/index.js +++ b/index.js @@ -52,6 +52,7 @@ class Analytics { this.flushInterval = options.flushInterval || 10000 this.flushed = false this.errorHandler = options.errorHandler + this.pendingFlush = null Object.defineProperty(this, 'enable', { configurable: false, writable: false, @@ -236,7 +237,8 @@ class Analytics { * @return {Analytics} */ - flush (callback) { + async flush (callback) { + await this.pendingFlush callback = callback || noop if (!this.enable) { @@ -291,7 +293,8 @@ class Analytics { req.timeout = typeof this.timeout === 'string' ? ms(this.timeout) : this.timeout } - return this.axiosInstance.post(`${this.host}${this.path}`, data, req) + return (this.pendingFlush = this.axiosInstance + .post(`${this.host}${this.path}`, data, req) .then(() => { done() return Promise.resolve(data) @@ -310,7 +313,7 @@ class Analytics { done(err) throw err - }) + })) } _isErrorRetryable (error) { From 7428915df9aefe05feb5acde2ae170ddffe56e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20Pe=C3=B1a?= Date: Fri, 4 Nov 2022 15:21:26 -0300 Subject: [PATCH 2/5] allow recovering from failed flushs --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1da08396..37821a50 100644 --- a/index.js +++ b/index.js @@ -237,9 +237,13 @@ class Analytics { * @return {Analytics} */ - async flush (callback) { - await this.pendingFlush - callback = callback || noop + async flush(callback) { + { + const pending = this.pendingFlush; + this.pendingFlush = null; + await pending; // this may throw + } + callback = callback || noop; if (!this.enable) { setImmediate(callback) From 51dafbc6c0d6646f2ff432638c9ed9ab0d7a63b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20Pe=C3=B1a?= Date: Mon, 7 Nov 2022 14:30:15 -0300 Subject: [PATCH 3/5] standard --fix --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 37821a50..4af5b43c 100644 --- a/index.js +++ b/index.js @@ -237,13 +237,13 @@ class Analytics { * @return {Analytics} */ - async flush(callback) { + async flush (callback) { { - const pending = this.pendingFlush; - this.pendingFlush = null; - await pending; // this may throw + const pending = this.pendingFlush + this.pendingFlush = null + await pending // this may throw } - callback = callback || noop; + callback = callback || noop if (!this.enable) { setImmediate(callback) From fda46516cc647d1ee6a7bf22bc21952cd55e4dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20Pe=C3=B1a?= Date: Mon, 7 Nov 2022 18:11:14 -0300 Subject: [PATCH 4/5] fix error handling --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4af5b43c..95911498 100644 --- a/index.js +++ b/index.js @@ -238,10 +238,11 @@ class Analytics { */ async flush (callback) { - { - const pending = this.pendingFlush + try { + await this.pendingFlush + } catch (err) { this.pendingFlush = null - await pending // this may throw + throw err } callback = callback || noop From edc3ac2a0d5147528dfcfe37fe0e1abab9a69742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20Pe=C3=B1a?= Date: Mon, 7 Nov 2022 18:14:55 -0300 Subject: [PATCH 5/5] first check for reasons not to flush and then for pending flushes --- index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 95911498..93842b72 100644 --- a/index.js +++ b/index.js @@ -238,12 +238,6 @@ class Analytics { */ async flush (callback) { - try { - await this.pendingFlush - } catch (err) { - this.pendingFlush = null - throw err - } callback = callback || noop if (!this.enable) { @@ -261,6 +255,13 @@ class Analytics { return Promise.resolve() } + try { + if (this.pendingFlush) { await this.pendingFlush } + } catch (err) { + this.pendingFlush = null + throw err + } + const items = this.queue.splice(0, this.flushAt) const callbacks = items.map(item => item.callback) const messages = items.map(item => item.message)