-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathinit.js
102 lines (95 loc) · 3 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
}