Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

add support for signal request option #399

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nicknisi
Copy link
Member

@nicknisi nicknisi commented May 31, 2018

Type: feature

The following has been addressed in the PR:

Description:

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 the AbortController#abort method, similar to the fetch API. When a request is aborted, the Promise is rejected with an AbortError, if possible.

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
@nicknisi nicknisi changed the title add support for signal request option fixes #390 add support for signal request option May 31, 2018
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.
abortError = new Error('Aborted');
abortError.name = 'AbortError';
}
reject(new DOMException('Aborted', 'AbortError'));
Copy link

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.

Copy link
Member Author

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', () => {
Copy link

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.

Copy link
Member Author

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.

Copy link

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', () => {
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants