Skip to content
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

Add Timeout to Finder #79

Merged
merged 4 commits into from
Feb 22, 2024
Merged

Conversation

lapritchett
Copy link
Contributor

@lapritchett lapritchett commented Feb 21, 2024

Addresses Issue #77
Adding an optional timeout in milliseconds to finder for cases where finder calls need to execute under a certain time threshold.

I tested this by temporarily updating L14 of tests/test.js to function check(html, config = {timeoutMs: 1}) { and seeing that a few tests timed out:

Screenshot 2024-02-20 at 6 14 18 PM

finder.ts Outdated
@@ -20,6 +20,8 @@ export type Options = {
optimizedMinLength: number
threshold: number
maxNumberOfTries: number
timeoutMs: number

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the input to finder is using Partial but I think we can possibly use undefined for timeoutMs as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try that out! -1 is less straight-forward

finder.ts Outdated
@@ -42,6 +44,8 @@ export function finder(input: Element, options?: Partial<Options>) {
optimizedMinLength: 2,
threshold: 1000,
maxNumberOfTries: 10000,
timeoutMs: -1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we may just want to set timeoutMs to undefined.

finder.ts Outdated
@@ -84,6 +88,10 @@ function bottomUpSearch(
let current: Element | null = input
let i = 0
while (current) {
const elapsedTime = new Date().getTime() - config.start.getTime();
if (config.timeoutMs !== -1 && elapsedTime > config.timeoutMs) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here we can check if config.timeoutMs is defined

finder.ts Outdated
@@ -42,6 +44,8 @@ export function finder(input: Element, options?: Partial<Options>) {
optimizedMinLength: 2,
threshold: 1000,
maxNumberOfTries: 10000,
timeoutMs: -1,
start: new Date(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me start seems more like an internal constant that does not need to be exposed through options. Can we set the start const globally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

@@ -20,6 +20,8 @@ export type Options = {
optimizedMinLength: number
threshold: number
maxNumberOfTries: number
timeoutMs: number
start: Date
}

let config: Options

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could have let start: Date | undefined here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took start out of options but I think you meant timeoutMs could be undefined

@antonmedv
Copy link
Owner

Cool! Will review it tonight.

Copy link

@kylevillegas93 kylevillegas93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@kylevillegas93 kylevillegas93 mentioned this pull request Feb 21, 2024
@antonmedv
Copy link
Owner

Looks good! But let's update documentation on the readme.

@lapritchett
Copy link
Contributor Author

Looks good! But let's update documentation on the readme.

@antonmedv Thank you! I updated the README.

@antonmedv antonmedv merged commit 82421db into antonmedv:master Feb 22, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants