Skip to content

Commit

Permalink
Add a _updateAllPages helper method to PDFFindController in order…
Browse files Browse the repository at this point in the history
… to reduce the amount of event dispatching

Given that dispatching the 'updatetextlayermatches' event with `pageIndex = -1` set is now used to target the textLayers of *all* pages, there's no need to send individual events to every single page during `_nextMatch`. Since there can be an arbitrary number of pages in a document, this small/simple optimization seems too easy to ignore.
  • Loading branch information
Snuffleupagus committed Oct 26, 2018
1 parent dc98bf7 commit 27b21f2
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ class PDFFindController {
});
}

_updateAllPages() {
this._eventBus.dispatch('updatetextlayermatches', {
source: this,
pageIndex: -1,
});
}

_nextMatch() {
const previous = this._state.findPrevious;
const currentPageIndex = this._linkService.page - 1;
Expand All @@ -407,18 +414,18 @@ class PDFFindController {
this._pageMatchesLength = null;
this._matchesCountTotal = 0;

for (let i = 0; i < numPages; i++) {
// Wipe out any previously highlighted matches.
this._updatePage(i);
this._updateAllPages(); // Wipe out any previously highlighted matches.

for (let i = 0; i < numPages; i++) {
// Start finding the matches as soon as the text is extracted.
if (!(i in this._pendingFindMatches)) {
this._pendingFindMatches[i] = true;
this._extractTextPromises[i].then((pageIdx) => {
delete this._pendingFindMatches[pageIdx];
this._calculateMatch(pageIdx);
});
if (this._pendingFindMatches[i] === true) {
continue;
}
this._pendingFindMatches[i] = true;
this._extractTextPromises[i].then((pageIdx) => {
delete this._pendingFindMatches[pageIdx];
this._calculateMatch(pageIdx);
});
}
}

Expand Down Expand Up @@ -557,11 +564,7 @@ class PDFFindController {
this._updateUIState(FindState.FOUND);
}
this._highlightMatches = false;

this._eventBus.dispatch('updatetextlayermatches', {
source: this,
pageIndex: -1,
});
this._updateAllPages(); // Wipe out any previously highlighted matches.
});
}

Expand Down

0 comments on commit 27b21f2

Please sign in to comment.