Add support for additional types of event listeners (#522)

* Add support for additional types of event listeners

* Fixes
This commit is contained in:
toasted-nutbread 2020-05-23 13:19:31 -04:00 committed by GitHub
parent d0dcff765f
commit 9a657214ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -255,15 +255,37 @@ class EventListenerCollection {
return this._eventListeners.length;
}
addEventListener(node, type, listener, options) {
node.addEventListener(type, listener, options);
this._eventListeners.push([node, type, listener, options]);
addEventListener(object, ...args) {
object.addEventListener(...args);
this._eventListeners.push(['removeEventListener', object, ...args]);
}
addListener(object, ...args) {
object.addListener(args);
this._eventListeners.push(['removeListener', object, ...args]);
}
on(object, ...args) {
object.on(args);
this._eventListeners.push(['off', object, ...args]);
}
removeAllEventListeners() {
if (this._eventListeners.length === 0) { return; }
for (const [node, type, listener, options] of this._eventListeners) {
node.removeEventListener(type, listener, options);
for (const [removeFunctionName, object, ...args] of this._eventListeners) {
switch (removeFunctionName) {
case 'removeEventListener':
object.removeEventListener(...args);
break;
case 'removeListener':
object.removeListener(...args);
break;
case 'off':
object.off(...args);
break;
default:
throw new Error(`Unknown remove function: ${removeFunctionName}`);
}
}
this._eventListeners = [];
}