Always initialize local heartbeats db
This commit is contained in:
@@ -18,7 +18,7 @@ class WakaTimeCore {
|
|||||||
lastHeartbeat: Heartbeat | undefined;
|
lastHeartbeat: Heartbeat | undefined;
|
||||||
lastHeartbeatSentAt = 0;
|
lastHeartbeatSentAt = 0;
|
||||||
lastExtensionState: ExtensionStatus = 'allGood';
|
lastExtensionState: ExtensionStatus = 'allGood';
|
||||||
db: IDBPDatabase | undefined;
|
_db: IDBPDatabase | undefined;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.tabsWithDevtoolsOpen = [];
|
this.tabsWithDevtoolsOpen = [];
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,8 @@ class WakaTimeCore {
|
|||||||
* Creates a IndexDB using idb https://github.com/jakearchibald/idb
|
* Creates a IndexDB using idb https://github.com/jakearchibald/idb
|
||||||
* a library that adds promises to IndexedDB and makes it easy to use
|
* a library that adds promises to IndexedDB and makes it easy to use
|
||||||
*/
|
*/
|
||||||
async createDB() {
|
async db() {
|
||||||
|
if (!this._db) {
|
||||||
const dbConnection = await openDB('wakatime', 1, {
|
const dbConnection = await openDB('wakatime', 1, {
|
||||||
upgrade(db) {
|
upgrade(db) {
|
||||||
db.createObjectStore(config.queueName, {
|
db.createObjectStore(config.queueName, {
|
||||||
@@ -35,7 +36,9 @@ class WakaTimeCore {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
this.db = dbConnection;
|
this._db = dbConnection;
|
||||||
|
}
|
||||||
|
return this._db;
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldSendHeartbeat(heartbeat: Heartbeat): boolean {
|
shouldSendHeartbeat(heartbeat: Heartbeat): boolean {
|
||||||
@@ -115,7 +118,7 @@ class WakaTimeCore {
|
|||||||
if (!this.shouldSendHeartbeat(heartbeat)) return;
|
if (!this.shouldSendHeartbeat(heartbeat)) return;
|
||||||
|
|
||||||
// append heartbeat to queue
|
// append heartbeat to queue
|
||||||
await this.db?.add(config.queueName, heartbeat);
|
await (await this.db()).add(config.queueName, heartbeat);
|
||||||
|
|
||||||
await this.sendHeartbeats();
|
await this.sendHeartbeats();
|
||||||
}
|
}
|
||||||
@@ -177,14 +180,14 @@ class WakaTimeCore {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const heartbeats = (await this.db?.getAll(config.queueName, undefined, 50)) as
|
const heartbeats = (await (await this.db()).getAll(config.queueName, undefined, 50)) as
|
||||||
| Heartbeat[]
|
| Heartbeat[]
|
||||||
| undefined;
|
| undefined;
|
||||||
if (!heartbeats || heartbeats.length === 0) return;
|
if (!heartbeats || heartbeats.length === 0) return;
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
heartbeats.map((heartbeat) => {
|
heartbeats.map(async (heartbeat) => {
|
||||||
return this.db?.delete(config.queueName, heartbeat.id);
|
return (await this.db()).delete(config.queueName, heartbeat.id);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -227,7 +230,6 @@ class WakaTimeCore {
|
|||||||
console.error(resp[0].error);
|
console.error(resp[0].error);
|
||||||
} else if (resp[1] === 201 && resp[0].data?.id) {
|
} else if (resp[1] === 201 && resp[0].data?.id) {
|
||||||
await changeExtensionStatus('allGood');
|
await changeExtensionStatus('allGood');
|
||||||
// await this.db?.delete(config.queueName, resp[0].data.id);
|
|
||||||
} else {
|
} else {
|
||||||
if (resp[1] !== 400) {
|
if (resp[1] !== 400) {
|
||||||
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
|
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
|
||||||
@@ -251,7 +253,7 @@ class WakaTimeCore {
|
|||||||
|
|
||||||
async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise<void> {
|
async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise<void> {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
heartbeats.map(async (heartbeat) => this.db?.add(config.queueName, heartbeat)),
|
heartbeats.map(async (heartbeat) => (await this.db()).add(config.queueName, heartbeat)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user