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

lib: add ability to add separate event name to defineEventHandler #45032

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,12 +1022,12 @@ function makeEventHandler(handler) {
return eventHandler;
}

function defineEventHandler(emitter, name) {
function defineEventHandler(emitter, name, event = name) {
// 8.1.5.1 Event handlers - basically `on[eventName]` attributes
const propName = `on${name}`;
function get() {
validateInternalField(this, kHandlers, 'EventTarget');
return this[kHandlers]?.get(name)?.handler ?? null;
return this[kHandlers]?.get(event)?.handler ?? null;
}
ObjectDefineProperty(get, 'name', {
__proto__: null,
Expand All @@ -1036,24 +1036,24 @@ function defineEventHandler(emitter, name) {

function set(value) {
validateInternalField(this, kHandlers, 'EventTarget');
let wrappedHandler = this[kHandlers]?.get(name);
let wrappedHandler = this[kHandlers]?.get(event);
if (wrappedHandler) {
if (typeof wrappedHandler.handler === 'function') {
this[kEvents].get(name).size--;
const size = this[kEvents].get(name).size;
this[kRemoveListener](size, name, wrappedHandler.handler, false);
this[kEvents].get(event).size--;
const size = this[kEvents].get(event).size;
this[kRemoveListener](size, event, wrappedHandler.handler, false);
}
wrappedHandler.handler = value;
if (typeof wrappedHandler.handler === 'function') {
this[kEvents].get(name).size++;
const size = this[kEvents].get(name).size;
this[kNewListener](size, name, value, false, false, false, false);
this[kEvents].get(event).size++;
const size = this[kEvents].get(event).size;
this[kNewListener](size, event, value, false, false, false, false);
}
} else {
wrappedHandler = makeEventHandler(value);
this.addEventListener(name, wrappedHandler);
this.addEventListener(event, wrappedHandler);
}
this[kHandlers].set(name, wrappedHandler);
this[kHandlers].set(event, wrappedHandler);
}
ObjectDefineProperty(set, 'name', {
__proto__: null,
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,15 @@ let asyncTest = Promise.resolve();
target.dispatchEvent(new Event('foo'));
deepStrictEqual(output, [1, 2, 3, 4]);
}
{
const target = new EventTarget();
defineEventHandler(target, 'foo', 'bar');
const output = [];
target.addEventListener('bar', () => output.push(1));
target.onfoo = () => output.push(2);
target.dispatchEvent(new Event('bar'));
deepStrictEqual(output, [1, 2]);
}
{
const et = new EventTarget();
const listener = common.mustNotCall();
Expand Down