Skip to content

Lifecycle ​

Every Jadis component follows the Custom Elements lifecycle. Jadis exposes the most important hooks as protected methods you can override.

Lifecycle Hooks ​

onConnect() ​

Called on the task after the component is connected, once its template has been rendered. This is the place to set up event listeners, fetch data, or perform connection-specific setup. It runs again each time the component is connected again after it was removed, on the same DOM: make it safe to repeat, and guard one-time work (filling a list, a first fetch) by what the DOM already holds. Moving the component is not removing it: see Moves.

typescript
class MyComponent extends Jadis {
  onConnect(): void {
    // Component is now in the DOM — safe to interact with it
    console.log('Component connected');
  }
}

Waiting for onConnect() ​

Code outside a component cannot see its onConnect run: a parent that calls a method of a child it just drew, or a test that clicks a button, may come before the listeners are wired. Two promises tell when they are.

A component's connected resolves once the onConnect of its connection has returned:

typescript
const input = createElement(SearchInput, {}, this.shadowRoot);
await input.connected;
input.focus();

It is resolved already when that is done. Before the first connection, and after a removal, it waits for the next connection; a component removed before its onConnect ran resolves it without calling onConnect.

Jadis.settled() resolves once no component has an onConnect left to call, those an onConnect connected included, and once the router has called the onRoute of a page it just showed. One wait covers a page just drawn, which is what a test needs:

typescript
document.body.append(page);
await Jadis.settled();
page.shadowRoot?.querySelector('button')?.click();

Neither waits for what a component starts later, such as a request: wait for that by what it draws.

onDisconnect() ​

Called a microtask after the component is removed from the DOM. Use this for cleanup that is not tied to killSignal, such as canceling timers or stopping external work. Listeners registered through Jadis helpers are cleaned up automatically.

typescript
class MyComponent extends Jadis {
  private _intervalId?: number;

  onConnect(): void {
    this._intervalId = window.setInterval(() => {
      console.log('ticking...');
    }, 1000);
  }

  onDisconnect(): void {
    clearInterval(this._intervalId);
    console.log('Component disconnected');
  }
}

Connection State ​

You can check whether a component is currently connected to the DOM using the isConnected getter. It matters after an await: the component may have been removed meanwhile. Pass killSignal to fetch, so a removed component stops waiting:

tsx
class ItemList extends Jadis {
  static readonly selector = 'item-list';

  templateHtml(): Node {
    return <ul></ul>;
  }

  async onConnect(): Promise<void> {
    const response = await fetch('/api/items', { signal: this.killSignal });
    const items: string[] = await response.json();
    if (!this.isConnected) {
      return;
    }
    this.getElement('ul').replaceChildren(...items.map((item) => <li>{item}</li>));
  }
}

Lifecycle Timeline ​

Constructor → connectedCallback (render template) → onConnect → ... → disconnectedCallback → onDisconnect
  1. Constructor — Runs when the class is instantiated. Shadow DOM is attached here if useShadowDom is true.
  2. connectedCallback() — Runs when the component is connected. On the first connection it renders templateHtml() and templateCss(); the rendered DOM is reused on reconnection. It then applies the useChange values set before the connection, then the attribute callbacks, all synchronously, before scheduling onConnect().
  3. onConnect() — Runs asynchronously after the component is connected. It runs again after each reconnection. A component removed before that task gets no onConnect() for that connection: the next connection schedules its own.
  4. Active — The component is in the DOM and responding to user interaction.
  5. disconnectedCallback() / onDisconnect() — A microtask after the component is removed, killSignal is aborted and onDisconnect() runs, unless the component was put back meanwhile: it was moved.

Moves ​

Moving a component keeps it as it is, whether it is moved with append(), insertBefore(), replaceChildren() or moveBefore(): its listeners, its killSignal and its state go through the move, and neither onDisconnect() nor onConnect() runs. A list can re-order the rows it keeps:

typescript
this._refs.list.replaceChildren(...todos.map((todo) => this._rows.get(todo.id) ?? this.createRow(todo)));

The browser disconnects a moved element and connects it again in the same call: Jadis waits a microtask before disconnecting, and a connection meanwhile is a move. A component removed and put back later, in another task, was disconnected: it connects again, and onConnect() runs.

Important Notes ​

  • Templates render only on the first connection; reconnection reuses the existing DOM.
  • onConnect() runs on a later task, not synchronously inside appendChild(). The template and the values set before the connection are already in place when appendChild() returns.
  • The killSignal is automatically aborted on disconnect, a microtask after the removal, canceling listeners registered via this.on(), useEvents(), and onBus().
  • Avoid heavy work in the constructor. Defer connection-specific work to onConnect().

See Also ​