mirror of
https://github.com/SukkaW/Surge.git
synced 2026-09-12 18:44:36 +08:00
Perf: offload reject fetch & parse into a worker thread
This commit is contained in:
@@ -189,19 +189,32 @@ export class FileOutput {
|
||||
return this;
|
||||
}
|
||||
|
||||
private addDomainsetLine(line: string) {
|
||||
const otherPoundSign = line.lastIndexOf('#');
|
||||
|
||||
if (otherPoundSign > 0) {
|
||||
line = line.slice(0, otherPoundSign).trimEnd();
|
||||
}
|
||||
|
||||
if (line[0] === '.') {
|
||||
this.addDomainSuffix(line);
|
||||
} else {
|
||||
this.domainTrie.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
private async addFromDomainsetPromise(source: MaybePromise<AsyncIterable<string> | Iterable<string> | string[]>) {
|
||||
for await (let line of await source) {
|
||||
const otherPoundSign = line.lastIndexOf('#');
|
||||
|
||||
if (otherPoundSign > 0) {
|
||||
line = line.slice(0, otherPoundSign).trimEnd();
|
||||
}
|
||||
|
||||
if (line[0] === '.') {
|
||||
this.addDomainSuffix(line);
|
||||
} else {
|
||||
this.domainTrie.add(line);
|
||||
const resolved = await source;
|
||||
// `for await` over a plain array still yields to the microtask queue once
|
||||
// per element; the parsed remote lists are hundreds of thousands of lines
|
||||
if (Array.isArray(resolved)) {
|
||||
for (let i = 0, len = resolved.length; i < len; i++) {
|
||||
this.addDomainsetLine(resolved[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for await (const line of resolved) {
|
||||
this.addDomainsetLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user