Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Solving race condition between flushes. #316

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class Analytics {
* @return {Analytics}
*/

identify (message, callback) {
async identify (message, callback) {
this._validate(message, 'identify')
this.enqueue('identify', message, callback)
await this.enqueue('identify', message, callback)
return this
}

Expand All @@ -93,9 +93,9 @@ class Analytics {
* @return {Analytics}
*/

group (message, callback) {
async group (message, callback) {
this._validate(message, 'group')
this.enqueue('group', message, callback)
await this.enqueue('group', message, callback)
return this
}

Expand All @@ -107,9 +107,9 @@ class Analytics {
* @return {Analytics}
*/

track (message, callback) {
async track (message, callback) {
this._validate(message, 'track')
this.enqueue('track', message, callback)
await this.enqueue('track', message, callback)
return this
}

Expand All @@ -121,9 +121,9 @@ class Analytics {
* @return {Analytics}
*/

page (message, callback) {
async page (message, callback) {
this._validate(message, 'page')
this.enqueue('page', message, callback)
await this.enqueue('page', message, callback)
return this
}

Expand All @@ -135,9 +135,9 @@ class Analytics {
* @return {Analytics}
*/

screen (message, callback) {
async screen (message, callback) {
this._validate(message, 'screen')
this.enqueue('screen', message, callback)
await this.enqueue('screen', message, callback)
return this
}

Expand All @@ -149,9 +149,9 @@ class Analytics {
* @return {Analytics}
*/

alias (message, callback) {
async alias (message, callback) {
this._validate(message, 'alias')
this.enqueue('alias', message, callback)
await this.enqueue('alias', message, callback)
return this
}

Expand Down Expand Up @@ -211,19 +211,19 @@ class Analytics {

if (!this.flushed) {
this.flushed = true
this.flush()
await this.flush()
return
}

const hasReachedFlushAt = this.queue.length >= this.flushAt
const hasReachedQueueSize = this.queue.reduce((acc, item) => acc + JSON.stringify(item).length, 0) >= this.maxQueueSize
if (hasReachedFlushAt || hasReachedQueueSize) {
this.flush()
await this.flush()
return
}

if (this.flushInterval && !this.timer) {
this.timer = setTimeout(this.flush.bind(this), this.flushInterval)
this.timer = setTimeout(await this.flush.bind(this), this.flushInterval)
}
}

Expand Down