From 236871c68b1330be9a62ae1534f7a53d9efe2f0e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 1 Oct 2018 17:41:57 +0200 Subject: [PATCH] [Regression] Restore the ability to start searching before a document has loaded, and ignore searches for previously opened documents (PR 10099 follow-up) For many years it's been possible to enter a search term into the findbar(s) before the document has finised loading, such that searching starts immediately once it has loaded. PR 10099 accidentally broke that, which I unfortunately missed during reviewing. Since searching is asynchronous you cannot directly check in `executeCommand` if the document is loaded/current, but need to wait until searching is actually enabled first. Furthermore this patch also ensures that the `_findTimeout` is always correctly cleared given that it adds further asynchronous behaviour to searching, since you obviously only want to deal with searches relevant to the current document. --- web/pdf_find_controller.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index cbd50dbb4..c103da0ba 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -110,9 +110,7 @@ class PDFFindController { } executeCommand(cmd, state) { - if (!this._pdfDocument) { - return; - } + const pdfDocument = this._pdfDocument; if (this._state === null || cmd !== 'findagain') { this._dirtyMatch = true; @@ -121,14 +119,25 @@ class PDFFindController { this._updateUIState(FindState.PENDING); this._firstPagePromise.then(() => { + if (!this._pdfDocument || + (pdfDocument && this._pdfDocument !== pdfDocument)) { + // If the document was closed before searching began, or if the search + // operation was relevant for a previously opened document, do nothing. + return; + } this._extractText(); - clearTimeout(this._findTimeout); + if (this._findTimeout) { + clearTimeout(this._findTimeout); + this._findTimeout = null; + } if (cmd === 'find') { // Trigger the find action with a small delay to avoid starting the // search when the user is still typing (saving resources). - this._findTimeout = - setTimeout(this._nextMatch.bind(this), FIND_TIMEOUT); + this._findTimeout = setTimeout(() => { + this._nextMatch(); + this._findTimeout = null; + }, FIND_TIMEOUT); } else { this._nextMatch(); } @@ -156,14 +165,19 @@ class PDFFindController { this._pendingFindMatches = Object.create(null); this._resumePageIdx = null; this._dirtyMatch = false; + clearTimeout(this._findTimeout); this._findTimeout = null; this._firstPagePromise = new Promise((resolve) => { - const eventBus = this._eventBus; - eventBus.on('pagesinit', function onPagesInit() { - eventBus.off('pagesinit', onPagesInit); + const onPagesInit = () => { + if (!this._pdfDocument) { + throw new Error( + 'PDFFindController: `setDocument()` should have been called.'); + } + this._eventBus.off('pagesinit', onPagesInit); resolve(); - }); + }; + this._eventBus.on('pagesinit', onPagesInit); }); }