# How It Works

Interlace is server-driven: your state lives in Python, the server renders HTML, and the client morphs
only the parts of the DOM that changed. No page reload, no client-side state, just a round-trip to the
server and a surgical DOM update.

## The request lifecycle

One interaction is one round-trip. The client sends the current state up, the server runs your
component and renders, and the client morphs the response back in. Click the counter to watch it flow:

```interlace-demo
LifecycleCounter
```

The component is a **Django class-based view** (it subclasses `TemplateView`), so "the server runs your
component" is ordinary Django: instantiate the view, restore its state from the snapshot, call the
method, render its template. No new runtime, no separate API.

```python
class Counter(Component):        # a Django TemplateView subclass
    count: int = 0

    def increment(self):
        self.count += 1

    def inline_template(self):
        return """
        <div>
            <p>Count: {{ count }}</p>
            <button lace:click="increment">+</button>
        </div>
        """
```

Clicking **+** POSTs `{"method": "increment", "snapshot": {...}}` to `/interlace/message`. The server
rebuilds the `Counter` from that snapshot, runs `increment()` (`count` becomes `1`), re-renders, and
returns the HTML plus the new snapshot. The client morphs `<p>Count: 0</p>` into `<p>Count: 1</p>`,
leaving the button (and its focus) untouched.

## Server-driven vs client-driven

| Aspect | Interlace (Server-Driven) | React/Vue (Client-Driven) |
|---|---|---|
| State location | Python server | JavaScript browser |
| Rendering | Django templates on server | JSX/Templates in browser |
| Updates | Server renders → DOM morphs | Virtual DOM diffing |
| Business logic | Python methods | JavaScript functions |
| Network | Request per interaction | API calls as needed |

## DOM morphing

Interlace uses Alpine's `morph` plugin to efficiently update the DOM:

```html
<!-- Before (count = 0) -->
<div lace:id="abc123">
    <p>Count: 0</p>
    <button>+</button>
</div>

<!-- Server sends new HTML (count = 1) -->
<div lace:id="abc123">
    <p>Count: 1</p>
    <button>+</button>
</div>

<!-- Alpine morph updates ONLY the text node -->
<!-- No full re-render, button element preserved -->
```

This preserves focus, scroll position, and CSS transitions. Only changed content updates.

## State snapshots

Each component maintains a state snapshot sent with every request:

```json
{
  "state": { "count": 1 },
  "memo": { "id": "abc123", "name": "Counter" },
  "checksum": "a3f2b9..."
}
```

- **state:** the component's public property values
- **memo:** component metadata (id, name, ...)
- **checksum:** a hash of state + memo + your `SECRET_KEY`, so the server can reject a tampered snapshot

## Why server-driven?

**Benefits**

- Write Python, not JavaScript
- Direct database access in components
- No API layer needed
- Server-side validation & security
- Simpler mental model

**Trade-offs**

- Requires server round-trip per interaction
- Not suitable for offline apps
- Network latency affects UX
- More server load than static SPAs

> Familiar with Laravel Livewire or Phoenix LiveView? Interlace is the same server-driven approach,
> implemented for Django.
