lazy-widgets
Version:
Typescript retained mode GUI for the HTML canvas API
41 lines • 1.18 kB
JavaScript
import { ConcurrentCollection } from './ConcurrentCollection.js';
/**
* A base class that can be listened to, and dispatch events.
*
* @category Helper
*/
export class Notifier {
constructor() {
this.listeners = new ConcurrentCollection();
}
/** Listen to events from this helper. Duplicate listeners allowed. */
addEventListener(listener) {
this.listeners.add(listener);
}
/**
* Stop listening to events from this helper. If a duplicate listener is
* removed, only one is removed.
*
* @returns True if a listener was removed, false otherwise.
*/
removeEventListener(listener) {
return this.listeners.removeByValue(listener);
}
/**
* Dispatch an event to a specific listener. Listener does not have to be
* added
*/
dispatchToListener(event, listener) {
try {
listener(event);
}
catch (err) {
console.error(err);
}
}
/** Dispatch an event to all listeners. */
dispatchEvent(event) {
this.listeners.forEach(this.dispatchToListener.bind(this, event));
}
}
//# sourceMappingURL=Notifier.js.map