@@ -5,16 +5,21 @@ import { FileType } from "./file-type.enum";
55import { generateOutputPath } from "./generate-output-path.function" ;
66import { LocationParameters } from "./locationparameters.class" ;
77import { MatchRequest } from "./match-request.class" ;
8+ import { MatchResult } from "./match-result.class" ;
89import { Region } from "./region.class" ;
910import { timeout } from "./util/poll-action.function" ;
1011
12+ export type FindHookCallback = ( target : MatchResult ) => Promise < void > ;
13+
1114export 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