Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/classes/screen.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ <h3>Methods</h3>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="screen.html#on" class="tsd-kind-icon">on</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="screen.html#waitfor" class="tsd-kind-icon">wait<wbr>For</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="screen.html#width" class="tsd-kind-icon">width</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="screen.html#cancelwaitfor" class="tsd-kind-icon">cancel<wbr>Wait<wbr>For</a></li>
</ul>
</section>
<section class="tsd-index-section ">
Expand Down Expand Up @@ -405,6 +406,37 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class">
<a name="cancelwaitfor" class="tsd-anchor"></a>
<h3>cancel<wbr>Wait<wbr>For</h3>
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class">
<li class="tsd-signature tsd-kind-icon">cancel<wbr>Wait<wbr>For<span class="tsd-signature-symbol">(</span>templateImageFilename<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">string</span>)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">&gt;</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/nut-tree/nut.js/blob/90e5072/lib/screen.class.ts#L193">screen.class.ts:144</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
<div class="lead">
<p><a href="screen.html#cancelwaitfor">cancelWaitFor</a> cancels a waitFor for any given template image</p>
</div>
</div>
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>templateImageFilename: <span class="tsd-signature-type">string</span></h5>
<div class="tsd-comment tsd-typography">
<p>Filename of the template image to cancel waitFor of, relative to <a href="screen.html#config.resourcedirectory">Screen.config.resourceDirectory</a></p>
</div>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">&gt;</span></h4>
</li>
</ul>
</section>
</section>
<section class="tsd-panel-group tsd-member-group ">
<h2>Object literals</h2>
Expand Down Expand Up @@ -539,6 +571,9 @@ <h3>resource<wbr>Directory</h3>
<li class=" tsd-kind-method tsd-parent-kind-class">
<a href="screen.html#width" class="tsd-kind-icon">width</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class">
<a href="screen.html#cancelwaitfor" class="tsd-kind-icon">cancel<wbr>Wait<wbr>For</a>
</li>
<li class=" tsd-kind-object-literal tsd-parent-kind-class">
<a href="screen.html#config" class="tsd-kind-icon">config</a>
</li>
Expand Down
22 changes: 22 additions & 0 deletions lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`);
Copy link
Author

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?

}
}
this.eventEmitter.addListener('cancelWaitFor', onCancel);
validateSearchRegion(searchRegion, screenSize);
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
if (matchResult.confidence >= minMatch) {
Expand All @@ -131,8 +142,10 @@ export class Screen {
matchResult.location.height
)
if (this.config.autoHighlight) {
this.eventEmitter.removeListener('cancelWaitFor', onCancel);
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The 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.
Having an explicit cancel method for each action with timeouts sound like too much overhead for me.

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
Expand Down