Skip to content

Commit

Permalink
fix getAll and use Map for db cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jan 10, 2025
1 parent 49ceea8 commit d149a3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
22 changes: 11 additions & 11 deletions src/background/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {STORAGE_KEY} from '@/js/prefs';
import {chromeLocal} from '@/js/storage-util';
import {CHROME} from '@/js/ua';
import {deepMerge} from '@/js/util';
import {bgBusy} from './common';
import ChromeStorageDB from './db-chrome-storage';
import offscreen, {offscreenCache} from './offscreen';
import {offloadCache} from './style-manager/util';
Expand Down Expand Up @@ -33,6 +32,9 @@ 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 Down Expand Up @@ -81,22 +83,20 @@ Object.assign(API, /** @namespace API */ {

async function cachedExec(dbName, cmd, a, b) {
const old = dataCache[dbName];
const hub = old || (dataCache[dbName] = {__proto__: null});
const res = cmd === 'get' && a in hub
? hub[a]
const hub = old || (dataCache[dbName] = new Map());
const res = cmd === 'get' && hub.has(a)
? hub.get(a)
: old && cmd === 'getAll'
? Object.values(old)
? getAll(a, hub)
: await exec(...arguments);
switch (cmd) {
case 'put':
cmd = DATA_KEY[dbName];
hub[cmd ? a[cmd] : b] = deepMerge(a);
hub.set(cmd ? a[cmd] : b, deepMerge(a));
break;
case 'delete':
delete hub[a];
break;
case 'clear':
delete dataCache[dbName];
hub[cmd](a);
break;
}
return res && typeof res === 'object' ? deepMerge(res) : res;
Expand All @@ -109,12 +109,12 @@ async function cachedExecOffscreen(dbName, cmd, a) {
&& offscreenCache
&& await offscreenCache
&& (res = offscreenCache[dbName])) {
res = cmd === 'get' ? res.get(a) : [...res.values()];
res = cmd === 'get' ? res.get(a) : getAll(a, res);
} else {
if ((a = offscreen[CLIENT])) {
if (!cachedClient.has(a)) {
cachedClient.add(a);
if (!bgBusy) offloadCache(dataCache[STATE_DB] || {});
if (!offscreenCache) setTimeout(offloadCache, 100, dataCache);
} else if (!isRead) {
offscreen.dbCache(...arguments);
}
Expand Down
15 changes: 6 additions & 9 deletions src/background/style-manager/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CACHE_DB, DB, STATE_DB, UCD} from '@/js/consts';
import {CACHE_DB, DB, UCD} from '@/js/consts';
import * as URLS from '@/js/urls';
import {deepEqual, isEmptyObj, mapObj} from '@/js/util';
import {broadcast} from '../broadcast';
Expand Down Expand Up @@ -70,20 +70,17 @@ export function *iterStyles() {
for (const v of dataMap.values()) yield v.style;
}

export async function offloadCache(stateData) {
export async function offloadCache(dbCache) {
if (bgBusy) await bgBusy;
const styleMap = new Map();
const cacheMap = new Map();
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...');
await offscreen.dbCache({
[DB]: styleMap,
[CACHE_DB]: cacheMap,
[STATE_DB]: new Map(Object.entries(stateData)),
});
await offscreen.dbCache(res);
}

export async function setOrderImpl(data, {
Expand Down

0 comments on commit d149a3b

Please sign in to comment.