Skip to content

Give a page script's widget view one lifecycle helper, whileInterfaceConnected - #4004

Merged
max-sixty merged 1 commit into
mainfrom
playground-view-lifecycle
Oct 25, 2026
Merged

max-sixty merged 1 commit into
mainfrom
playground-view-lifecycle

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A playground's page script repeated about 15 lines of lifecycle in four examples (alert-thresholds, code-comparison, data-explorer, notification-playground) and in the playground's author instructions, which #3952 made show the script whole: wait for PAGE_INTERFACE and hold presentation with present(), declare slices and the instruction provider once per lifetime behind a flag, subscribe on each connection, reconnect at once, and ignore the PAGE_INTERFACE a revision applied in place dispatches again. This gives that lifecycle one owner and cuts every copy over to it.

whileInterfaceConnected(playground, {
  declare: () => playground.registerInstructionProvider(instruction),
  connect: () => playground.subscribe(paint),
});

declare() runs once, before the first connect(signal); the first connection waits for PAGE_INTERFACE and holds presentation until connect has run, a reconnection runs it at once, and the cleanup connect returns runs as the element leaves.

Where it lives. The brief asked for a helper in the playground package, but a page script can't reach one: revision_artifact.resolve_dependency admits only /page/ modules and PUBLIC_MODULES (widget-api.js and Floating UI) from authored code, and a method on lf-playground can't be called before the widget upgrades, which is the wait the helper exists to hide. The helper is generic instead: it names no widget, so it lives in the runtime beside PAGE_INTERFACE (runtime/presentation.js) and is exported from widget-api.js. module-authoring.md, "Startup and presentation", documents it, and the playground's author instructions show the short form. Making package modules importable from pages would be a new public-module mechanism, which this doesn't need.

No protocol bump. The change adds a widget-api.js export and removes or changes none; layer-registry.md, "Page protocol", keeps the number for additive public APIs, and pages vendored earlier keep running their long form against the same playground methods.

tests/runtime/interface-connected.test.mjs covers the lifecycle: one declaration, connection at the page interface, none again on a later one, cleanup on departure, an immediate reconnection, and a view that left before the page interface. Removing the "already connected" guard fails it.

Checks: pnpm run test:runtime (561 pass), pnpm run check:browser, pre-commit; tests/browser/widgets/test_playgrounds.py -k "control_written_in_the_preview or rail_stays_in_view or short_workspace_shows_more or code_reader_design_comment or notification_playground or undoing_a_playground_choice" (7 pass); leaf-dev probe on the four examples shows each provider's instruction, the contributors' paint and no console errors. Nothing visible changes, so no stills or preview refresh.

This was written by Claude Code on behalf of max-sixty

…Connected

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit cbef002 into main Oct 25, 2026
4 checks passed
@max-sixty
max-sixty deleted the playground-view-lifecycle branch October 25, 2026 18:46
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