Skip to content

Commit

Permalink
fix drafts/livePreview tracking in MV2
Browse files Browse the repository at this point in the history
+ extract styleMan::init
  • Loading branch information
tophf committed Jan 22, 2025
1 parent 549c0c6 commit 3c77156
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 104 deletions.
31 changes: 0 additions & 31 deletions src/background/style-manager/connector.js

This file was deleted.

79 changes: 6 additions & 73 deletions src/background/style-manager/index.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,29 @@
import {DB, IMPORT_THROTTLE, k_size, kInjectionOrder, kStyleViaXhr, kUrl, UCD} from '@/js/consts';
import {STORAGE_KEY} from '@/js/prefs';
import {IMPORT_THROTTLE, k_size, kStyleViaXhr, kUrl, UCD} from '@/js/consts';
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, execMirror, prefsDB} from '../db';
import {uuidIndex} from '../common';
import {db, draftsDB} from '../db';
import * as syncMan from '../sync-manager';
import tabCache from '../tab-manager';
import {getUrlOrigin} from '../tab-util';
import * as usercssMan from '../usercss-manager';
import * as uswApi from '../usw-api';
import * as styleCache from './cache';
import {buildCache} from './cache-builder';
import './connector';
import {fixKnownProblems, onBeforeSave, onSaved} from './fixer';
import './init';
import {onBeforeSave, onSaved} from './fixer';
import {urlMatchSection, urlMatchStyle} from './matcher';
import {
broadcastStyleUpdated, calcRemoteId, dataMap, getById, getByUuid,
mergeWithMapped, order, orderWrap, setOrderImpl, storeInMap,
mergeWithMapped, order, orderWrap, setOrderImpl,
} from './util';

bgInit.push(async () => {
__.DEBUGLOG('styleMan init...');
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, mirrored);
styleCache.hydrate(dataMap);
__.DEBUGLOG('styleMan init done');
});

onSchemeChange.add(() => {
for (const {style} of dataMap.values()) {
if (colorScheme.SCHEMES.includes(style.preferScheme)) {
broadcastStyleUpdated(style, 'colorScheme');
}
}
});

styleCache.setOnDeleted(val => {
for (const id in val.sections) {
dataMap.get(+id)?.appliesTo.delete(val.url);
}
});

export * from '../style-search-db';
export {getById as get};

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 {
try { fix = fixKnownProblems(style, true); } catch {}
if (fix) (fixed ??= new Map()).set(style.id, fix);
else (lost ??= []).push(style);
}
}
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>} */
export async function config(id, prop, value) {
const style = Object.assign({}, getById(id));
Expand Down
102 changes: 102 additions & 0 deletions src/background/style-manager/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {DB, kInjectionOrder, kResolve} from '@/js/consts';
import {onConnect, onDisconnect} from '@/js/msg';
import {STORAGE_KEY} from '@/js/prefs';
import * as colorScheme from '../color-scheme';
import {bgBusy, bgInit, onSchemeChange} from '../common';
import {db, draftsDB, execMirror, prefsDB} from '../db';
import * as styleCache from './cache';
import './init';
import {fixKnownProblems} from './fixer';
import {broadcastStyleUpdated, dataMap, setOrderImpl, storeInMap} from './util';

bgInit.push(async () => {
__.DEBUGLOG('styleMan init...');
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, mirrored);
styleCache.hydrate(dataMap);
__.DEBUGLOG('styleMan init done');
});

onSchemeChange.add(() => {
for (const {style} of dataMap.values()) {
if (colorScheme.SCHEMES.includes(style.preferScheme)) {
broadcastStyleUpdated(style, 'colorScheme');
}
}
});

styleCache.setOnDeleted(val => {
for (const id in val.sections) {
dataMap.get(+id)?.appliesTo.delete(val.url);
}
});

// Using ports to reliably track when the client is closed, however not for messaging,
// because our `API` is much faster due to direct invocation.
onDisconnect.draft = port => {
if (__.MV3) port[kResolve]();
const id = port.name.split(':')[1];
draftsDB.delete(+id || id).catch(() => {
});
};

onDisconnect.livePreview = port => {
if (__.MV3) port[kResolve]();
const id = +port.name.split(':')[1];
const data = dataMap.get(id);
if (!data) return;
data.preview = null;
broadcastStyleUpdated(data.style, 'editPreviewEnd');
};

if (__.MV3) {
onConnect.draft = onConnect.livePreview = port => {
__.KEEP_ALIVE(new Promise(resolve => {
port[kResolve] = resolve;
}));
};
}

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 {
try { fix = fixKnownProblems(style, true); } catch {}
if (fix) (fixed ??= new Map()).set(style.id, fix);
else (lost ??= []).push(style);
}
}
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);
}

0 comments on commit 3c77156

Please sign in to comment.