-
-
Notifications
You must be signed in to change notification settings - Fork 150
added method to cancel screen waitFor #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,18 @@ import {MatchResult} from "./match-result.class"; | |
import {Region} from "./region.class"; | ||
import {timeout} from "./util/poll-action.function"; | ||
import { Image } from "./image.class"; | ||
import { EventEmitter } from 'events'; | ||
|
||
export type FindHookCallback = (target: MatchResult) => Promise<void>; | ||
|
||
/** | ||
* {@link Screen} class provides methods to access screen content of a systems main display | ||
*/ | ||
export class Screen { | ||
/** | ||
* Event Emitter for cancelling waitFor | ||
*/ | ||
private eventEmitter: EventEmitter = new EventEmitter(); | ||
|
||
/** | ||
* Config object for {@link Screen} class | ||
|
@@ -117,6 +122,12 @@ export class Screen { | |
|
||
return new Promise<Region>(async (resolve, reject) => { | ||
try { | ||
const onCancel = (cancelTemplateImageFileName: string) => { | ||
if (templateImageFilename === cancelTemplateImageFileName) { | ||
reject(`Cancelled searching for ${cancelTemplateImageFileName}`); | ||
} | ||
} | ||
this.eventEmitter.addListener('cancelWaitFor', onCancel); | ||
validateSearchRegion(searchRegion, screenSize); | ||
const matchResult = await this.vision.findOnScreenRegion(matchRequest); | ||
if (matchResult.confidence >= minMatch) { | ||
|
@@ -131,8 +142,10 @@ export class Screen { | |
matchResult.location.height | ||
) | ||
if (this.config.autoHighlight) { | ||
this.eventEmitter.removeListener('cancelWaitFor', onCancel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Listeners are not removed on error paths and thus pile up over time |
||
resolve(this.highlight(resultRegion)); | ||
} else { | ||
this.eventEmitter.removeListener('cancelWaitFor', onCancel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Listeners are not removed on error paths and thus pile up over time |
||
resolve(resultRegion); | ||
} | ||
} else { | ||
|
@@ -174,6 +187,15 @@ export class Screen { | |
return timeout(500, timeoutMs, () => this.find(templateImageFilename, params)); | ||
} | ||
|
||
/** | ||
* @param templateImageFilename Filename of the template image to cancel waitFor of, relative to {@link Screen.config.resourceDirectory} | ||
*/ | ||
public async cancelWaitFor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I don't think having explicit methods to cancel actions is something I want. |
||
templateImageFilename: string, | ||
): Promise<boolean> { | ||
return this.eventEmitter.emit('cancelWaitFor', templateImageFilename); | ||
} | ||
|
||
/** | ||
* {@link on} registeres a callback which is triggered once a certain template image is found | ||
* @param templateImageFilename Template image to trigger the callback on | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@s1hofmann @svettwer Not sure, that whether we should reject or resolve it here?