@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
69 lines • 2.73 kB
JavaScript
import { subscriptionRegistry } from "../../state/core/subscription-registry.js";
export function createJayJsElementClass(tagName) {
if (!/^[a-z][a-z0-9-]*$/.test(tagName)) {
throw new Error(`Nome de elemento inválido: ${tagName}`);
}
const baseElement = document.createElement(tagName);
const BaseClass = baseElement.constructor;
class JayJsElement extends BaseClass {
connectedCallback() {
if (typeof this.onmount === "function") {
const result = this.onmount(this);
if (result instanceof Promise) {
result
.then((cleanup) => {
if (typeof cleanup === "function") {
this._cleanupFromMount = cleanup;
}
})
.catch((error) => {
console.error("JayJS: Error in async onmount:", error);
});
}
else if (typeof result === "function") {
this._cleanupFromMount = result;
}
}
}
disconnectedCallback() {
// Cleanup subscriptions FIRST to prevent state updates after unmount
if (subscriptionRegistry.hasSubscriptions(this)) {
subscriptionRegistry.cleanupElement(this);
}
if (this._ref) {
this._ref.current = null;
this._ref = undefined;
}
if (typeof this._cleanupFromMount === "function") {
try {
this._cleanupFromMount();
}
catch (error) {
console.error("JayJS: Error in onmount cleanup:", error);
}
this._cleanupFromMount = undefined;
}
if (typeof this.onunmount === "function") {
try {
const result = this.onunmount(this);
if (result instanceof Promise) {
result.catch((error) => {
console.error("JayJS: Error in async onunmount:", error);
});
}
}
catch (error) {
console.error("JayJS: Error in onunmount:", error);
}
}
}
}
return JayJsElement;
}
export function registerJayJsElement(tagName) {
if (!customElements.get(`jayjs-${tagName}`)) {
const ElementClass = createJayJsElementClass(tagName);
customElements.define(`jayjs-${tagName}`, ElementClass, { extends: tagName });
}
}
//# sourceMappingURL=jay-js-element.js.map