@inweb/eventemitter2
Version:
JavaScript event emitter
76 lines (73 loc) • 2.79 kB
JavaScript
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2026, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
// This application incorporates Open Design Alliance software pursuant to a
// license agreement with Open Design Alliance.
// Open Design Alliance Copyright (C) 2002-2026 by Open Design Alliance.
// All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
class EventEmitter2 {
constructor() {
this._listeners = {};
}
addEventListener(type, listener) {
if (this._listeners[type] === undefined)
this._listeners[type] = [];
this._listeners[type].push(listener);
return this;
}
removeEventListener(type, listener) {
if (this._listeners[type] === undefined)
return this;
const listeners = this._listeners[type].filter((x) => x !== listener);
if (listeners.length !== 0)
this._listeners[type] = listeners;
else
delete this._listeners[type];
return this;
}
removeAllListeners(type) {
if (type)
delete this._listeners[type];
else
this._listeners = {};
return this;
}
emitEvent(event) {
if (this._listeners[event.type] === undefined)
return false;
const invoke = this._listeners[event.type].slice();
invoke.forEach((listener) => listener.call(this, event));
return true;
}
on(type, listener) {
return this.addEventListener(type, listener);
}
off(type, listener) {
return this.removeEventListener(type, listener);
}
emit(type, ...args) {
if (typeof type === "string")
return this.emitEvent({ type, args });
else if (typeof type === "object")
return this.emitEvent(type);
else
return false;
}
}
export { EventEmitter2 };
//# sourceMappingURL=eventemitter2.module.js.map