Skip to content

Commit

Permalink
call fixKnownProblems as a fallback at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Jan 5, 2025
1 parent 27e268f commit 5757083
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
15 changes: 10 additions & 5 deletions src/background/style-manager/fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ const MISSING_PROPS = {

const hasVarsAndImport = ({code}) => code.startsWith(':root {\n --') && /@import\s/i.test(code);

export function fixKnownProblems(style, initIndex, initArray) {
/**
* @param {StyleObj} style
* @param {boolean} [revive]
* @return {?StyleObj|Promise<StyleObj>}
*/
export function fixKnownProblems(style, revive) {
let res = 0;
let v;
if (!style || typeof style !== 'object')
style = {};
if (!style.id && initArray) {
return style;
if (!+style.id && revive) {
res = new Set();
for (const x of initArray) if (x) res.add(x.id);
for (const x of revive) if (x) res.add(x.id);
for (v = 1, res.has(v); ;) v++;
}
for (const key in MISSING_PROPS) {
Expand Down Expand Up @@ -76,7 +81,7 @@ export function fixKnownProblems(style, initIndex, initArray) {
if (!style.url) res = style.url = v;
if (!style.installationUrl) res = style.installationUrl = v;
}
if (initArray && (
if (revive && (
!Array.isArray(v = style.sections) && (v = 0, true) ||
/* @import must precede `vars` that we add at beginning */
!isEmptyObj(style[UCD]?.vars) && v.some(hasVarsAndImport)
Expand Down
39 changes: 28 additions & 11 deletions src/background/style-manager/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IMPORT_THROTTLE, kUrl, UCD} from '@/js/consts';
import * as prefs from '@/js/prefs';
import {calcStyleDigest, styleCodeEmpty} from '@/js/sections-util';
import {isEmptyObj, mapObj, sleep} from '@/js/util';
import {isEmptyObj, mapObj} from '@/js/util';
import {broadcast} from '../broadcast';
import * as colorScheme from '../color-scheme';
import {bgBusy, bgInit, uuidIndex} from '../common';
Expand All @@ -28,25 +28,20 @@ bgInit.push(async () => {
db.getAll(),
styleCache.loadAll(),
]);
(async () => {
await bgBusy;
await sleep();
__.DEBUGLOG('styleMan fixKnownProblems...');
const fixed = (await Promise.all(styles.map(fixKnownProblems))).filter(Boolean);
setTimeout(db.putMany, 0, fixed);
})();
setOrderImpl(orderFromDb, {store: false});
for (const style of styles)
try { storeInMap(style); } catch {}
initStyleMap(styles);
styleCache.hydrate(dataMap);
__.DEBUGLOG('styleMan init done');
});

bgBusy.then(() => {
colorScheme.onChange(() => {
for (const {style} of dataMap.values()) {
if (colorScheme.SCHEMES.includes(style.preferScheme)) {
broadcastStyleUpdated(style, 'colorScheme');
}
}
}, !__.MV3);
__.DEBUGLOG('styleMan init done');
});

styleCache.setOnDeleted(val => {
Expand All @@ -58,6 +53,28 @@ styleCache.setOnDeleted(val => {
export * from '../style-search-db';
export {getById as get};

function initStyleMap(styles) {
let fixed, lost;
for (let style of styles) {
try {
if (+style.id > 0 && style._id.trim() && typeof style.sections[0].code === 'string') {
storeInMap(style);
continue;
}
} catch {}
style = fixKnownProblems(style, true);
if (style) (fixed ??= {})[style.id] = style;
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, 0, fixed));
}
if (lost)
console.error(`Skipped ${lost.length} invalid entries:`, lost);
}

/** @returns {Promise<void>} */
export async function config(id, prop, value) {
const style = Object.assign({}, getById(id));
Expand Down

0 comments on commit 5757083

Please sign in to comment.