File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,26 @@ describe("Screen.", () => {
6161 expect ( testCallback ) . toBeCalledWith ( matchResult ) ;
6262 } ) ;
6363
64+ it ( "should call multiple registered hooks before resolve" , async ( ) => {
65+ const matchResult = new MatchResult ( 0.99 , searchRegion ) ;
66+ VisionAdapter . prototype . findOnScreenRegion = jest . fn ( ( ) => {
67+ return Promise . resolve ( matchResult ) ;
68+ } ) ;
69+ const visionAdapterMock = new VisionAdapter ( ) ;
70+
71+ const SUT = new Screen ( visionAdapterMock ) ;
72+ const testCallback = jest . fn ( ( ) => Promise . resolve ( ) ) ;
73+ const secondCallback = jest . fn ( ( ) => Promise . resolve ( ) ) ;
74+ const imagePath = "test/path/to/image.png" ;
75+ SUT . on ( imagePath , testCallback ) ;
76+ SUT . on ( imagePath , secondCallback ) ;
77+ await SUT . find ( imagePath ) ;
78+ for ( const callback of [ testCallback , secondCallback ] ) {
79+ expect ( callback ) . toBeCalledTimes ( 1 ) ;
80+ expect ( callback ) . toBeCalledWith ( matchResult ) ;
81+ }
82+ } ) ;
83+
6484 it ( "should reject with insufficient confidence." , async ( ) => {
6585 const matchResult = new MatchResult ( 0.8 , searchRegion ) ;
6686
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ export class Screen {
1919
2020 constructor (
2121 private vision : VisionAdapter ,
22- private findHooks : Map < string , FindHookCallback > = new Map < string , FindHookCallback > ( ) ) {
22+ private findHooks : Map < string , FindHookCallback [ ] > = new Map < string , FindHookCallback [ ] > ( ) ) {
2323 }
2424
2525 public width ( ) {
@@ -53,9 +53,9 @@ export class Screen {
5353 try {
5454 const matchResult = await this . vision . findOnScreenRegion ( matchRequest ) ;
5555 if ( matchResult . confidence >= minMatch ) {
56- const possibleHook = this . findHooks . get ( pathToNeedle ) ;
57- if ( possibleHook ) {
58- await possibleHook ( matchResult ) ;
56+ const possibleHooks = this . findHooks . get ( pathToNeedle ) || [ ] ;
57+ for ( const hook of possibleHooks ) {
58+ await hook ( matchResult ) ;
5959 }
6060 resolve ( matchResult . location ) ;
6161 } else {
@@ -82,7 +82,8 @@ export class Screen {
8282 }
8383
8484 public on ( pathToNeedle : string , callback : FindHookCallback ) {
85- this . findHooks . set ( pathToNeedle , callback ) ;
85+ const existingHooks = this . findHooks . get ( pathToNeedle ) || [ ] ;
86+ this . findHooks . set ( pathToNeedle , [ ...existingHooks , callback ] ) ;
8687 }
8788
8889 public async capture (
You can’t perform that action at this time.
0 commit comments