-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add Timeout to Finder #79
Conversation
finder.ts
Outdated
@@ -20,6 +20,8 @@ export type Options = { | |||
optimizedMinLength: number | |||
threshold: number | |||
maxNumberOfTries: number | |||
timeoutMs: number |
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.
I see that the input to finder is using Partial but I think we can possibly use undefined for timeoutMs as well.
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.
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, |
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.
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) { |
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.
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(), |
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.
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?
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.
Good call!
@@ -20,6 +20,8 @@ export type Options = { | |||
optimizedMinLength: number | |||
threshold: number | |||
maxNumberOfTries: number | |||
timeoutMs: number | |||
start: Date | |||
} | |||
|
|||
let config: Options |
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.
I think we could have let start: Date | undefined here
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.
I took start out of options but I think you meant timeoutMs could be undefined
Cool! Will review it tonight. |
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.
Looks good!
Looks good! But let's update documentation on the readme. |
@antonmedv Thank you! I updated the README. |
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: