Skip to content

Commit

Permalink
feat: Implement AbortController.prototype.abort(reason)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored and mo committed Oct 8, 2022
1 parent 1a255dc commit 8eb3ee5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/abortcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class AbortSignal extends Emitter {
// we want Object.keys(new AbortController().signal) to be [] for compat with the native impl
Object.defineProperty(this, 'aborted', { value: false, writable: true, configurable: true });
Object.defineProperty(this, 'onabort', { value: null, writable: true, configurable: true });
Object.defineProperty(this, 'reason', { value: undefined, writable: true, configurable: true });
}
toString() {
return '[object AbortSignal]';
Expand All @@ -82,7 +83,7 @@ export class AbortController {
// we want Object.keys(new AbortController()) to be [] for compat with the native impl
Object.defineProperty(this, 'signal', { value: new AbortSignal(), writable: true, configurable: true });
}
abort() {
abort(reason) {
let event;
try {
event = new Event('abort');
Expand All @@ -106,6 +107,18 @@ export class AbortController {
};
}
}

let signalReason = reason;
if (signalReason === undefined) {
if (typeof document === 'undefined') {
signalReason = new Error('This operation was aborted');
signalReason.name = 'AbortError';
} else {
signalReason = new DOMException('signal is aborted without reason');
}
}
this.signal.reason = signalReason;

this.signal.dispatchEvent(event);
}
toString() {
Expand Down
20 changes: 16 additions & 4 deletions tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const runBasicTests = (testSuiteTitle, TESTPAGE_URL) => {
it('abort during fetch', async () => {
const { server, baseUrl } = createFetchTargetServer();
await browser.url(TESTPAGE_URL);
const err = await browser.executeAsync(async (baseUrl, done) => {
const { errorName, signalReason } = await browser.executeAsync(async (baseUrl, done) => {
setTimeout(() => {
done({ name: 'fail' });
}, 2000);
Expand All @@ -64,11 +64,12 @@ const runBasicTests = (testSuiteTitle, TESTPAGE_URL) => {
}, 500);
try {
await fetch(`${baseUrl}?sleepMillis=1000`, { signal });
} catch (err) {
done(err);
} catch (error) {
done({ errorName: error.name, signalReason: signal.reason });
}
}, baseUrl);
expect(err.name).toBe('AbortError');
expect(errorName).toBe('AbortError');
expect(signalReason.message).toBe('signal is aborted without reason');
server.close();
});

Expand Down Expand Up @@ -335,6 +336,17 @@ const runBasicTests = (testSuiteTitle, TESTPAGE_URL) => {
});
expect(result).toBe('[object AbortSignal]');
});

it('abort(reason)', async () => {
await browser.url(TESTPAGE_URL);
const signalReason = await browser.executeAsync(async (done) => {
const controller = new AbortController();
controller.abort('My reason');

done(controller.signal.reason);
});
expect(signalReason).toEqual('My reason');
});
});
};

Expand Down

0 comments on commit 8eb3ee5

Please sign in to comment.