From 68025b9a34c06376e1915fbff2c7cf45a1814da6 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Thu, 4 Dec 2025 00:05:51 +0100 Subject: [PATCH] Optimization --- src/index.ts | 3 ++- src/suggestions.ts | 51 +++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8405f65..3abd45c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); } } diff --git a/src/suggestions.ts b/src/suggestions.ts index d43340a..a32d142 100644 --- a/src/suggestions.ts +++ b/src/suggestions.ts @@ -66,19 +66,29 @@ export class SuggestionEngine { }) } - public async forAllBlocksSuggest(suggest: boolean = false, render: boolean = true) { - const blockPromises = Object.keys(this.blockStorage).map(async (blockID) => { - if(!(blockID in this.blockStorage)) { - return - } - if(suggest && this.blockStorage[blockID].suggestions == null) { - await this.suggestForBlock(blockID) - } - if(render) { - await this.renderSuggestions(blockID) - } - }); - await Promise.all(blockPromises); + 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 === true && this.blockStorage[blockID].suggestions == null) { + await this.suggestForBlock(blockID) + } + 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) { @@ -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 }