Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"test": "eslint src test && jest",
"build": "microbundle src/index.mjs && microbundle -f cjs polyfill/polyfill.mjs -o polyfill/index.js --no-sourcemap && cp dist/unfetch.mjs dist/unfetch.es.js",
"prepare": "npm run -s build",
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish"
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish",
"format": "eslint {src,test} --fix"
},
"repository": "developit/unfetch",
"keywords": [
Expand Down
6 changes: 6 additions & 0 deletions src/AbortController.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function() {
this.signal = { onabort: () => {} };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.abort = () => {
this.signal.onabort();
};
}
3 changes: 3 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default function(url, options) {

request.onerror = reject;

if (options.signal) options.signal.onabort = () => { request.abort(); }
Copy link

@joaovieira joaovieira Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request.onabort = () => reject(new DOMException('The user aborted a request.'));
Copy link
Author

@stereobooster stereobooster Sep 29, 2018

Choose a reason for hiding this comment

The 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

Copy link

@joaovieira joaovieira Oct 3, 2018

Choose a reason for hiding this comment

The 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
https://dom.spec.whatwg.org/#aborting-ongoing-activities-example
https://github.com/github/fetch/blob/master/fetch.js#L446

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The 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 Error instead?


request.send(options.body || null);

function response() {
Expand Down
46 changes: 46 additions & 0 deletions test/AbortController.js
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;
});
});
});