-
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
Changes from all commits
6e790e6
1b26e66
d10059f
acf6a95
2d5d037
676db06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. IIRC the typescript types allow |
||||||||||||||||||
|
||||||||||||||||||
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 commentThe 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 |
||||||||||||||||||
scheduleNext(); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
const scheduleNext = () => { | ||||||||||||||||||
if (cancelled) { | ||||||||||||||||||
return; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
timeoutId = window.setTimeout(refreshAndScheduleNext, interval); | ||||||||||||||||||
Comment on lines
+13
to
+17
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. Maybe rather than an early return, purely stylistic
Suggested change
|
||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
const cancel = () => { | ||||||||||||||||||
cancelled = true; | ||||||||||||||||||
window.clearTimeout(timeoutId); | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
scheduleNext(); | ||||||||||||||||||
|
||||||||||||||||||
return cancel; | ||||||||||||||||||
}; |
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!