@barchart/common-js
Version:
Library of common JavaScript utilities
188 lines (182 loc) • 5.38 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var EventMap_exports = {};
__export(EventMap_exports, {
default: () => EventMap
});
module.exports = __toCommonJS(EventMap_exports);
var assert = __toESM(require("./../lang/assert.js"));
var import_Disposable = __toESM(require("./../lang/Disposable.js"));
var import_Event = __toESM(require("./Event.js"));
class EventMap extends import_Disposable.default {
#events;
constructor() {
super();
this.#events = {};
}
/**
* Fires the appropriate event which is mapped to the event name.
* See {@link Event#fire} for more information.
*
* @public
* @param {string} eventName - The event's name.
* @param {*} data - The data to provide to observers.
*/
fire(eventName, data) {
const event = this.#events[eventName];
if (event) {
event.fire(data);
}
}
/**
* Registers a handler. See {@link Event#register} for more information.
*
* @public
* @param {string} eventName - The event's name.
* @param {Function} handler
* @returns {Disposable}
*/
register(eventName, handler) {
assert.argumentIsRequired(eventName, "eventName", String);
if (this.disposed) {
throw new Error("The event has been disposed.");
}
let event = this.#events[eventName];
if (!event) {
event = this.#events[eventName] = new import_Event.default(this);
}
return event.register(handler);
}
/**
* Removes a handler. See {@link Event#unregister} for more information.
*
* @public
* @param {string} eventName - The event's name.
* @param {Function} handler
*/
unregister(eventName, handler) {
assert.argumentIsRequired(eventName, "eventName", String);
const event = this.#events[eventName];
if (event) {
event.unregister(handler);
if (event.getIsEmpty()) {
delete this.#events[eventName];
}
}
}
/**
* Clears an event's handlers. See {@link Event#clear} for more information.
*
* @public
* @param {string} eventName - The event's name.
*/
clear(eventName) {
assert.argumentIsRequired(eventName, "eventName", String);
const event = this.#events[eventName];
if (event) {
event.clear();
delete this.#events[eventName];
}
}
/**
* Returns true, if no handlers are currently registered for the
* specified event. See {@link Event#getIsEmpty} for more information.
*
* @public
* @param {string} eventName
* @returns {boolean}
*/
getIsEmpty(eventName) {
const event = this.#events[eventName];
let returnVal;
if (event) {
returnVal = event.getIsEmpty();
} else {
returnVal = true;
}
return returnVal;
}
/**
* Returns an array of all the event names.
*
* @public
* @returns {Array<string>}
*/
getKeys() {
const keys = [];
for (let key in this.#events) {
if (Object.prototype.hasOwnProperty.call(this.#events, key)) {
keys.push(key);
}
}
return keys;
}
/**
* Returns true, if an event with the given name exists.
*
* @public
* @param {string} key
* @returns {boolean}
*/
hasKey(key) {
return Object.prototype.hasOwnProperty.call(this.#events, key);
}
/**
* @protected
* @override
*/
_onDispose() {
let keys = this.getKeys();
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
this.#events[key].dispose();
}
this.#events = {};
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[EventMap]";
}
}
{
const cjsExports = module.exports;
const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports;
if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) {
Object.keys(cjsExports).forEach((key) => {
if (key !== 'default' && key !== '__esModule') {
cjsDefaultExport[key] = cjsExports[key];
}
});
}
module.exports = cjsDefaultExport;
}