-
Notifications
You must be signed in to change notification settings - Fork 58
add support for signal request option #399
base: master
Are you sure you want to change the base?
Conversation
add support for a signal: AbortSignal property in the request options When provided, the request can be aborted by calling the AbortController#abort method, similar to the fetch API
When abort is triggered, update the request Task to reject with a AbortError instead of canceling the Task. Tasks can still be canceled with the .cancel method.
src/request/providers/xhr.ts
Outdated
abortError = new Error('Aborted'); | ||
abortError.name = 'AbortError'; | ||
} | ||
reject(new DOMException('Aborted', 'AbortError')); |
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 like you should use abortError
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.
Nice catch! Thanks!
@@ -205,6 +205,19 @@ export default function xhr(url: string, options: XhrRequestOptions = {}): Uploa | |||
const task = <UploadObservableTask<XhrResponse>>new Task<XhrResponse>((resolve, reject) => { | |||
timeoutReject = reject; | |||
|
|||
if (options.signal) { | |||
options.signal.addEventListener('abort', () => { |
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.
when the request is fulfilled, the event listener should be removed.
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.
@ycabon I'm not sure there's a good place in the code to remove this event listener. Also, I'm not sure it's necessary as subsequent abort
events will have no effect on a fulfilled Task.
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.
My concern was more for cases where the signal is reused for multiple calls, would the Task be kept in memory until the signal and controller are GCed.
@@ -654,6 +654,15 @@ export default function node(url: string, options: NodeRequestOptions = {}): Upl | |||
timeoutReject && timeoutReject(new TimeoutError('The request timed out')); | |||
}, options.timeout); | |||
} | |||
|
|||
if (options.signal) { | |||
options.signal.addEventListener('abort', () => { |
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.
when the request is fulfilled, the event listener should be removed.
Type: feature
The following has been addressed in the PR:
prettier
as per the readme code style guidelinesDescription:
Note: This PR is dependent on dojo/shim#143.
Resolves #390
add support for a
signal: AbortSignal
property in the request options. When provided, the request can be aborted by calling theAbortController#abort
method, similar to the fetch API. When a request is aborted, thePromise
is rejected with anAbortError
, if possible.