chore: change localStorage to indexedDB for caching heartbeats while offline (#166)

This commit is contained in:
Juan Sebastian velez Posada
2023-02-13 19:29:26 -05:00
committed by GitHub
parent be368b520e
commit 0a2326948c
4 changed files with 73 additions and 31 deletions

11
package-lock.json generated
View File

@@ -13,6 +13,7 @@
"classnames": "^2.3.2", "classnames": "^2.3.2",
"create-react-class": "^15.7.0", "create-react-class": "^15.7.0",
"font-awesome": "4.7.0", "font-awesome": "4.7.0",
"idb": "^7.1.1",
"jquery": "^3.6.3", "jquery": "^3.6.3",
"moment": "^2.29.4", "moment": "^2.29.4",
"react": "^18.2.0", "react": "^18.2.0",
@@ -11899,6 +11900,11 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/idb": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/idb/-/idb-7.1.1.tgz",
"integrity": "sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ=="
},
"node_modules/ieee754": { "node_modules/ieee754": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
@@ -32397,6 +32403,11 @@
"safer-buffer": ">= 2.1.2 < 3" "safer-buffer": ">= 2.1.2 < 3"
} }
}, },
"idb": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/idb/-/idb-7.1.1.tgz",
"integrity": "sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ=="
},
"ieee754": { "ieee754": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",

View File

@@ -33,6 +33,7 @@
"classnames": "^2.3.2", "classnames": "^2.3.2",
"create-react-class": "^15.7.0", "create-react-class": "^15.7.0",
"font-awesome": "4.7.0", "font-awesome": "4.7.0",
"idb": "^7.1.1",
"jquery": "^3.6.3", "jquery": "^3.6.3",
"moment": "^2.29.4", "moment": "^2.29.4",
"react": "^18.2.0", "react": "^18.2.0",

View File

@@ -10,11 +10,7 @@ browser.alarms.onAlarm.addListener(async (alarm) => {
// Checks if the user is online and if there are cached heartbeats requests, // Checks if the user is online and if there are cached heartbeats requests,
// if so then procedd to send these payload to wakatime api // if so then procedd to send these payload to wakatime api
if (navigator.onLine) { if (navigator.onLine) {
const { cachedHeartbeats } = await browser.storage.sync.get({ await WakaTimeCore.sendCachedHeartbeatsRequest();
cachedHeartbeats: [],
});
await browser.storage.sync.set({ cachedHeartbeats: [] });
await WakaTimeCore.sendCachedHeartbeatsRequest(cachedHeartbeats as Record<string, unknown>[]);
} }
} }
}); });
@@ -54,3 +50,11 @@ browser.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
} }
} }
}); });
/**
* Creates IndexedDB
* https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
*/
self.addEventListener('activate', async () => {
await WakaTimeCore.createDB();
});

View File

@@ -1,6 +1,9 @@
/* eslint-disable no-fallthrough */
/* eslint-disable default-case */
import axios, { AxiosResponse } from 'axios'; import axios, { AxiosResponse } from 'axios';
import moment from 'moment'; import moment from 'moment';
import browser, { Tabs } from 'webextension-polyfill'; import browser, { Tabs } from 'webextension-polyfill';
import { IDBPDatabase, openDB } from 'idb';
import { AxiosUserResponse, User } from '../types/user'; import { AxiosUserResponse, User } from '../types/user';
import config from '../config/config'; import config from '../config/config';
import { SummariesPayload, GrandTotal } from '../types/summaries'; import { SummariesPayload, GrandTotal } from '../types/summaries';
@@ -11,10 +14,36 @@ import getDomainFromUrl from '../utils/getDomainFromUrl';
class WakaTimeCore { class WakaTimeCore {
tabsWithDevtoolsOpen: Tabs.Tab[]; tabsWithDevtoolsOpen: Tabs.Tab[];
db: IDBPDatabase | undefined;
constructor() { constructor() {
this.tabsWithDevtoolsOpen = []; this.tabsWithDevtoolsOpen = [];
} }
/**
* 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, oldVersion) {
// Create a store of objects
const store = db.createObjectStore('cacheHeartbeats', {
// The `time` property of the object will be the key, and be incremented automatically
keyPath: 'time',
});
// Switch over the oldVersion, *without breaks*, to allow the database to be incrementally upgraded.
switch (oldVersion) {
case 0:
// Placeholder to execute when database is created (oldVersion is 0)
case 1:
// Create an index called `type` based on the `type` property of objects in the store
store.createIndex('time', 'time');
}
},
});
this.db = dbConnection;
}
setTabsWithDevtoolsOpen(tabs: Tabs.Tab[]): void { setTabsWithDevtoolsOpen(tabs: Tabs.Tab[]): void {
this.tabsWithDevtoolsOpen = tabs; this.tabsWithDevtoolsOpen = tabs;
} }
@@ -278,21 +307,18 @@ class WakaTimeCore {
* @param method * @param method
* @returns {*} * @returns {*}
*/ */
async sendPostRequestToApi(payload: Record<string, unknown>, apiKey = '') { async sendPostRequestToApi(payload: Record<string, unknown>, apiKey = ''): Promise<void> {
try { try {
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, { const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, {
body: JSON.stringify(payload), body: JSON.stringify(payload),
method: 'POST', method: 'POST',
}); });
const data = await response.json(); await response.json();
return data;
} catch (err: unknown) { } catch (err: unknown) {
// Stores the payload of the request to be send later if (this.db) {
const { cachedHeartbeats } = await browser.storage.sync.get({ await this.db.add('cacheHeartbeats', payload);
cachedHeartbeats: [], }
});
cachedHeartbeats.push(payload);
await browser.storage.sync.set({ cachedHeartbeats });
await changeExtensionState('notSignedIn'); await changeExtensionState('notSignedIn');
} }
} }
@@ -301,22 +327,21 @@ class WakaTimeCore {
* Sends cached heartbeats request to wakatime api * Sends cached heartbeats request to wakatime api
* @param requests * @param requests
*/ */
async sendCachedHeartbeatsRequest(requests: Record<string, unknown>[]): Promise<void> { async sendCachedHeartbeatsRequest(): Promise<void> {
const apiKey = await this.getApiKey(); const apiKey = await this.getApiKey();
if (!apiKey) { if (!apiKey) {
return changeExtensionState('notLogging'); return changeExtensionState('notLogging');
} }
if (this.db) {
const requests = await this.db.getAll('cacheHeartbeats');
await this.db.clear('cacheHeartbeats');
const chunkSize = 50; // Create batches of max 50 request const chunkSize = 50; // Create batches of max 50 request
for (let i = 0; i < requests.length; i += chunkSize) { for (let i = 0; i < requests.length; i += chunkSize) {
const chunk = requests.slice(i, i + chunkSize); const chunk = requests.slice(i, i + chunkSize);
const requestsPromises: Promise<Response>[] = []; const requestsPromises: Promise<void>[] = [];
chunk.forEach((request) => chunk.forEach((request: Record<string, unknown>) =>
requestsPromises.push( requestsPromises.push(this.sendPostRequestToApi(request, apiKey)),
fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, {
body: JSON.stringify(request),
method: 'POST',
}),
),
); );
try { try {
await Promise.all(requestsPromises); await Promise.all(requestsPromises);
@@ -326,5 +351,6 @@ class WakaTimeCore {
} }
} }
} }
}
export default new WakaTimeCore(); export default new WakaTimeCore();