Skip to content

Commit

Permalink
Optional storage session for tab stats (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban authored Oct 17, 2023
1 parent bffd4e0 commit 40da99f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extension-manifest-v3/src/background/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import DailyStats, { getMergedStats } from '/store/daily-stats.js';
import Options, { observe } from '/store/options.js';

import { shouldShowOperaSerpAlert } from '/notifications/opera-serp.js';
import AutoSyncingMap from '/utils/map.js';

import Request from './utils/request.js';
import * as trackerDb from './utils/trackerdb.js';
import AutoSyncingMap from './utils/map.js';

export const tabStats = new AutoSyncingMap({ storageKey: 'tabStats:v1' });

Expand Down
6 changes: 3 additions & 3 deletions extension-manifest-v3/src/store/tab-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import { store } from 'hybrids';

import AutoSyncingMap from '/utils/map.js';

const Tracker = {
id: true,
name: '',
Expand Down Expand Up @@ -66,9 +68,7 @@ const Stats = {
const tab = await chrome.runtime.sendMessage({
action: 'getCurrentTab',
});

const storage = await chrome.storage.session.get(['tabStats:v1']);
const stats = tab && storage['tabStats:v1']?.entries[tab.id];
const stats = tab && (await AutoSyncingMap.get('tabStats:v1', tab.id));

return stats && tab.url.includes(stats.domain) ? stats : {};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
// with session storage from time to time. That is done to prevent
// the loss of all stats when the browser terminates the execution
// context (background script or service worker).

export const storage = chrome.storage.session || chrome.storage.local;

export default class AutoSyncingMap {
static async get(storageKey, key) {
const data = await storage.get([storageKey]);
return data[storageKey]?.entries[key];
}

constructor({
storageKey,
softFlushIntervalInMs = 200,
Expand Down Expand Up @@ -71,9 +79,11 @@ export default class AutoSyncingMap {
this._pending = new Promise((resolve, reject) => {
// Migration to session storage
// TODO: Remove this code after a few releases
chrome.storage.local.remove(this.storageKey);
if (chrome.storage.session) {
chrome.storage.local.remove(this.storageKey);
}

chrome.storage.session.get([this.storageKey], (result) => {
storage.get([this.storageKey], (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
Expand Down Expand Up @@ -141,7 +151,7 @@ export default class AutoSyncingMap {

this._scheduleAction(
new Promise((resolve, reject) => {
chrome.storage.session.remove(this.storageKey, () => {
storage.remove(this.storageKey, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
Expand Down Expand Up @@ -221,7 +231,7 @@ export default class AutoSyncingMap {
entries: Object.fromEntries(this.inMemoryMap),
ttl: Object.fromEntries(this._ttlMap),
};
chrome.storage.session.set({ [this.storageKey]: serialized }, () => {
storage.set({ [this.storageKey]: serialized }, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
Expand Down

0 comments on commit 40da99f

Please sign in to comment.