Skip to content

Commit 3fba222

Browse files
committed
(#51) Find hooks
1 parent 2f1b734 commit 3fba222

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/screen.class.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ describe("Screen.", () => {
4545
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(matchRequest);
4646
});
4747

48+
it("should call registered hook before resolve", async () => {
49+
const matchResult = new MatchResult(0.99, searchRegion);
50+
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
51+
return Promise.resolve(matchResult);
52+
});
53+
const visionAdapterMock = new VisionAdapter();
54+
55+
const SUT = new Screen(visionAdapterMock);
56+
const testCallback = jest.fn(() => Promise.resolve());
57+
const imagePath = "test/path/to/image.png";
58+
SUT.on(imagePath, testCallback);
59+
await SUT.find(imagePath);
60+
expect(testCallback).toBeCalledTimes(1);
61+
expect(testCallback).toBeCalledWith(matchResult);
62+
});
63+
4864
it("should reject with insufficient confidence.", async () => {
4965
const matchResult = new MatchResult(0.8, searchRegion);
5066

lib/screen.class.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ import { FileType } from "./file-type.enum";
55
import { generateOutputPath } from "./generate-output-path.function";
66
import { LocationParameters } from "./locationparameters.class";
77
import { MatchRequest } from "./match-request.class";
8+
import { MatchResult } from "./match-result.class";
89
import { Region } from "./region.class";
910
import { timeout } from "./util/poll-action.function";
1011

12+
export type FindHookCallback = (target: MatchResult) => Promise<void>;
13+
1114
export class Screen {
1215
public config = {
1316
confidence: 0.99,
1417
resourceDirectory: cwd(),
1518
};
1619

17-
constructor(private vision: VisionAdapter) {
20+
constructor(
21+
private vision: VisionAdapter,
22+
private findHooks: Map<string, FindHookCallback> = new Map<string, FindHookCallback>()) {
1823
}
1924

2025
public width() {
@@ -34,7 +39,6 @@ export class Screen {
3439
(params && params.searchRegion) || await this.vision.screenSize();
3540

3641
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, pathToNeedle));
37-
// console.log(`Full path to needle: ${fullPathToNeedle}`);
3842

3943
const screenImage = await this.vision.grabScreen();
4044

@@ -49,6 +53,10 @@ export class Screen {
4953
try {
5054
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
5155
if (matchResult.confidence >= minMatch) {
56+
const possibleHook = this.findHooks.get(pathToNeedle);
57+
if (possibleHook) {
58+
await possibleHook(matchResult);
59+
}
5260
resolve(matchResult.location);
5361
} else {
5462
reject(
@@ -73,6 +81,10 @@ export class Screen {
7381
return timeout(500, timeoutMs, () => this.find(pathToNeedle, params));
7482
}
7583

84+
public on(pathToNeedle: string, callback: FindHookCallback) {
85+
this.findHooks.set(pathToNeedle, callback);
86+
}
87+
7688
public async capture(
7789
fileName: string,
7890
fileFormat: FileType = FileType.PNG,

0 commit comments

Comments
 (0)