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) { if(settings.enabled) {
await this.suggestions.storeBlocks(protyle, settings.language) 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,19 +66,29 @@ export class SuggestionEngine {
}) })
} }
public async forAllBlocksSuggest(suggest: boolean = false, render: boolean = true) { public async forAllBlocksSuggest(suggest: boolean = false, render: boolean = true, concurrencyLimit: number = 1000) {
const blockPromises = Object.keys(this.blockStorage).map(async (blockID) => {
if(!(blockID in this.blockStorage)) { const blockIDs = Object.keys(this.blockStorage);
return for (let i = 0; i < blockIDs.length; i += concurrencyLimit) {
}
if(suggest && this.blockStorage[blockID].suggestions == null) { const batch = blockIDs.slice(i, i + concurrencyLimit);
await this.suggestForBlock(blockID) const blockPromises = batch.map(async (blockID) => {
} if(!(blockID in this.blockStorage)) {
if(render) { return
await this.renderSuggestions(blockID) }
} if(suggest === true && this.blockStorage[blockID].suggestions == null) {
}); await this.suggestForBlock(blockID)
await Promise.all(blockPromises); }
if(render) {
await this.renderSuggestions(blockID)
}
});
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) { public async suggestAndRender(blockID: string) {
@ -148,14 +158,13 @@ export class SuggestionEngine {
private shouldSuggest(blockID: string, block: StoredBlock, suggestion: Suggestion): boolean { private shouldSuggest(blockID: string, block: StoredBlock, suggestion: Suggestion): boolean {
const element = block.protyle.fastGetBlockElement(blockID) 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) { return !SuggestionEngine.blacklisted.some(blacklisted =>
if(eai instanceof Element && eai.matches(blacklisted)) { (eaiStart instanceof Element && eaiStart.matches(blacklisted)) ||
return false (eaiEnd instanceof Element && eaiEnd.matches(blacklisted))
} );
}
return true
} }
@ -188,7 +197,7 @@ export class SuggestionEngine {
const offset = this.getAbsoluteOffsetInBlock(range, blockID) const offset = this.getAbsoluteOffsetInBlock(range, blockID)
let suggNo = -1 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) { if(offset >= suggestion.offset && offset <= suggestion.offset + suggestion.length) {
suggNo = i suggNo = i
} }