Skip to content

Commit

Permalink
auto-restore mirrored styles
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jan 17, 2025
1 parent 4ce43a8 commit c337ba1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
20 changes: 18 additions & 2 deletions src/background/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const CACHING = {
[STORAGE_KEY]: cachedExec,
};
const {CompressionStream} = global;
const MIRROR_INIT = CompressionStream && {headers: {[kContentType]: 'application/gzip'}};
const kApplicationGzip = 'application/gzip';
const MIRROR_INIT = CompressionStream && {headers: {[kContentType]: kApplicationGzip}};
const MIRROR_PREFIX = 'http://_/';
/** @type {{[id: string]: Cache}} */
const MIRROR = {
Expand Down Expand Up @@ -210,11 +211,26 @@ function create(event) {
return idb;
}

async function execMirror(dbName, method, a, b) {
export async function execMirror(dbName, method, a, b) {
const mirror = MIRROR[dbName] ??= await caches.open(dbName);
switch (method) {
case 'delete':
return mirror.delete(MIRROR_PREFIX + a);
case 'get':
b = await execMirror(dbName, 'getAll', a);
return b[0];
case 'getAll':
a = await mirror.matchAll(a);
for (let i = 0; i < a.length; i++) {
b = a[i];
if (MIRROR_INIT && b.headers.get(kContentType) === kApplicationGzip)
b = new Response(b.body.pipeThrough(new DecompressionStream('gzip')));
a[i] = b.text();
}
a = await Promise.all(a);
for (let i = 0; i < a.length; i++)
a[i] = JSON.parse(a[i]);
return a;
case 'put':
await sleep(10);
if (dbName === DB && a[UCD])
Expand Down
47 changes: 32 additions & 15 deletions src/background/style-manager/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {IMPORT_THROTTLE, k_size, kStyleViaXhr, kUrl, UCD} from '@/js/consts';
import {DB, IMPORT_THROTTLE, k_size, kInjectionOrder, kStyleViaXhr, kUrl, UCD} from '@/js/consts';
import {STORAGE_KEY} from '@/js/prefs';
import * as prefs from '@/js/prefs';
import {calcStyleDigest, styleCodeEmpty} from '@/js/sections-util';
import {calcObjSize, mapObj} from '@/js/util';
import {broadcast} from '../broadcast';
import * as colorScheme from '../color-scheme';
import {bgBusy, bgInit, onSchemeChange, uuidIndex} from '../common';
import {db, draftsDB, prefsDB} from '../db';
import {db, draftsDB, execMirror, prefsDB} from '../db';
import * as syncMan from '../sync-manager';
import tabCache from '../tab-manager';
import {getUrlOrigin} from '../tab-util';
Expand All @@ -23,13 +24,18 @@ import {

bgInit.push(async () => {
__.DEBUGLOG('styleMan init...');
const [orderFromDb, styles = []] = await Promise.all([
prefsDB.get(orderWrap.id),
let mirrored;
let [orderFromDb, styles] = await Promise.all([
prefsDB.get(kInjectionOrder),
db.getAll(),
styleCache.loadAll(),
]);
if (!orderFromDb)
orderFromDb = await execMirror(STORAGE_KEY, 'get', kInjectionOrder);
if (!styles[0])
styles = mirrored = await execMirror(DB, 'getAll');
setOrderImpl(orderFromDb, {store: false});
initStyleMap(styles);
initStyleMap(styles, mirrored);
styleCache.hydrate(dataMap);
__.DEBUGLOG('styleMan init done');
});
Expand All @@ -51,27 +57,38 @@ styleCache.setOnDeleted(val => {
export * from '../style-search-db';
export {getById as get};

function initStyleMap(styles) {
let fixed, lost;
for (const style of styles) {
async function initStyleMap(styles, mirrored) {
let fix, fixed, lost, i, style, len;
for (i = 0, len = 0, style; i < styles.length; i++) {
style = styles[i];
if (+style.id > 0
&& typeof style._id === 'string'
&& typeof style.sections?.[0]?.code === 'string') {
storeInMap(style);
if (mirrored) {
if (i > len) styles[len] = style;
len++;
}
} else {
let fix;
try { fix = fixKnownProblems(style, true); } catch {}
if (fix) (fixed ??= {})[fix.id] = fix;
if (fix) (fixed ??= new Map()).set(style.id, fix);
else (lost ??= []).push(style);
}
}
if (fixed) {
console.warn(`Fixed ${fixed.length} styles, ids:`, ...Object.keys(fixed));
Promise.all([...bgBusy, Object.values(fixed)])
.then(() => setTimeout(db.putMany, 1000, fixed));
}
styles.length = len;
if (lost)
console.error(`Skipped ${lost.length} unrecoverable styles:`, lost);
if (fixed) {
console[mirrored ? 'log' : 'warn'](`Fixed ${fixed.size} styles, ids:`, ...fixed.keys());
fixed = await Promise.all([...fixed.values(), bgBusy]);
fixed.pop();
if (mirrored) {
styles.push(...fixed);
fixed.forEach(storeInMap);
}
}
if (styles.length)
setTimeout(db.putMany, 100, styles);
}

/** @returns {Promise<void>} */
Expand Down

0 comments on commit c337ba1

Please sign in to comment.