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.", () => {
61
61
expect ( testCallback ) . toBeCalledWith ( matchResult ) ;
62
62
} ) ;
63
63
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
+
64
84
it ( "should reject with insufficient confidence." , async ( ) => {
65
85
const matchResult = new MatchResult ( 0.8 , searchRegion ) ;
66
86
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ export class Screen {
19
19
20
20
constructor (
21
21
private vision : VisionAdapter ,
22
- private findHooks : Map < string , FindHookCallback > = new Map < string , FindHookCallback > ( ) ) {
22
+ private findHooks : Map < string , FindHookCallback [ ] > = new Map < string , FindHookCallback [ ] > ( ) ) {
23
23
}
24
24
25
25
public width ( ) {
@@ -53,9 +53,9 @@ export class Screen {
53
53
try {
54
54
const matchResult = await this . vision . findOnScreenRegion ( matchRequest ) ;
55
55
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 ) ;
59
59
}
60
60
resolve ( matchResult . location ) ;
61
61
} else {
@@ -82,7 +82,8 @@ export class Screen {
82
82
}
83
83
84
84
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 ] ) ;
86
87
}
87
88
88
89
public async capture (
You can’t perform that action at this time.
0 commit comments