Skip to content

Centralize widget connection lifetimes and scheduled paints - #2325

Merged
leaf-agent merged 1 commit into
mainfrom
lit-lifetime
Oct 11, 2026
Merged

leaf-agent merged 1 commit into
mainfrom
lit-lifetime

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Widgets now share one connection lifetime for mechanical resources. whileConnected(owner, start) supplies an abort signal and accepts an optional cleanup function; frameFor(owner, run) coalesces rendering work and cancels it on departure. Resources survive moves within one mutation batch and restart on a later connection.

Adopts the helpers across fourteen default widget modules. Board grab cancellation and sample dialog embedding remain synchronous; ToC resources rebind when its main or scrollport changes. The same tests exposed and now cover a watchOwner enrollment leak when an owner connects and leaves before the first arrival delivery.

Measured source lines for survey item 5: the fourteen widget modules are 5,681 → 5,576 lines. The survey's lifecycle counter reports disconnected callbacks 118 → 8 lines, reconnect branches 39 → 5 lines, and lifetime fields 23 → 12. These counts describe the actual implementation, rather than the survey's unrun sketch. The shared runtime grows by 48 lines; public documentation and regression coverage are additional.

Validation: all 320 runtime tests and 12 focused browser cases pass, covering owner lifetimes, board cleanup and moves, tabs/history, glossary input routes, draft controls and reconnection, chart resize, sample reconnection/Full view, and ToC revisions with retained focus and scroll. Pre-commit checks pass. Independent source and ordinary-input UI reviews found no unresolved issues. Matched feature-gallery startup readings keep 276 code requests and reduce code bytes by 1,427; both have zero initial layout shifts.

This was written by Codex on behalf of max-sixty

Add whileConnected and frameFor and adopt them across the default widgets. Same-batch moves retain mechanical resources; actual departure releases them and later connection restarts them. Keep board grab and sample dialog teardown synchronous.

Enroll synchronously connected watchOwner subscriptions before setup so removal before the first arrival batch still releases resources. Cover this path alongside reconnect, coalescing and cancellation.
@leaf-agent
leaf-agent merged commit c801ee1 into main Oct 11, 2026
6 checks passed
@leaf-agent
leaf-agent deleted the lit-lifetime branch October 11, 2026 02:16
max-sixty added a commit that referenced this pull request Oct 12, 2026
…frameFor the one widget lifetime API (#2503)

Finishes the connection-lifetime helpers from #2325. A widget whose
paint failed no longer comes back to life when it returns to the page.
`whileConnected`/`frameFor` are now the one lifetime API that widgets,
packages, examples and the authoring guide use. `watchOwner` stays
inside the runtime.

## Review item 23: a failed widget stays failed when it returns

- **lf-chart.** When a redraw failed, the chart disconnected its size
observer but left the `whileConnected` subscription armed. A chart that
left the page and came back built a new observer, painted into the
drawing the failure had replaced, and reported the error again. The
catch now calls the `stop` that `whileConnected` returns, so the failure
box stays.
- **lf-diff, same shape.** On return, an inline patch was re-rendered
whenever `lf-rendered` was unset, and a failure clears that class. A
refused patch therefore rendered and reported again. `render` now marks
the work `settled` when it completes or fails. A return renders again
only when the departure cut a rendering short.
- **lf-playground, same shape.** A playground whose build failed
re-registered its reading regions on return. The conversion below
registers them only for a playground that built.
- **`failSoft` deliberately does not end a widget's lifetimes.**
lf-text-document, bound lf-diff and lf-visual-review recover from a
failure when a later good source arrives. That recovery arrives through
their data and thread subscriptions, so the widget that knows its
failure is final stops its own lifetime.

## Review item 28: one lifetime API

- **Package widgets converted:**
- lf-trace, lf-trace-compare, the recording timeline and
lf-visual-review: their scheduled frames, size observers, listeners and
reading regions moved off hand-written frames, `cancelRender` and
`disconnectedCallback`.
- lf-diff, lf-file-editor and lf-text-document: moved off `watchOwner`.
  - lf-playground: moved off `disconnectedCallback`.
- **Visible behaviour change.** A move within one mutation batch now
keeps these resources (a playing recording keeps playing) instead of
tearing them down and rebuilding them.
- **Synchronous teardown that stays.** Two package widgets keep a
`disconnectedCallback`, each with a comment at the site saying why:
lf-board cancels a grab, and lf-sample closes a modal Full view.
- **Examples and the fixture** now teach the same pattern:
code-comparison, notification-playground, the conversation workspace and
margin-entry gallery, the sort and merge films, the atlas widgets, the
PR brief, and lf-test-worker.
- **`watchOwner`.** Dropped from `widget-api.js`; data, projection
watching and widget controllers still use it.
- **Guidance.**
- `module-authoring.md` gains a "Connection lifetimes" section with an
example.
- Its checklist, reading-region, Public API and contribution-example
passages now use `whileConnected`/`frameFor`, keeping
`disconnectedCallback` for synchronous teardown only.
- The `arrivals.js` header now describes `watchArrivals`' returned
`offer` and the owner lifetime separately.

## Measured

| | hand-written source (excl. tests, vendored, Markdown, generated
corpus) | tests |
|---|---|---|
| Item 23 (lf-chart stop, lf-diff `settled`) | +5 | +64 (two new browser
tests) |
| Item 28 (packages and runtime −67, examples −17) | −84 | 0
(lf-test-worker fixture converted) |

## Validation

- **New tests.**
`test_a_chart_that_fails_on_a_redraw_stays_failed_when_it_returns` and
`test_an_inline_patch_that_failed_stays_failed_when_its_diff_returns`
each failed on `main` and pass here.
- **Focused checks, passing:**
  - `tests/runtime/arrivals.test.mjs`;
- 29 focused browser test runs: chart resize, code comparison,
playground state and settlement, notification playground, bounded text
document, merge-film and sort-film controls, roster clock, visual
review, PR brief disconnect, file editor, trace comparison, transport,
zoom and hidden recording, diff thread lifecycle and chunk retry, and
the conversation workspace's primary arrival.
- **Independent review.** An independent reviewer found no regressions
in the conversions, and also found the lf-diff case above.

## Follow-up

Bound widgets that fail still re-fail on every return. `data.js`
documents that it delivers the newest snapshot on return "even if its
revision is unchanged", so a failed bound source runs again and logs a
second console error; the event log dedupes it. The rule that fixes this
is the one lf-diff now applies locally: deliver again on return only
when the departure abandoned a paint in flight. It changes every bound
widget's return path, so it is left for its own change.

> _This was written by Claude Code on behalf of max-sixty_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants