FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

AbortController ponyfill by stereobooster · Pull Request #94 · developit/unfetch · GitHub

Repository navigation

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

Filter by extension

Filter by extension .js  (1) .json  (2) .mjs  (2) All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function() {
this.signal = { onabort: () => {} };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

this.abort = () => {
this.signal.onabort();
};
}
3 changes: 3 additions & 0 deletions src/index.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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(); }

joaovieira Oct 3, 2018 •
edited
Loading

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

stereobooster Sep 29, 2018 •
edited
Loading

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

this is to simulate what Chrome does, but i don't insist on it

joaovieira Oct 3, 2018 •
edited
Loading

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
});
});
});

Back | FazBrowse Home | New Git URL