Merge pull request #178 from poweroftrue/master

Improve chord loading speed
This commit is contained in:
2025-03-31 13:13:17 +02:00
committed by GitHub

View File

@@ -131,6 +131,7 @@
codes: Map<number, KeyInfo>, codes: Map<number, KeyInfo>,
): Promise<FlexSearch.Index> { ): Promise<FlexSearch.Index> {
if (chords.length === 0 || !browser) return index; if (chords.length === 0 || !browser) return index;
index = new FlexSearch.Index({ index = new FlexSearch.Index({
tokenize: "full", tokenize: "full",
encode(phrase: string) { encode(phrase: string) {
@@ -148,20 +149,36 @@
}); });
}, },
}); });
let abort = false; let abort = false;
abortIndexing = () => { abortIndexing = () => {
abort = true; abort = true;
}; };
for (let i = 0; i < chords.length; i++) {
const batchSize = 200;
const batches = Math.ceil(chords.length / batchSize);
for (let b = 0; b < batches; b++) {
if (abort) return index; if (abort) return index;
const chord = chords[i]!; const start = b * batchSize;
progress = i + 1; const end = Math.min((b + 1) * batchSize, chords.length);
const batch = chords.slice(start, end);
const promises = batch.map((chord, i) => {
const chordIndex = start + i;
progress = chordIndex + 1;
if ("phrase" in chord) { if ("phrase" in chord) {
await index.addAsync(i, encodeChord(chord, osLayout, codes)); const encodedChord = encodeChord(chord, osLayout, codes);
return index.addAsync(chordIndex, encodedChord);
} }
return Promise.resolve();
});
await Promise.all(promises);
} }
return index; return index;
} }