UNPKG

web-signature

Version:

Primitive and fast framework for rendering web interfaces

79 lines (78 loc) 2.74 kB
export default class Library { name; version; author; libs = {}; components = {}; /** * @param {string} name The name of the library. * @param {string} [author] Optional author of the library. * @param {string} [version] Optional version of the library. */ constructor(name, author, version) { this.name = name; // optional properties this.author = author; this.version = version; } /** * Registers a component in the library. * @param {ComponentConstructor} component The component to register. * @param {string} [name] Optional name for the component. If not provided, uses the component's name property. */ add(component, name) { const key = typeof name === "string" ? name : component.name; if (this.components[key]) { console.warn(new Error(`Component with name ${key} already exists.`)); } this.components[key] = component; } /** * Registers a library. * @import {Library} from "./Library.js"; * @param {Library} library The library to register. * @param {string[]} exclude Optional array of component names to exclude from the library registration. */ register(library, ...exclude) { if (this.libs[library.name]) { console.warn(new Error(`Library with name ${library.name} already exists in ${this.name}.`)); } const components = library.list().filter(com => !(com.name in exclude)); this.libs[library.name] = { name: library.name, version: library.version, author: library.author, components: components.map(com => com.name), dependencies: library.libs }; for (const com of components) { this.add(com.component, `${library.name}-${com.name}`); } } /** * Retrieves a component by its name. * @param {string} name The name of the component to retrieve. * @return {ComponentConstructor | undefined} The component associated with the name, or undefined if it does not exist. */ get(name) { return this.components[name]; } /** * Returns a library. * @param {string} name The name of the library. * @return {LibMeta} */ lib(name) { return this.libs[name]; } /** * Lists all registered components in the library. * @return {Array<{ component: ComponentConstructor, name: string }>} An array of objects containing component constructors and their names. */ list() { return Object.entries(this.components).map(([name, component]) => ({ component, name })); } }