Skip to content

Commit

Permalink
fix(xmlhttprequest): replace to req.destroy from req.abort (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Jul 26, 2020
1 parent 7ed320a commit 99555d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,22 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
*/
abort(): void {
if (
this.readyState === XMLHttpRequest.HEADERS_RECEIVED ||
this.readyState === XMLHttpRequest.LOADING
this.readyState === XMLHttpRequest.UNSENT ||
this.readyState === XMLHttpRequest.OPENED ||
this.readyState === XMLHttpRequest.DONE ||
!this.#client
) {
this.#client?.abort();
return;
}

this.#client.destroy();

this.#readyState = XMLHttpRequest.UNSENT;

this.dispatchEvent(new ProgressEvent('abort'));
this.upload.dispatchEvent(new ProgressEvent('abort'));

this.#client = null;
}

/**
Expand Down Expand Up @@ -314,14 +325,6 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
protocol
});

this.#client.addListener('abort', () => {
this.dispatchEvent(new ProgressEvent('abort'));
this.upload.dispatchEvent(new ProgressEvent('abort'));

this.#client = null;
this.#readyState = XMLHttpRequest.UNSENT;
});

this.#client.addListener('error', () => {
this.dispatchEvent(new ProgressEvent('error'));
this.upload.dispatchEvent(new ProgressEvent('error'));
Expand Down
19 changes: 19 additions & 0 deletions test/integration/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ describe('XMLHttpRequest', () => {
baseURL = null;
});

describe('abort()', () => {
it('basic use case', (done) => {
const client = new XMLHttpRequest();

client.addEventListener('abort', (event) => {
expect(event.type).toBe('abort');
expect(client.readyState).toBe(XMLHttpRequest.UNSENT);

done();
});
client.addEventListener('loadstart', () => {
client.abort();
});

client.open('GET', `${baseURL}/?body=abort`);
client.send();
});
});

describe("addEventListener(event: 'error')", () => {
it('basic use case', (done) => {
const client = new XMLHttpRequest();
Expand Down

0 comments on commit 99555d8

Please sign in to comment.