From 71842220b3ad29c3d2970d44329a1c5634118289 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Tue, 5 Nov 2024 13:37:08 +0000 Subject: [PATCH] Don't block page rendering while search is in progress https://forums.zotero.org/discussion/119108/zotero-search-is-slow-and-unusable-inside-of-pdf-documents --- src/pdf/pdf-find-controller.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/pdf/pdf-find-controller.js b/src/pdf/pdf-find-controller.js index a2beffcb..6e40b9ba 100644 --- a/src/pdf/pdf-find-controller.js +++ b/src/pdf/pdf-find-controller.js @@ -1002,16 +1002,22 @@ class PDFFindController { return; } + let resolvers = []; for (let i = 0, ii = this._linkService.pagesCount; i < ii; i++) { const { promise, resolve } = Promise.withResolvers(); this._extractTextPromises[i] = promise; + resolvers.push(resolve); + } - (async () => { + (async () => { + for (let i = 0; i < resolvers.length; i++) { + let resolve = resolvers[i]; let text = ''; let chars = []; try { + await new Promise(resolve => setTimeout(resolve)); let pageData = await this._pdfDocument.getPageData({ pageIndex: i }); function getTextFromChars(chars) { @@ -1027,7 +1033,8 @@ class PDFFindController { chars = pageData.chars; text = getTextFromChars(pageData.chars); - } catch (e) { + } + catch (e) { console.log(e); } @@ -1041,8 +1048,8 @@ class PDFFindController { ] = normalize(text); resolve(); - })(); - } + } + })(); } _nextMatch() {