Skip to content

Commit

Permalink
[GV] Avoid to update the finder when the results aren't complete
Browse files Browse the repository at this point in the history
At the beginning of a search we can an update can be triggered with 0 over 0
found matches.
In the GeckoView context, we can't update the finder whenever we want but only
when it has been required.
  • Loading branch information
calixteman committed Jan 20, 2023
1 parent deb07ae commit dc94b75
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
31 changes: 22 additions & 9 deletions test/unit/pdf_find_controller_spec.js
Expand Up @@ -85,7 +85,8 @@ function testSearch({
selectedMatch,
pageMatches = null,
pageMatchesLength = null,
countUpdate = null,
updateFindMatchesCount = null,
updateFindControlState = null,
}) {
return new Promise(function (resolve) {
const eventState = Object.assign(
Expand Down Expand Up @@ -127,11 +128,20 @@ function testSearch({
return a + b;
});

if (updateFindControlState) {
eventBus.on(
"updatefindcontrolstate",
function onUpdateFindControlState(evt) {
updateFindControlState[0] += 1;
}
);
}

eventBus.on(
"updatefindmatchescount",
function onUpdateFindMatchesCount(evt) {
if (countUpdate) {
countUpdate[0] += 1;
if (updateFindMatchesCount) {
updateFindMatchesCount[0] += 1;
}
if (pdfFindController.pageMatches.length !== totalPages) {
return;
Expand Down Expand Up @@ -199,7 +209,7 @@ function testEmptySearch({ eventBus, pdfFindController, state }) {
describe("pdf_find_controller", function () {
it("performs a normal search", async function () {
const { eventBus, pdfFindController } = await initPdfFindController();
const countUpdate = [0];
const updateFindMatchesCount = [0];

await testSearch({
eventBus,
Expand All @@ -212,18 +222,19 @@ describe("pdf_find_controller", function () {
pageIndex: 0,
matchIndex: 0,
},
countUpdate,
updateFindMatchesCount,
});

expect(countUpdate[0]).toBe(9);
expect(updateFindMatchesCount[0]).toBe(9);
});

it("performs a normal search but the total counts is only updated one time", async function () {
const { eventBus, pdfFindController } = await initPdfFindController(
null,
false
);
const countUpdate = [0];
const updateFindMatchesCount = [0];
const updateFindControlState = [0];

await testSearch({
eventBus,
Expand All @@ -236,10 +247,12 @@ describe("pdf_find_controller", function () {
pageIndex: 0,
matchIndex: 0,
},
countUpdate,
updateFindMatchesCount,
updateFindControlState,
});

expect(countUpdate[0]).toBe(1);
expect(updateFindMatchesCount[0]).toBe(1);
expect(updateFindControlState[0]).toBe(0);
});

it("performs a normal search and finds the previous result", async function () {
Expand Down
10 changes: 10 additions & 0 deletions web/pdf_find_controller.js
Expand Up @@ -1052,6 +1052,16 @@ class PDFFindController {
}

#updateUIState(state, previous = false) {
if (
!this.#updateMatchesCountOnProgress &&
(this.#visitedPagesCount !== this._linkService.pagesCount ||
state === FindState.PENDING)
) {
// When this.#updateMatchesCountOnProgress is false we only send an update
// when everything is ready.
return;
}

this._eventBus.dispatch("updatefindcontrolstate", {
source: this,
state,
Expand Down

0 comments on commit dc94b75

Please sign in to comment.