-
Notifications
You must be signed in to change notification settings - Fork 113
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 async cancellable scheduling function #2321
Add async cancellable scheduling function #2321
Conversation
|
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'm not familiar with scheduleAsyncPoll
or how it works, but assuming there are no behavioral changes this looks fine to me!
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.
🎈
const refreshAndScheduleNext = async () => { | ||
await callback(); | ||
|
||
// eslint-disable-next-line no-use-before-define |
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.
Happily enough, the common JS config will remove the need for this override by configuring the no-use-before-define
rule to only complain about actual errors
@@ -0,0 +1,28 @@ | |||
export const scheduleAsyncPoll = (callback: () => Promise<void>, interval: number) => { | |||
let cancelled = false; | |||
let timeoutId = -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.
IIRC the typescript types allow undefined
for clearTimeout
(and it's certainly allowed at runtime) so I softly prefer that to a -1
magic value for "not set"
if (cancelled) { | ||
return; | ||
} | ||
|
||
timeoutId = window.setTimeout(refreshAndScheduleNext, interval); |
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.
Maybe rather than an early return, purely stylistic
if (cancelled) { | |
return; | |
} | |
timeoutId = window.setTimeout(refreshAndScheduleNext, interval); | |
if (!cancelled) { | |
timeoutId = window.setTimeout(refreshAndScheduleNext, interval); | |
} |
return displayError(error as ServiceError); | ||
} | ||
rcLogConditionally(request); | ||
props.client.encoderService.getProperties(request, new grpc.Metadata(), (error, response) => { |
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.
Seems like a good candidate for a promisification helper in a future PR
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.
Yeah, I think as a next step I'd like to start moving these out into an api
folder that promisifies all these callback apis. Quarantine zones FTW!
This PR introduces a new util,
scheduleAsyncPoll
, which schedules a clearable timeout for an async polling function. It also introduces better practices for polling in the Encoder card:I'll update other components in a follow-up PR.