mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
fix(op_crates/web): Expose event properties in console output (#8103)
Fixes #8073
This commit is contained in:
parent
b03f4a4a1c
commit
9fb4931a95
3 changed files with 104 additions and 5 deletions
|
@ -61,7 +61,7 @@
|
|||
}).then(() => {
|
||||
this.#readyState = CLOSED;
|
||||
|
||||
const errEvent = new Event("error");
|
||||
const errEvent = new ErrorEvent("error");
|
||||
errEvent.target = this;
|
||||
this.onerror?.(errEvent);
|
||||
this.dispatchEvent(errEvent);
|
||||
|
@ -84,7 +84,7 @@
|
|||
} else {
|
||||
this.#readyState = CLOSED;
|
||||
|
||||
const errEvent = new Event("error");
|
||||
const errEvent = new ErrorEvent("error");
|
||||
errEvent.target = this;
|
||||
this.onerror?.(errEvent);
|
||||
this.dispatchEvent(errEvent);
|
||||
|
@ -289,7 +289,7 @@
|
|||
} else if (message.type === "error") {
|
||||
this.#readyState = CLOSED;
|
||||
|
||||
const errorEv = new Event("error");
|
||||
const errorEv = new ErrorEvent("error");
|
||||
errorEv.target = this;
|
||||
this.onerror?.(errorEv);
|
||||
this.dispatchEvent(errorEv);
|
||||
|
|
|
@ -92,3 +92,41 @@ unitTest(function eventIsTrusted(): void {
|
|||
|
||||
assertEquals(desc1!.get, desc2!.get);
|
||||
});
|
||||
|
||||
unitTest(function eventInspectOutput(): void {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const cases: Array<[any, (event: any) => string]> = [
|
||||
[
|
||||
new Event("test"),
|
||||
(event: Event) =>
|
||||
`Event {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "test"\n}`,
|
||||
],
|
||||
[
|
||||
new ErrorEvent("error"),
|
||||
(event: Event) =>
|
||||
`ErrorEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "error",\n message: "",\n filename: "",\n lineno: 0,\n colno: 0,\n error: null\n}`,
|
||||
],
|
||||
[
|
||||
new CloseEvent("close"),
|
||||
(event: Event) =>
|
||||
`CloseEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "close",\n wasClean: false,\n code: 0,\n reason: ""\n}`,
|
||||
],
|
||||
[
|
||||
new CustomEvent("custom"),
|
||||
(event: Event) =>
|
||||
`CustomEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "custom",\n detail: undefined\n}`,
|
||||
],
|
||||
[
|
||||
new ProgressEvent("progress"),
|
||||
(event: Event) =>
|
||||
`ProgressEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "progress",\n lengthComputable: false,\n loaded: 0,\n total: 0\n}`,
|
||||
],
|
||||
];
|
||||
|
||||
for (const [event, outputProvider] of cases) {
|
||||
assertEquals(
|
||||
event[Symbol.for("Deno.customInspect")](),
|
||||
outputProvider(event),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -133,6 +133,10 @@
|
|||
});
|
||||
}
|
||||
|
||||
[Symbol.for("Deno.customInspect")]() {
|
||||
return buildCustomInspectOutput(this, EVENT_PROPS);
|
||||
}
|
||||
|
||||
get bubbles() {
|
||||
return this.#attributes.bubbles;
|
||||
}
|
||||
|
@ -373,6 +377,16 @@
|
|||
}
|
||||
}
|
||||
|
||||
function buildCustomInspectOutput(obj, props) {
|
||||
const inspectObj = {};
|
||||
|
||||
for (const prop of props) {
|
||||
inspectObj[prop] = obj[prop];
|
||||
}
|
||||
|
||||
return `${obj.constructor.name} ${Deno.inspect(inspectObj)}`;
|
||||
}
|
||||
|
||||
function defineEnumerableProps(
|
||||
Ctor,
|
||||
props,
|
||||
|
@ -382,7 +396,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
defineEnumerableProps(Event, [
|
||||
const EVENT_PROPS = [
|
||||
"bubbles",
|
||||
"cancelable",
|
||||
"composed",
|
||||
|
@ -392,7 +406,9 @@
|
|||
"target",
|
||||
"timeStamp",
|
||||
"type",
|
||||
]);
|
||||
];
|
||||
|
||||
defineEnumerableProps(Event, EVENT_PROPS);
|
||||
|
||||
// This is currently the only node type we are using, so instead of implementing
|
||||
// the whole of the Node interface at the moment, this just gives us the one
|
||||
|
@ -1001,6 +1017,17 @@
|
|||
get [Symbol.toStringTag]() {
|
||||
return "ErrorEvent";
|
||||
}
|
||||
|
||||
[Symbol.for("Deno.customInspect")]() {
|
||||
return buildCustomInspectOutput(this, [
|
||||
...EVENT_PROPS,
|
||||
"message",
|
||||
"filename",
|
||||
"lineno",
|
||||
"colno",
|
||||
"error",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
defineEnumerableProps(ErrorEvent, [
|
||||
|
@ -1044,6 +1071,15 @@
|
|||
this.#code = code;
|
||||
this.#reason = reason;
|
||||
}
|
||||
|
||||
[Symbol.for("Deno.customInspect")]() {
|
||||
return buildCustomInspectOutput(this, [
|
||||
...EVENT_PROPS,
|
||||
"wasClean",
|
||||
"code",
|
||||
"reason",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class MessageEvent extends Event {
|
||||
|
@ -1058,6 +1094,15 @@
|
|||
this.origin = eventInitDict?.origin ?? "";
|
||||
this.lastEventId = eventInitDict?.lastEventId ?? "";
|
||||
}
|
||||
|
||||
[Symbol.for("Deno.customInspect")]() {
|
||||
return buildCustomInspectOutput(this, [
|
||||
...EVENT_PROPS,
|
||||
"data",
|
||||
"origin",
|
||||
"lastEventId",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class CustomEvent extends Event {
|
||||
|
@ -1077,6 +1122,13 @@
|
|||
get [Symbol.toStringTag]() {
|
||||
return "CustomEvent";
|
||||
}
|
||||
|
||||
[Symbol.for("Deno.customInspect")]() {
|
||||
return buildCustomInspectOutput(this, [
|
||||
...EVENT_PROPS,
|
||||
"detail",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reflect.defineProperty(CustomEvent.prototype, "detail", {
|
||||
|
@ -1093,6 +1145,15 @@
|
|||
this.loaded = eventInitDict?.loaded ?? 0;
|
||||
this.total = eventInitDict?.total ?? 0;
|
||||
}
|
||||
|
||||
[Symbol.for("Deno.customInspect")]() {
|
||||
return buildCustomInspectOutput(this, [
|
||||
...EVENT_PROPS,
|
||||
"lengthComputable",
|
||||
"loaded",
|
||||
"total",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
window.Event = Event;
|
||||
|
|
Loading…
Reference in a new issue