Optimization
All checks were successful
Build on Push and create Release on Tag / build (push) Successful in 3m58s

This commit is contained in:
MassiveBox 2025-12-04 00:05:51 +01:00
parent b7c81d1fa0
commit 68025b9a34
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
2 changed files with 32 additions and 22 deletions

View file

@ -114,7 +114,8 @@ export default class SpellCheckPlugin extends Plugin {
if(settings.enabled) {
await this.suggestions.storeBlocks(protyle, settings.language)
void this.suggestions.forAllBlocksSuggest(true, true)
const useOnline = this.settingsUtil.get('online');
void this.suggestions.forAllBlocksSuggest(true, true, useOnline ? undefined : 10);
}
}

View file

@ -66,12 +66,17 @@ export class SuggestionEngine {
})
}
public async forAllBlocksSuggest(suggest: boolean = false, render: boolean = true) {
const blockPromises = Object.keys(this.blockStorage).map(async (blockID) => {
public async forAllBlocksSuggest(suggest: boolean = false, render: boolean = true, concurrencyLimit: number = 1000) {
const blockIDs = Object.keys(this.blockStorage);
for (let i = 0; i < blockIDs.length; i += concurrencyLimit) {
const batch = blockIDs.slice(i, i + concurrencyLimit);
const blockPromises = batch.map(async (blockID) => {
if(!(blockID in this.blockStorage)) {
return
}
if(suggest && this.blockStorage[blockID].suggestions == null) {
if(suggest === true && this.blockStorage[blockID].suggestions == null) {
await this.suggestForBlock(blockID)
}
if(render) {
@ -79,6 +84,11 @@ export class SuggestionEngine {
}
});
await Promise.all(blockPromises);
// yield to the event loop to prevent UI freezing
await new Promise(resolve => setTimeout(resolve, 1));
}
}
public async suggestAndRender(blockID: string) {
@ -148,14 +158,13 @@ export class SuggestionEngine {
private shouldSuggest(blockID: string, block: StoredBlock, suggestion: Suggestion): boolean {
const element = block.protyle.fastGetBlockElement(blockID)
const eai = ProtyleHelper.getElementAtTextIndex(element, suggestion.offset + suggestion.length)
const eaiStart = ProtyleHelper.getElementAtTextIndex(element, suggestion.offset)
const eaiEnd = ProtyleHelper.getElementAtTextIndex(element, suggestion.offset + suggestion.length)
for(let blacklisted of SuggestionEngine.blacklisted) {
if(eai instanceof Element && eai.matches(blacklisted)) {
return false
}
}
return true
return !SuggestionEngine.blacklisted.some(blacklisted =>
(eaiStart instanceof Element && eaiStart.matches(blacklisted)) ||
(eaiEnd instanceof Element && eaiEnd.matches(blacklisted))
);
}
@ -188,7 +197,7 @@ export class SuggestionEngine {
const offset = this.getAbsoluteOffsetInBlock(range, blockID)
let suggNo = -1
this.blockStorage[blockID].suggestions.forEach((suggestion, i) => {
this.blockStorage[blockID].suggestions?.forEach((suggestion, i) => {
if(offset >= suggestion.offset && offset <= suggestion.offset + suggestion.length) {
suggNo = i
}