Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
HARP-10772: Finish PickListener results explicitely.
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Mandado <andres.mandado-almajano@here.com>
  • Loading branch information
atomicsulfate committed Jul 20, 2020
1 parent dc0937c commit bcd7362
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions @here/harp-mapview/lib/PickHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export class PickHandler {
}
}

pickListener.finish();
return pickListener.results;
}

Expand Down
34 changes: 22 additions & 12 deletions @here/harp-mapview/lib/PickListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { assert } from "@here/harp-utils";
import { IntersectParams } from "./IntersectParams";
import { PickResult } from "./PickHandler";

Expand All @@ -26,6 +27,7 @@ function defaultSort(lhs: PickResult, rhs: PickResult) {
export class PickListener {
private m_results: PickResult[] = [];
private m_sorted: boolean = true;
private m_finished: boolean = true;

/**
* Constructs a new `PickListener`.
Expand Down Expand Up @@ -55,6 +57,7 @@ export class PickListener {

if (foundFeatureIdx < 0) {
this.m_sorted = false;
this.m_finished = false;
this.m_results.push(result);
return;
}
Expand All @@ -64,6 +67,7 @@ export class PickListener {
if (defaultSort(result, oldResult) < 0) {
this.m_results[foundFeatureIdx] = result;
this.m_sorted = false;
this.m_finished = false;
}
}

Expand All @@ -76,18 +80,32 @@ export class PickListener {
}

/**
* Returns the collected results, ordered by distance first, then by reversed render order
* (topmost/highest render order first).
* Orders the collected results by distance first, then by reversed render order
* (topmost/highest render order first), and limits the number of results to the maximum
* accepted number, see {@link IntersectParams.maxResultCount}.
*/
finish(): void {
// Keep only the closest max results.
this.sortResults();
if (this.maxResults && this.m_results.length > this.maxResults) {
this.m_results.length = this.maxResults;
}
this.m_finished = true;
}

/**
* Returns the collected results. {@link PickListener.finish} should be called first to ensure
* the proper sorting and result count.
* @returns The pick results.
*/
get results(): PickResult[] {
this.finish();
assert(this.m_finished, "finish() was not called before getting the results");
return this.m_results;
}

/**
* Returns the closest result collected so far, following the order documented in
* {@link PickListener.results}
* {@link PickListener.finish}
* @returns The closest pick result, or `undefined` if no result was collected.
*/
get closestResult(): PickResult | undefined {
Expand Down Expand Up @@ -116,12 +134,4 @@ export class PickListener {
this.m_sorted = true;
}
}

private finish(): void {
// Keep only the closest max results.
this.sortResults();
if (this.maxResults && this.m_results.length > this.maxResults) {
this.m_results.length = this.maxResults;
}
}
}
15 changes: 11 additions & 4 deletions @here/harp-mapview/test/PickListenerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("PickListener", function() {
userData: {},
intersection
});

listener.finish();
expect(listener.results).to.have.lengthOf(5);
});

Expand Down Expand Up @@ -102,6 +102,7 @@ describe("PickListener", function() {
featureId: 1,
intersection
});
listener.finish();

expect(listener.results).to.have.lengthOf(1);
expect(listener.results[0]).equals(firstResult);
Expand All @@ -126,6 +127,7 @@ describe("PickListener", function() {
userData,
intersection
});
listener.finish();

expect(listener.results).to.have.lengthOf(1);
expect(listener.results[0]).equals(firstResult);
Expand Down Expand Up @@ -153,14 +155,15 @@ describe("PickListener", function() {
intersection
};
listener.addResult(closerResult);
listener.finish();

expect(listener.results).to.have.lengthOf(1);
expect(listener.results[0]).equals(closerResult);
});
});

describe("results", function() {
it("returns results ordered", function() {
describe("finish", function() {
it("orders the results", function() {
const listener = new PickListener();
const expectedResults: PickResult[] = [
{
Expand All @@ -185,11 +188,12 @@ describe("PickListener", function() {
listener.addResult(expectedResults[2]);
listener.addResult(expectedResults[1]);
listener.addResult(expectedResults[0]);
listener.finish();

expect(listener.results).to.have.ordered.members(expectedResults);
});

it("returns only the closest maximum result count if specified", function() {
it("keeps only the closest maximum result count if specified", function() {
const maxResultCount = 1;
const listener = new PickListener({ maxResultCount });
const results: PickResult[] = [
Expand All @@ -208,6 +212,7 @@ describe("PickListener", function() {
];
listener.addResult(results[1]);
listener.addResult(results[0]);
listener.finish();

expect(listener.results).to.have.lengthOf(maxResultCount);
expect(listener.results[0]).equals(results[0]);
Expand Down Expand Up @@ -238,6 +243,7 @@ describe("PickListener", function() {
];
listener.addResult(results[1]);
listener.addResult(results[0]);
listener.finish();

expect(listener.closestResult).to.equal(results[0]);
});
Expand Down Expand Up @@ -267,6 +273,7 @@ describe("PickListener", function() {
];
listener.addResult(results[1]);
listener.addResult(results[0]);
listener.finish();

expect(listener.furthestResult).to.equal(results[1]);
});
Expand Down

0 comments on commit bcd7362

Please sign in to comment.