Skip to content

Fix onunload #3

@Eggsecuter

Description

@Eggsecuter

Problem

Onunload does not get called when a component gets reloaded or updated.
This is crucial as connections or event subscribers can't be closed properly.

Quick fixed it.

Don't know if this is properly implemented but after testing it a bit, I'm confident it works in any case.

Component.prototype.reload = async function () {
	await this.unload(); <-------------- !!!
	await this.onload();

		if (this.child) {
			await this.child.reload();
		}

	await this.update();
}

Component.prototype['addToElement'] = function addToElement(item, element: Node) {
	if (item instanceof Node) {
		element.appendChild(item);
	} else if (Array.isArray(item)) {
		for (let child of item) {
			this.addToElement(child, element);
		}
	} else if (item instanceof Component) {
		const placeholder = item.renderLoader();

		element.appendChild(placeholder);

		item.parent = this;

		(async () => {
			await item.onunload(); <-------------- !!!
			await item.onload();

			const child = item.render();
			item.rootNode = child;

			element.replaceChild(child, placeholder);
		})();
	} else if (item !== false && item !== undefined && item !== null) {
		element.appendChild(document.createTextNode(item));
	}
}

Component.prototype.host = async function host(parent: Node) {
	await this.onunload(); <-------------- !!!
	await this.onload();

	const root = this.render();
	this.rootNode = root;
	this.parent = null;

	parent.appendChild(root);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions