Skip to content

Commit

Permalink
remove offscreen cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jan 10, 2025
1 parent 8bf13db commit cdfe413
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 143 deletions.
11 changes: 4 additions & 7 deletions src/background/color-scheme.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {kDark, kStyleViaXhr, STATE_DB} from '@/js/consts';
import {kDark, kStyleViaXhr} from '@/js/consts';
import {CLIENT} from '@/js/port';
import * as prefs from '@/js/prefs';
import {debounce, isCssDarkScheme} from '@/js/util';
import {broadcastExtension} from './broadcast';
import {bgBusy, bgInit, bgPreInit} from './common';
import {bgBusy, bgPreInit} from './common';
import {stateDB} from './db';
import offscreen, {offscreenCache} from './offscreen';
import offscreen from './offscreen';

const changeListeners = new Set();
const kSTATE = 'schemeSwitcher.enabled';
Expand Down Expand Up @@ -35,10 +35,7 @@ let prefState;
chrome.alarms.onAlarm.addListener(onAlarm);

if (__.MV3) {
bgPreInit.push(offscreenCache.then(v => {
if (v && (v = v[STATE_DB])) setSystemDark(v.get(kDark));
else bgInit.push(refreshSystemDark);
}));
bgPreInit.push(stateDB.get(kDark).then(setSystemDark));
prefs.subscribe([kSTATE, kStyleViaXhr], (key, val, init) => {
if (init && key !== kStyleViaXhr) // only process the last one on init
return;
Expand Down
83 changes: 11 additions & 72 deletions src/background/db.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {CACHE_DB, DB, STATE_DB} from '@/js/consts';
import {API} from '@/js/msg-api';
import {CLIENT} from '@/js/port';
import {STORAGE_KEY, subscribe} from '@/js/prefs';
import {STORAGE_KEY} from '@/js/prefs';
import {chromeLocal} from '@/js/storage-util';
import {CHROME} from '@/js/ua';
import {deepMerge} from '@/js/util';
import ChromeStorageDB from './db-chrome-storage';
import offscreen, {offscreenCache} from './offscreen';
import {offloadCache} from './style-manager/util';

/*
Initialize a database. There are some problems using IndexedDB in Firefox:
Expand All @@ -19,15 +16,13 @@ import {offloadCache} from './style-manager/util';
let exec = __.BUILD === 'chrome' || CHROME
? dbExecIndexedDB
: tryUsingIndexedDB;
const cachedClient = new WeakSet();
const FALLBACK = 'dbInChromeStorage';
const REASON = FALLBACK + 'Reason';
const DRAFTS_DB = 'drafts';
const CACHING = {
[DRAFTS_DB]: cachedExec,
[STORAGE_KEY]: __.MV3 ? cachedExecOffscreen : cachedExec,
[STORAGE_KEY]: cachedExec,
};
const CACHING2 = {};
const DATA_KEY = {};
const STORES = {};
const VERSIONS = {};
Expand All @@ -37,9 +32,6 @@ const databases = {};
const proxyHandler = {
get: ({dbName}, cmd) => (CACHING[dbName] || exec).bind(null, dbName, cmd),
};
const getAll = (range, map) => range instanceof IDBKeyRange
? [...map.keys()].filter(range.includes, range).map(map.get, map)
: [...map.values()];
/**
* @param {string} dbName
* @param {object} [cfg]
Expand All @@ -66,75 +58,22 @@ export const draftsDb = getDbProxy(DRAFTS_DB);
export const prefsDb = getDbProxy(STORAGE_KEY);
export const stateDB = __.MV3 && getDbProxy(STATE_DB, {store: 'kv'});

if (__.MV3) {
const toggleOffscreenCache = val => {
CACHING[CACHE_DB] = CACHING[DB] = CACHING[STATE_DB] = val
? cachedExecOffscreen
: cachedExec;
if (!val && offscreen[CLIENT])
offscreen.dbCache(null);
};
toggleOffscreenCache(true);
CACHING2[STORAGE_KEY] = CACHING2[STATE_DB] = cachedExec;
subscribe('keepAliveCache', (key, val) => toggleOffscreenCache(val), true);
}

Object.assign(API, /** @namespace API */ {
draftsDb,
prefsDb,
});

async function cachedExec(dbName, cmd, a, b) {
const old = dataCache[dbName];
const hub = old || (dataCache[dbName] = new Map());
const res = cmd === 'get' && hub.has(a)
? hub.get(a)
: old && (
cmd === 'getAll' ? getAll(a, hub)
: cmd === 'getMany' && a.map(hub.get, hub)
) || await exec(...arguments);
switch (cmd) {
case 'put':
cmd = DATA_KEY[dbName];
hub.set(cmd ? a[cmd] : b, deepMerge(a));
break;
case 'putMany':
cmd = DATA_KEY[dbName];
for (b of a) hub.set(b[cmd], deepMerge(b));
break;
case 'deleteMany':
a.forEach(hub.delete, hub);
break;
case 'delete':
case 'clear':
hub[cmd](a);
break;
}
return res && typeof res === 'object' ? deepMerge(res) : res;
}

async function cachedExecOffscreen(dbName, cmd, a, b) {
let res, client, isRead;
if ((isRead = cmd.startsWith('get'))
&& offscreenCache
&& await offscreenCache
&& (res = offscreenCache[dbName])) {
return cmd === 'get' ? res.get(a)
: cmd === 'getMany' ? a.map(res.get, res)
: getAll(a, res);
}
if ((client = offscreen[CLIENT]) && !cachedClient.has(client)) {
cachedClient.add(client);
if (!offscreenCache) {
offloadCache(dataCache);
client = null;
}
const hub = dataCache[dbName] ??= {};
const res = cmd === 'get' && a in hub ? hub[a] : await exec(...arguments);
if (cmd === 'get') {
hub[a] = deepMerge(res);
} else if (cmd === 'put') {
const key = DATA_KEY[dbName];
hub[key ? a[key] : b] = deepMerge(a);
} else if (cmd === 'delete') {
delete hub[a];
}
if (client && !isRead)
offscreen.dbCache(dbName, cmd, a, b, DATA_KEY[dbName]);
res = await (CACHING2[dbName] || exec)(dbName, cmd, a, b);
if (client && isRead)
offscreen.dbCache(dbName, 'res:' + cmd, res, a, DATA_KEY[dbName]);
return res;
}

Expand Down
3 changes: 2 additions & 1 deletion src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {setSystemDark} from './color-scheme';
import {bgBusy, bgInit, bgPreInit, dataHub} from './common';
import reinjectContentScripts from './content-scripts';
import initContextMenus from './context-menus';
import {draftsDb, prefsDb, stateDB} from './db';
import {cacheDB, draftsDb, prefsDb, stateDB} from './db';
import download from './download';
import {refreshIconsWhenReady, updateIconBadge} from './icon-manager';
import prefsApi from './prefs-api';
Expand Down Expand Up @@ -85,6 +85,7 @@ chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => {
}
if (__.MV3) {
(bgPreInit.length ? bgPreInit : []).push(
cacheDB.clear(),
stateDB.clear(),
DNR.getDynamicRules().then(rules => updateDynamicRules(undefined, getRuleIds(rules))),
DNR.getSessionRules().then(rules => updateSessionRules(undefined, getRuleIds(rules))),
Expand Down
9 changes: 1 addition & 8 deletions src/background/offscreen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {CLIENT, createPortProxy} from '@/js/port';
import {createPortProxy} from '@/js/port';
import {ownRoot} from '@/js/urls';
import {bgBusy} from './common';
import {getWindowClients} from './util';

const FILENAME = 'offscreen.html';
Expand All @@ -14,12 +13,6 @@ const offscreen = createPortProxy(() => (
});
export default offscreen;

export let offscreenCache = __.MV3 && (async () => {
bgBusy.then(() => (offscreenCache = null));
offscreenCache = (offscreen[CLIENT] = await findOffscreenClient()) &&
await offscreen.getData();
return offscreenCache;
})();
let creating;

async function findOffscreenClient() {
Expand Down
19 changes: 7 additions & 12 deletions src/background/style-manager/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ function del(items) {
cacheDB.deleteMany(items);
}

export function getCacheSkeletons(arr = cache.values(), toDel = []) {
/** @return {void} */
function flush() {
const bare = [];
let toDel;
nextEntry:
for (const val of arr) {
for (const val of toWrite) {
const {d, url, maybeMatch, sections} = val;
/** @type {MatchCache.IndexMap} */
const indexes = {};
Expand All @@ -146,7 +148,7 @@ export function getCacheSkeletons(arr = cache.values(), toDel = []) {
const sec = sections[styleId];
const idx = sec && (Array.isArray(sec) ? sec : sec.idx);
if (!idx) {
toDel.push(val);
(toDel ??= []).push(val);
continue nextEntry;
}
indexes[styleId] = idx;
Expand All @@ -158,15 +160,8 @@ export function getCacheSkeletons(arr = cache.values(), toDel = []) {
res.url = url;
bare.push(res);
}
return bare;
}

/** @return {void} */
function flush() {
const toDel = [];
const res = getCacheSkeletons(toWrite, toDel);
if (toDel[0]) del(toDel);
cacheDB.putMany(res);
if (toDel) del(toDel);
cacheDB.putMany(bare);
toWrite.clear();
timer = null;
}
Expand Down
16 changes: 1 addition & 15 deletions src/background/style-manager/util.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {CACHE_DB, DB, UCD} from '@/js/consts';
import {UCD} from '@/js/consts';
import * as URLS from '@/js/urls';
import {deepEqual, isEmptyObj, mapObj} from '@/js/util';
import {broadcast} from '../broadcast';
import broadcastInjectorConfig from '../broadcast-injector-config';
import {uuidIndex} from '../common';
import {prefsDb} from '../db';
import offscreen from '../offscreen';
import * as syncMan from '../sync-manager';
import {getCacheSkeletons} from './cache';
import {buildCacheForStyle} from './cache-builder';

/** @type {StyleDataMap} */
Expand Down Expand Up @@ -70,18 +68,6 @@ export function *iterStyles() {
for (const v of dataMap.values()) yield v.style;
}

export function offloadCache(dbCache) {
const res = {...dbCache};
const styleMap = res[DB] = new Map();
const cacheMap = res[CACHE_DB] = new Map();
for (const {style} of dataMap.values())
styleMap.set(style.id, style);
for (const v of getCacheSkeletons())
cacheMap.set(v.url, v);
__.DEBUGLOG('Offloading cache...');
return offscreen.dbCache(res);
}

export async function setOrderImpl(data, {
broadcast: broadcastAllowed,
calc = true,
Expand Down
3 changes: 1 addition & 2 deletions src/background/style-via-webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as colorScheme from './color-scheme';
import {bgBusy, bgPreInit, dataHub, onUnload} from './common';
import {stateDB} from './db';
import {webNavigation} from './navigation-manager';
import offscreen, {offscreenCache} from './offscreen';
import offscreen from './offscreen';
import makePopupData from './popup-data';
import {getSectionsByUrl} from './style-manager';
import * as styleCache from './style-manager/cache';
Expand Down Expand Up @@ -47,7 +47,6 @@ let curXHR = false;
if (__.MV3) {
toggle(); // register listeners synchronously so they wake up the SW next time it dies
bgPreInit.push((async () => {
await offscreenCache;
ruleIds = await stateDB.get(kRuleIds) || {};
for (const id in ruleIds) ruleIdKeys[ruleIds[id]] = +id;
})());
Expand Down
6 changes: 4 additions & 2 deletions src/background/tab-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ bgInit.push(async () => {
cache.set(id, data);
}
}
if (__.MV3)
stateDB.deleteMany([...dbMap.keys()].filter(k => !cache.has(k)));
if (__.MV3) {
const toDel = [...dbMap.keys()].filter(k => !cache.has(k));
if (toDel.length) stateDB.deleteMany(toDel);
}
});

bgBusy.then(() => {
Expand Down
24 changes: 0 additions & 24 deletions src/offscreen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,13 @@ import {API} from '@/js/msg-api';
import {COMMANDS} from '@/js/port';
import {fetchWebDAV, isCssDarkScheme, mapObj} from '@/js/util';

let dbCache;
let webdavInstance;

/** @namespace OffscreenAPI */
Object.assign(COMMANDS, {
isDark: isCssDarkScheme,
createObjectURL: URL.createObjectURL,
revokeObjectURL: URL.revokeObjectURL,
dbCache(dbName, cmd, a, b, dataKey) {
if (typeof dbName === 'object' /* includes `null` */) {
dbCache = dbName;
return;
}
const map = (dbCache ??= {})[dbName] ??= new Map();
if (cmd === 'put' || cmd === 'res:get')
map.set(b, a);
else if (cmd === 'delete' || cmd === 'clear')
map[cmd](a);
else if (cmd === 'deleteMany')
a.forEach(map.delete, map);
else if (cmd === 'putMany' || cmd === 'res:getAll')
for (b of a) map.set(b[dataKey], b);
else if (cmd === 'res:getMany')
for (let i = 0; i < a.length; i++)
map.set(b[i], a[i]);
else return cmd === 'get' ? map.get(a)
: cmd === 'getAll' ? [...map.values()]
: cmd === 'getMany' ? a.map(map.get, map)
: undefined;
},
getData: () => dbCache,
webdav: (cmd, ...args) => webdavInstance[cmd](...args),
webdavInit: async cfg => {
if (!webdavInstance) await loadScript(__.JS + 'webdav.js');
Expand Down

0 comments on commit cdfe413

Please sign in to comment.