Skip to content

Commit 512ea05

Browse files
committed
lib: add weak event handlers
1 parent b41e402 commit 512ea05

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

lib/internal/event_target.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ class EventTarget {
455455
}
456456
const callback = handler.weak ?
457457
handler.callback.deref() : handler.callback;
458-
const result = FunctionPrototypeCall(callback, this, arg);
458+
let result;
459+
if (callback) {
460+
result = FunctionPrototypeCall(callback, this, arg);
461+
}
459462
if (result !== undefined && result !== null)
460463
addCatch(this, result, createEvent());
461464
} catch (err) {

test/parallel/test-events-static-geteventlisteners.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
2-
2+
// Flags: --expose-internals --no-warnings
33
const common = require('../common');
4+
const { kWeakHandler } = require('internal/event_target');
45

56
const {
67
deepStrictEqual,
@@ -41,3 +42,11 @@ const { getEventListeners, EventEmitter } = require('events');
4142
getEventListeners('INVALID_EMITTER');
4243
}, /ERR_INVALID_ARG_TYPE/);
4344
}
45+
{
46+
// Test weak listeners
47+
const target = new EventTarget();
48+
const fn = common.mustNotCall();
49+
target.addEventListener('foo', fn, { [kWeakHandler]: {} });
50+
const listeners = getEventListeners(target, 'foo');
51+
deepStrictEqual(listeners, [fn]);
52+
}

test/parallel/test-eventtarget.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,6 @@ let asyncTest = Promise.resolve();
588588
et.removeEventListener('foo', listener);
589589
et.dispatchEvent(new Event('foo'));
590590
}
591-
592591
{
593592
// Test listeners are held weakly
594593
const et = new EventTarget();

0 commit comments

Comments
 (0)