Skip to content

Commit a303721

Browse files
Add AbortSignal's reason property
Implements whatwg/dom#1027.
1 parent 158ada2 commit a303721

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

lib/jsdom/living/aborting/AbortController-impl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class AbortControllerImpl {
77
this.signal = AbortSignal.createImpl(globalObject, []);
88
}
99

10-
abort() {
11-
this.signal._signalAbort();
10+
abort(reason) {
11+
this.signal._signalAbort(reason);
1212
}
1313
}
1414

lib/jsdom/living/aborting/AbortController.webidl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ interface AbortController {
55

66
[SameObject] readonly attribute AbortSignal signal;
77

8-
undefined abort();
8+
undefined abort(optional any reason);
99
};

lib/jsdom/living/aborting/AbortSignal-impl.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { setupForSimpleEventAccessors } = require("../helpers/create-event-access
44
const { fireAnEvent } = require("../helpers/events");
55
const EventTargetImpl = require("../events/EventTarget-impl").implementation;
66
const AbortSignal = require("../generated/AbortSignal");
7+
const DOMException = require("domexception/webidl2js-wrapper");
78

89
class AbortSignalImpl extends EventTargetImpl {
910
constructor(globalObject, args, privateData) {
@@ -12,21 +13,34 @@ class AbortSignalImpl extends EventTargetImpl {
1213
// make event firing possible
1314
this._ownerDocument = globalObject.document;
1415

15-
this.aborted = false;
16+
this.reason = undefined;
1617
this.abortAlgorithms = new Set();
1718
}
1819

19-
static abort(globalObject) {
20+
get aborted() {
21+
return this.reason !== undefined;
22+
}
23+
24+
static abort(globalObject, reason) {
2025
const abortSignal = AbortSignal.createImpl(globalObject, []);
21-
abortSignal.aborted = true;
26+
if (reason !== undefined) {
27+
abortSignal.reason = reason;
28+
} else {
29+
abortSignal.reason = DOMException.create(globalObject, ["The operation was aborted.", "AbortError"]);
30+
}
2231
return abortSignal;
2332
}
2433

25-
_signalAbort() {
34+
_signalAbort(reason) {
2635
if (this.aborted) {
2736
return;
2837
}
29-
this.aborted = true;
38+
39+
if (reason !== undefined) {
40+
this.reason = reason;
41+
} else {
42+
this.reason = DOMException.create(this._globalObject, ["The operation was aborted.", "AbortError"]);
43+
}
3044

3145
for (const algorithm of this.abortAlgorithms) {
3246
algorithm();
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[Exposed=(Window,Worker)]
22
interface AbortSignal : EventTarget {
3-
[WebIDL2JSCallWithGlobal, NewObject] static AbortSignal abort();
3+
[WebIDL2JSCallWithGlobal, NewObject] static AbortSignal abort(optional any reason);
44

55
readonly attribute boolean aborted;
6+
readonly attribute any reason;
67

78
attribute EventHandler onabort;
89
};

test/web-platform-tests/to-run.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,6 @@ throw-on-dynamic-markup-insertion-counter-reactions.html: [timeout, document.ope
173173

174174
DIR: dom/abort
175175

176-
event.any.html:
177-
"AbortController abort() should fire event synchronously": [fail, AbortSignal.reason is not implemented]
178-
"AbortController abort(reason) should set signal.reason": [fail, AbortSignal.reason is not implemented]
179-
"aborting AbortController without reason creates an \"AbortError\" DOMException": [fail, AbortSignal.reason is not implemented]
180-
"AbortController abort(undefined) creates an \"AbortError\" DOMException": [fail, AbortSignal.reason is not implemented]
181-
"static aborting signal should have right properties": [fail, AbortSignal.reason is not implemented]
182-
"static aborting signal with reason should set signal.reason": [fail, AbortSignal.reason is not implemented]
183-
184176
---
185177

186178
DIR: dom/collections

0 commit comments

Comments
 (0)