Litewire 1.1.0 ยท Reference

Processing & cleanup

Processing initializes Litewire behavior in a DOM subtree. Cleanup releases Litewire-managed work and component resources when that subtree is removed or the instance is destroyed.

Most pages do not need to call either API manually. Litewire processes the initial document and watches document.body for inserted or removed elements. Use litewire.process(root) when you need to initialize a detached or app-managed subtree immediately; cleanup runs automatically when a connected subtree is actually removed.

What processing does

Processing scans a root element and its descendants for Litewire directives, event bindings, polling triggers, and lw-component modules. Litewire runs this on initial load and after its own HTML swaps. Its mutation observer also scans elements added under the document body.

When you insert a detached subtree

const panel = document.createElement("div");
panel.setAttribute("lw-component", "/widgets/my-panel.js");
panel.setAttribute("lw-trigger", "load");

// Initialize before handing the detached node to another part of the app.
window.litewire.process(panel);
document.querySelector("#panel-host").append(panel);

When another library inserts connected markup

const host = document.querySelector("#third-party-host");
host.insertAdjacentHTML("beforeend", trustedMarkup);

// The observer normally processes additions under document.body.
// Call process when the app needs an immediate, explicit scan.
window.litewire.process(host.lastElementChild);

process(root) scans the supplied root and its descendants; it does not insert the root into the document. Markup inserted under the observed body is normally processed automatically, so avoid redundant calls unless you need immediate setup or are working with a detached subtree.


Live example: process, mount, and clean up

The button creates a detached component, explicitly processes it, and then inserts it. Remove it to see its returned cleanup function stop the timer. This is useful when your app owns DOM creation and teardown.

No component is mounted.

Component module

export default (element) => {
 let seconds = 0;
 const render = () => {
 element.textContent = `Component is active: ${seconds} seconds`;
 };

 render();
 const timer = setInterval(() => {
 seconds += 1;
 render();
 }, 1000);

 return () => {
 clearInterval(timer);
 };
};

Litewire invokes the returned function on removal. Component modules may also return an object with unmount() or destroy().

App-owned mount and unmount

const host = document.querySelector("#processing-demo-host");
const component = document.createElement("div");
component.setAttribute("lw-component", "/widgets/processing-cleanup-demo.js");
component.setAttribute("lw-trigger", "load");

window.litewire.process(component);
host.append(component);

// Later, remove it. Litewire's observer calls the component cleanup.
component.remove();

What cleanup releases

When Litewire detects that a node was removed and is no longer connected to the document, it disposes that node and its descendants.

ResourceCleanup behavior
ComponentCalls the cleanup function returned by the module, or an object's unmount() or destroy() method.
RequestsAborts active requests owned by elements in the removed subtree.
PollingClears polling intervals attached to removed elements.
Element listenersRemoves event listeners registered by Litewire for removed elements.
Moved nodesA node moved elsewhere while still connected is not treated as removed.

The live demo reports when its component cleanup function runs. Litewire handles cleanup for its own directives; custom listeners and timers created by your code still need your own cleanup function.


Abort one request or destroy an instance

Use abort(element) to cancel active requests started by a particular trigger element. Use destroy() only when the entire Litewire instance is being torn down.

const searchForm = document.querySelector("#search-form");
window.litewire.abort(searchForm);

// At application teardown, after the page no longer needs Litewire:
window.litewire.destroy();