-
Notifications
You must be signed in to change notification settings - Fork 202
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
AbortController ponyfill #94
Changes from all commits
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,6 @@ | ||
export default function() { | ||
this.signal = { onabort: () => {} }; | ||
this.abort = () => { | ||
this.signal.onabort(); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ export default function(url, options) { | |
|
||
request.onerror = reject; | ||
|
||
if (options.signal) options.signal.onabort = () => { request.abort(); } | ||
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. if signal is already aborted it should reject promise, first thing: https://dom.spec.whatwg.org/#aborting-ongoing-activities-spec-example |
||
request.onabort = () => reject(new DOMException('The user aborted a request.')); | ||
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. this is to simulate what Chrome does, but i don't insist on it 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. per the spec (even that chrome exception has a name of AbortError): reject(new DOMException('Aborted', 'AbortError')); https://dom.spec.whatwg.org/#aborting-ongoing-activities 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. Also, don't think https://developer.mozilla.org/en-US/docs/Web/API/DOMException 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. I wonder what browsers don't have it. I can't find data about this in caniuse. Shall we use |
||
|
||
request.send(options.body || null); | ||
|
||
function response() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import fetch from '../src/index.mjs'; | ||
import AbortController from '../src/AbortController.mjs'; | ||
|
||
describe('AbortController', () => { | ||
it('should be a function', () => { | ||
expect(AbortController).toEqual(expect.any(Function)); | ||
}); | ||
|
||
describe('AbortController()', () => { | ||
let xhr; | ||
|
||
beforeEach(() => { | ||
xhr = { | ||
setRequestHeader: jest.fn(), | ||
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'), | ||
open: jest.fn(), | ||
send: jest.fn(), | ||
abort: jest.fn(() => xhr.onabort({ type: 'abort' })) , | ||
status: 200, | ||
statusText: 'OK', | ||
responseText: '{"a":"b"}', | ||
responseURL: '/foo?redirect' | ||
}; | ||
|
||
global.XMLHttpRequest = jest.fn(() => xhr); | ||
}); | ||
|
||
afterEach(() => { | ||
delete global.XMLHttpRequest; | ||
}); | ||
|
||
it('handles abort', () => { | ||
let controller = new AbortController(); | ||
let signal = controller.signal; | ||
let p = fetch('/foo', { signal }) | ||
.then(() => {}) | ||
.catch((e) => { | ||
expect(e.message).toEqual('The user aborted a request.'); | ||
}); | ||
|
||
controller.abort(); | ||
|
||
return p; | ||
}); | ||
}); | ||
}); |
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.
signal has an
aborted
flag indicating it's been aborted or not, which can be handy:https://dom.spec.whatwg.org/#abortsignal-aborted-flag
https://github.com/mo/abortcontroller-polyfill/blob/master/src/abortcontroller.js#L42
https://github.com/mysticatea/abort-controller/blob/master/src/abort-signal.mjs#L28