Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit 188c814

Browse files
authored
fix: catch flush error and send to callback (#1)
* fix: catch flush error and send to callback * release: 0.1.1
1 parent 0d253f9 commit 188c814

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sensorsdata-sdk",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "SDK of sensorsdata Written in Typescript",
55
"main": "dist/src/sensors-analytics.js",
66
"scripts": {

src/sensors-analytics.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,27 @@ export class SensorsAnalytics extends Writable {
129129

130130
this.dataQueue.push(envelope)
131131
const now = Date.now()
132-
if (
133-
this.debug ||
134-
this.dataQueue.length >= this.options.buffCount ||
135-
now >= this.nextSubmitTime
136-
) {
137-
if (this.timer) {
138-
clearTimeout(this.timer)
132+
try {
133+
if (
134+
this.debug ||
135+
this.dataQueue.length >= this.options.buffCount ||
136+
now >= this.nextSubmitTime
137+
) {
138+
if (this.timer) {
139+
clearTimeout(this.timer)
140+
}
141+
await this.flush()
142+
} else {
143+
if (this.timer) {
144+
clearTimeout(this.timer)
145+
}
146+
this.timer = setTimeout(() => this.flush(), this.nextSubmitTime - now)
139147
}
140-
await this.flush()
141-
} else {
142-
if (this.timer) {
143-
clearTimeout(this.timer)
144-
}
145-
this.timer = setTimeout(() => this.flush(), this.nextSubmitTime - now)
148+
149+
callback()
150+
} catch (err) {
151+
callback(err)
146152
}
147-
callback()
148153
}
149154

150155
async submit(messages: object[]) {
@@ -255,7 +260,7 @@ export class SensorsAnalytics extends Writable {
255260
await this.submit(this.popAllData())
256261
}
257262
async close() {
258-
await new Promise(resolve => {
263+
await new Promise((resolve) => {
259264
super.end(() => resolve())
260265
})
261266
await this.flush()

0 commit comments

Comments
 (0)