MeshCentral/public/novnc/core/util/eventtarget.js

36 lines
896 B
JavaScript
Raw Normal View History

2020-06-06 21:55:47 -04:00
/*
* noVNC: HTML5 VNC client
2021-11-29 15:08:15 -05:00
* Copyright (C) 2019 The noVNC Authors
2020-06-06 21:55:47 -04:00
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
export default class EventTargetMixin {
constructor() {
this._listeners = new Map();
}
addEventListener(type, callback) {
if (!this._listeners.has(type)) {
this._listeners.set(type, new Set());
}
this._listeners.get(type).add(callback);
}
removeEventListener(type, callback) {
if (this._listeners.has(type)) {
this._listeners.get(type).delete(callback);
}
}
dispatchEvent(event) {
if (!this._listeners.has(event.type)) {
return true;
}
this._listeners.get(event.type)
.forEach(callback => callback.call(this, event));
return !event.defaultPrevented;
}
}