From 1d008f6f69b360fcfaf5d6715522a145c29157d0 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 2 Oct 2024 07:24:18 +0200 Subject: [PATCH] Always initialize local heartbeats db --- src/core/WakaTimeCore.ts | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts index f6fd7ea..e1829a9 100644 --- a/src/core/WakaTimeCore.ts +++ b/src/core/WakaTimeCore.ts @@ -18,7 +18,7 @@ class WakaTimeCore { lastHeartbeat: Heartbeat | undefined; lastHeartbeatSentAt = 0; lastExtensionState: ExtensionStatus = 'allGood'; - db: IDBPDatabase | undefined; + _db: IDBPDatabase | undefined; constructor() { this.tabsWithDevtoolsOpen = []; } @@ -27,15 +27,18 @@ class WakaTimeCore { * Creates a IndexDB using idb https://github.com/jakearchibald/idb * a library that adds promises to IndexedDB and makes it easy to use */ - async createDB() { - const dbConnection = await openDB('wakatime', 1, { - upgrade(db) { - db.createObjectStore(config.queueName, { - keyPath: 'id', - }); - }, - }); - this.db = dbConnection; + async db() { + if (!this._db) { + const dbConnection = await openDB('wakatime', 1, { + upgrade(db) { + db.createObjectStore(config.queueName, { + keyPath: 'id', + }); + }, + }); + this._db = dbConnection; + } + return this._db; } shouldSendHeartbeat(heartbeat: Heartbeat): boolean { @@ -115,7 +118,7 @@ class WakaTimeCore { if (!this.shouldSendHeartbeat(heartbeat)) return; // append heartbeat to queue - await this.db?.add(config.queueName, heartbeat); + await (await this.db()).add(config.queueName, heartbeat); await this.sendHeartbeats(); } @@ -177,14 +180,14 @@ class WakaTimeCore { 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[] | undefined; if (!heartbeats || heartbeats.length === 0) return; await Promise.all( - heartbeats.map((heartbeat) => { - return this.db?.delete(config.queueName, heartbeat.id); + heartbeats.map(async (heartbeat) => { + return (await this.db()).delete(config.queueName, heartbeat.id); }), ); @@ -227,7 +230,6 @@ class WakaTimeCore { console.error(resp[0].error); } else if (resp[1] === 201 && resp[0].data?.id) { await changeExtensionStatus('allGood'); - // await this.db?.delete(config.queueName, resp[0].data.id); } else { if (resp[1] !== 400) { await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber)); @@ -251,7 +253,7 @@ class WakaTimeCore { async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise { await Promise.all( - heartbeats.map(async (heartbeat) => this.db?.add(config.queueName, heartbeat)), + heartbeats.map(async (heartbeat) => (await this.db()).add(config.queueName, heartbeat)), ); }