Starting Settings modules + implemented basic events for ECMAScript <=> Qt compat.
This commit is contained in:
parent
9663c33563
commit
af2950c3d2
5 changed files with 276 additions and 5 deletions
96
common/src/events.mjs
Normal file
96
common/src/events.mjs
Normal file
|
@ -0,0 +1,96 @@
|
|||
/**
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
||||
* Copyright (C) 2021-2024 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* We do not inherit the DOM's Event, because not only the DOM part is unnecessary,
|
||||
* but also because it does not exist within Qt environments.
|
||||
*/
|
||||
|
||||
|
||||
export class BaseEvent {
|
||||
/**
|
||||
* @property {string} name - Name of the event.
|
||||
*/
|
||||
constructor(name) {
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Base class for all classes which can emit events.
|
||||
*/
|
||||
export class BaseEventEmitter {
|
||||
static emits = []
|
||||
|
||||
/** @type {Record<string, function[]>} */
|
||||
#listeners = {}
|
||||
|
||||
constructor() {
|
||||
for(const eventType of this.constructor.emits) {
|
||||
this.#listeners[eventType] = new Set()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener to an event that can be emitted by this object.
|
||||
*
|
||||
* @param {string} eventType - Name of the event to listen to. Throws an error if this object does not emit this kind of event.
|
||||
* @param {function(BaseEvent)} eventListener - The function to be called back when the event is emitted.
|
||||
*/
|
||||
addEventListener(eventType, eventListener) {
|
||||
if(!this.emits.includes(eventType)) {
|
||||
const className = this.constructor.name
|
||||
const eventTypes = this.constructor.emits.join(", ")
|
||||
throw new Error(`Cannot listen to unknown event ${eventType} in class ${className}. ${className} only emits: ${eventTypes}`)
|
||||
}
|
||||
if(!this.#listeners[eventType].has(eventListener))
|
||||
this.#listeners[eventType].add(eventListener)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remvoes a listener from an event that can be emitted by this object.
|
||||
*
|
||||
* @param {string} eventType - Name of the event that was listened to. Throws an error if this object does not emit this kind of event.
|
||||
* @param {function(BaseEvent)} eventListener - The function previously registered as a listener.
|
||||
* @returns {boolean} True if the listener was removed, false if it was not found.
|
||||
*/
|
||||
removeEventListener(eventType, eventListener) {
|
||||
if(!this.emits.includes(eventType)) {
|
||||
const className = this.constructor.name
|
||||
const eventTypes = this.constructor.emits.join(", ")
|
||||
throw new Error(`Cannot listen to unknown event ${eventType} in class ${className}. ${className} only emits: ${eventTypes}`)
|
||||
}
|
||||
return this.#listeners[eventType].delete(eventListener)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event to all of its listeners.
|
||||
*
|
||||
* @param {BaseEvent} e
|
||||
*/
|
||||
emit(e) {
|
||||
if(!(e instanceof BaseEvent))
|
||||
throw new Error("Cannot emit non event object.")
|
||||
if(!this.emits.includes(e.name))
|
||||
throw new Error(`Cannot emit event '${e.name}' from class ${this.constructor.name}. ${this.constructor.name} can only emits: ${this.constructor.emits.join(", ")}.`)
|
||||
for(const listener of this.#listeners[e.name])
|
||||
listener(e)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue