This repository was archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Improve "until": Don't evaluate the condition twice if it's true from the beginning #250
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -488,29 +488,35 @@ export async function sleep(ms: number = 0) { | |
| }) | ||
| } | ||
|
|
||
| // condition could as well return any instead of boolean, could be convenient sometimes if waiting until a value is returned. Maybe change if such use case emerges. | ||
| /** | ||
| * Wait until a condition is true | ||
| * @param condition - wait until this callback function returns true | ||
| * @param timeOutMs - stop waiting after that many milliseconds, -1 for disable | ||
| * @param pollingIntervalMs - check condition between so many milliseconds | ||
| * @param failedMsgFn - append the string return value of this getter function to the error message, if given | ||
| * @return the (last) truthy value returned by the condition function | ||
| */ | ||
| export async function until(condition: MaybeAsync<() => boolean>, timeOutMs = 10000, pollingIntervalMs = 100, failedMsgFn?: () => string) { | ||
| const err = new Error(`Timeout after ${timeOutMs} milliseconds`) | ||
| let timeout = false | ||
| if (timeOutMs > 0) { | ||
| setTimeout(() => { timeout = true }, timeOutMs) | ||
| } | ||
|
|
||
| // Promise wrapped condition function works for normal functions just the same as Promises | ||
| while (!await Promise.resolve().then(condition)) { // eslint-disable-line no-await-in-loop | ||
| if (timeout) { | ||
| if (failedMsgFn) { | ||
| err.message += ` ${failedMsgFn()}` | ||
| } | ||
| throw err | ||
| let wasDone | ||
| while (!wasDone && !timeout) { // eslint-disable-line no-await-in-loop | ||
| wasDone = await Promise.resolve().then(condition) // eslint-disable-line no-await-in-loop | ||
| if (!wasDone && !timeout) { | ||
| await sleep(pollingIntervalMs) // eslint-disable-line no-await-in-loop | ||
| } | ||
|
|
||
| await sleep(pollingIntervalMs) // eslint-disable-line no-await-in-loop | ||
| } | ||
| return condition() | ||
| if (timeout) { | ||
| if (failedMsgFn) { | ||
| err.message += ` ${failedMsgFn()}` | ||
| } | ||
| throw err | ||
| } | ||
| return wasDone | ||
|
Contributor
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. This can never return |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
booleanis perfectly fine IMO.anydoesn't communicate that we're expecting atruthyorfalseyresult, and in the worst case someone just needs to doreturn !!valueinstead ofreturn valueAlso note line length too long.
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.
Ahh I see, you mean it could return the first/last
truthyresult of callingcondition()… interesting.Probably don't want
anyfor that, instead you'd infer the type fromcondition, something like: