-
Notifications
You must be signed in to change notification settings - Fork 834
send alternatives on timeout toast #6920
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
base: main
Are you sure you want to change the base?
Changes from 28 commits
4475480
3aded0e
7ba995b
05806ee
b4130de
86810e7
6101b45
86d7f04
94d1e60
f2110a8
79ba675
7f6560c
8535c12
97f6186
aa436d3
961f4c1
ac10a50
366129c
bd140a1
d607f60
afa4381
a7588f6
3bd3862
6913bca
c694660
f5b88c1
9de43bb
9d9a91e
0a2d350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { CancelablePromise } from './gen' | ||
|
|
||
| export namespace CancelablePromiseUtils { | ||
| export function then<T, U>( | ||
| promise: CancelablePromise<T>, | ||
| f: (value: T) => CancelablePromise<U> | ||
| ): CancelablePromise<U> { | ||
| let promiseToBeCanceled: CancelablePromise<any> = promise | ||
| let p = new CancelablePromise<U>((resolve, reject) => { | ||
| promise | ||
| .then((value1) => { | ||
| let promise2 = f(value1) | ||
| promiseToBeCanceled = promise2 | ||
| promise2.then((value2) => resolve(value2)).catch((err) => reject(err)) | ||
| }) | ||
| .catch((err) => reject(err)) | ||
| }) | ||
| p.cancel = () => promiseToBeCanceled.cancel() | ||
| return p | ||
| } | ||
|
|
||
| export function pure<T>(value: T): CancelablePromise<T> { | ||
| return new CancelablePromise((resolve) => resolve(value)) | ||
| } | ||
|
|
||
| export function err<T>(error: any): CancelablePromise<T> { | ||
| return new CancelablePromise((_, reject) => reject(error)) | ||
| } | ||
|
|
||
| export function map<T, U>( | ||
| promise: CancelablePromise<T>, | ||
| f: (value: T) => U | ||
| ): CancelablePromise<U> { | ||
| return then(promise, (value) => pure(f(value))) | ||
| } | ||
|
|
||
| export function pipe<T>( | ||
| promise: CancelablePromise<T>, | ||
| f: (value: T) => void | ||
| ): CancelablePromise<T> { | ||
| promise.then((value) => { | ||
| f(value) | ||
| }) | ||
| return promise | ||
| } | ||
|
|
||
| export function catchErr<T, U>( | ||
| promise: CancelablePromise<T>, | ||
| f: (error: any) => CancelablePromise<U> | ||
| ): CancelablePromise<T | U> { | ||
| let promiseToBeCanceled: CancelablePromise<any> = promise | ||
| let p = new CancelablePromise<T | U>((resolve, reject) => { | ||
| promise | ||
| .then((value) => resolve(value)) | ||
| .catch((err) => { | ||
| let promise2 = f(err) | ||
| promiseToBeCanceled = promise2 | ||
| return promise2.then((value2) => resolve(value2)).catch((err2) => reject(err2)) | ||
| }) | ||
| .catch((err) => reject(err)) | ||
|
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. The second Consider removing this line for clarity. |
||
| }) | ||
| p.cancel = () => promiseToBeCanceled.cancel() | ||
| return p | ||
| } | ||
|
|
||
| export function finallyDo<T>(promise: CancelablePromise<T>, f: () => void): CancelablePromise<T> { | ||
| promise = map(promise, (value) => (f(), value)) | ||
| promise = catchErr(promise, (e) => (f(), err(e))) | ||
| return promise | ||
| } | ||
|
|
||
| // Calls onTimeout if the promise does not settle within timeoutMs milliseconds | ||
| export function onTimeout<T>( | ||
| promise: CancelablePromise<T>, | ||
| timeoutMs: number, | ||
| onTimeout: () => void | ||
| ): CancelablePromise<T> { | ||
| let timeoutId: number | undefined = setTimeout(onTimeout, timeoutMs) | ||
| promise = finallyDo(promise, () => { | ||
| if (timeoutId !== undefined) clearTimeout(timeoutId) | ||
| }) | ||
| return promise | ||
| } | ||
| } | ||
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.
🔴 Critical: Potential memory leak and unhandled rejection
The
pipefunction doesn't handle promise rejections, which will cause unhandled promise rejection warnings. Additionally, it doesn't properly integrate with the cancellation chain.Suggested fix:
Alternatively, if this function is not heavily used, consider using
finallyDoinstead, which already handles this pattern correctly.