Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ext/event): optimize addEventListener options converter #20203

Merged
merged 5 commits into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 19 additions & 39 deletions ext/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,44 +896,28 @@ function getDefaultTargetData() {
};
}

// This is lazy loaded because there is a circular dependency with AbortSignal.
let addEventListenerOptionsConverter;
function addEventListenerOptionsConverter(V, prefix) {
if (webidl.type(V) !== "Object") {
return { capture: !!V, once: false, passive: false };
}

function lazyAddEventListenerOptionsConverter() {
addEventListenerOptionsConverter ??= webidl.createDictionaryConverter(
"AddEventListenerOptions",
[
{
key: "capture",
defaultValue: false,
converter: webidl.converters.boolean,
},
{
key: "passive",
defaultValue: false,
converter: webidl.converters.boolean,
},
{
key: "once",
defaultValue: false,
converter: webidl.converters.boolean,
},
{
key: "signal",
converter: webidl.converters.AbortSignal,
},
],
);
}
const options = {
capture: !!V.capture,
once: !!V.once,
passive: !!V.passive,
};

webidl.converters.AddEventListenerOptions = (V, prefix, context, opts) => {
if (webidl.type(V) !== "Object" || V === null) {
V = { capture: Boolean(V) };
const signal = V.signal;
if (signal !== undefined) {
options.signal = webidl.converters.AbortSignal(
signal,
prefix,
"'signal' of 'AddEventListenerOptions' (Argument 3)",
);
}

lazyAddEventListenerOptionsConverter();
return addEventListenerOptionsConverter(V, prefix, context, opts);
};
return options;
}

class EventTarget {
constructor() {
Expand All @@ -952,11 +936,7 @@ class EventTarget {

webidl.requiredArguments(arguments.length, 2, prefix);

options = webidl.converters.AddEventListenerOptions(
options,
prefix,
"Argument 3",
);
options = addEventListenerOptionsConverter(options, prefix);

if (callback === null) {
return;
Expand Down