Skip to content

Commit 656332a

Browse files
committedSep 9, 2020
(#169) [Proposal] Use promise-like return type instead of wrapping a promise by hand
1 parent 819ba1a commit 656332a

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed
 

Diff for: ‎lib/provider/opencv/template-matching-finder.class.spec.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ describe("Template-matching finder", () => {
6565
// THEN
6666
await expect(SUT.findMatch(matchRequest))
6767
.rejects
68-
.toEqual(
69-
expect
70-
.stringMatching(expectedRejection)
71-
);
68+
.toThrowError(expectedRejection);
7269
});
7370

7471
it("findMatch should throw on invalid image paths", async () => {
@@ -102,7 +99,7 @@ describe("Template-matching finder", () => {
10299
const minConfidence = 0.99;
103100
const searchRegion = new Region(0, 0, haystack.width, haystack.height);
104101
const matchRequest = new MatchRequest(haystack, needlePath, searchRegion, minConfidence);
105-
const expectedRejection = "The provided image sample is larger than the provided search region"
102+
const expectedRejection = new Error("The provided image sample is larger than the provided search region")
106103

107104
// WHEN
108105
const findMatchPromise = SUT.findMatch(matchRequest);

Diff for: ‎lib/provider/opencv/template-matching-finder.class.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,24 @@ export default class TemplateMatchingFinder implements FinderInterface {
132132
}
133133

134134
public async findMatch(matchRequest: MatchRequest, debug: boolean = false): Promise<MatchResult> {
135-
return new Promise<MatchResult>(async (resolve, reject) => {
136-
try {
137-
const matches = await this.findMatches(matchRequest, debug);
138-
const potentialMatches = matches
139-
.filter(match => match.confidence >= matchRequest.confidence);
140-
if (potentialMatches.length === 0) {
141-
matches.sort((a, b) => a.confidence - b.confidence);
142-
const bestMatch = matches.pop();
143-
if (bestMatch) {
144-
if(bestMatch.error) {
145-
reject(bestMatch.error.message)
146-
}else {
147-
reject(`No match with required confidence ${matchRequest.confidence}. Best match: ${bestMatch.confidence} at ${bestMatch.location}`)
148-
}
149-
} else {
150-
reject(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
151-
}
135+
136+
const matches = await this.findMatches(matchRequest, debug);
137+
const potentialMatches = matches
138+
.filter(match => match.confidence >= matchRequest.confidence);
139+
if (potentialMatches.length === 0) {
140+
matches.sort((a, b) => a.confidence - b.confidence);
141+
const bestMatch = matches.pop();
142+
if (bestMatch) {
143+
if(bestMatch.error) {
144+
throw new Error(bestMatch.error.message)
145+
}else {
146+
throw new Error(`No match with required confidence ${matchRequest.confidence}. Best match: ${bestMatch.confidence} at ${bestMatch.location}`)
152147
}
153-
resolve(potentialMatches[0]);
154-
} catch (e) {
155-
reject(e);
148+
} else {
149+
throw new Error(`Unable to locate ${matchRequest.pathToNeedle}, no match!`);
156150
}
157-
});
151+
}
152+
return potentialMatches[0];
158153
}
159154

160155
private searchMultipleScales(needle: cv.Mat, haystack: cv.Mat) {

0 commit comments

Comments
 (0)
Please sign in to comment.