Repository navigation
Conversation
A LitElement hears when it leaves the document, and a HookComponent disposes what useEffectOnce returned. A template rendered with Lit.render into a plain element hears nothing when other code removes that element, so timers and subscriptions started inside it keep running. lit itself only tells directives about parts it clears; it never watches the document. Lit.renderPart returns lit's root part, which the binding used to throw away. part.setConnected false reaches every AsyncDirective in the tree. Lit.render still returns unit, so existing call sites are unchanged. Lit.trackConnection tag turns the tag into a custom element whose only behaviour is to forward its connected and disconnected callbacks to the root part inside it: the same mechanism LitElement uses. Directives pause when it leaves the document and resume when it comes back, so a moved element keeps what it had. The overload with a handler runs setup when an element with the tag joins the document and disposes what it returned when the element leaves. It returns an IDisposable that stops that handler, so it can sit in a hook. Elements already in the document when a handler subscribes are reported to it straight away: defining a tag upgrades elements already in the page on the spot, before the handler is registered, so server-rendered elements would otherwise never be reported. Five tests in DisconnectTest.fs. Removing the forwarding fails one; removing the report of elements already present fails two. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gives markup rendered into a plain element a way to hear that it left the page.
A
LitElementgetsdisconnectedCallback, and aHookComponentdisposes whatHook.useEffectOncereturned. But a template rendered withLit.renderinto a plain<div>hears nothing when other code removes that div, so timers, sockets and subscriptions started inside it keep running. lit tells directives when it clears a part, but it never watches the document.What's added
Lit.renderPart el template: renders likeLit.renderand returns lit's root part, which the binding used to throw away.part.setConnected falsereaches everyAsyncDirectivein the tree.Lit.renderstill returnsunit, so existing call sites don't change.Lit.trackConnection "my-island": turns the tag into a custom element whose only behaviour is to forward its connected and disconnected callbacks to the root part inside it. That's the same mechanismLitElementuses. Directives pause when the element leaves the document and resume when it returns, so a moved element keeps what it had.Lit.trackConnection("my-island", fun el -> ...): also runs your own setup while the element is in the document:IDisposablethat stops the handler and disposes whatever is still set up. The forwarding and any other handlers carry on (a custom element can't be undefined).Docs: a "Leaving the page" section in
docsrc/docs/templates.md. Its examples compile.One signature change
LitBindings.rendernow returnsChildPartinstead ofunit, because that's what lit'srenderreturns. Code that calls the raw binding as a statement gets the usual "implicitly ignored" warning.Lit.renderis unchanged.Tests
Five in a new
test/DisconnectTest.fs:renderPart's part reaches the directives in the tree;Mutation check: removing the forwarding fails the third test, and removing the report of elements already present fails the last two.
dotnet build Fable.Lit.slnandnpm testpass (42 tests).🤖 Generated with Claude Code