Skip to content

Commit

Permalink
lib: add ability to add separate event name to defineEventHandler
Browse files Browse the repository at this point in the history
Adds an optional third argument to `defineEventHandler()` to specify
an event name that is separate from the name used for the property.
This will be used, for instance, by the quic implementation to support
generating the `on...` events where the event name itself is hyphenated.
For instance `defineEventHandler(target, 'streamreset', 'stream-reset')`

Separated out from #44325

Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: #45032
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell authored and RafaelGSS committed Nov 10, 2022
1 parent 2105f09 commit 9ed9aa8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
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

0 comments on commit 9ed9aa8

Please sign in to comment.