move static define() block to end of class body#39
Merged
Conversation
`customElements.define` synchronously upgrades any pre-existing matching
elements in the DOM, which runs the constructor. The constructor reads
static metadata (`assignableAttributes`, `events`, `bindMethods`) and
may also touch class-level singletons like `static observer = new
ResizeObserver(...)`. Calling `define()` mid-class-body — e.g. right
after `static tagName = ...` as we did everywhere — let that upgrade
fire before the rest of the static field initializers had run, leaving
the upgraded instance with empty `assignableAttributes` and risking
errors on missing singletons.
Move every `static { this.define() }` block to be the last static in
its class. Also document the requirement on `KompElement.define`'s
JSDoc and update `FormField`'s example. Drops a leftover empty
`if (!window.customElements.get('komp-floater'))` block in floater.js.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Follow-up to #37. Even with the cache-invalidation fix in
scanPrototypesFor, the first instance constructed by an upgrade-during-class-init still ends up with empty static metadata — the cache no longer poisons subsequent instances, but that one early instance is broken. This change eliminates the timing window entirely.customElements.definesynchronously upgrades any pre-existing matching elements in the DOM, which runs the constructor. The constructor readsassignableAttributes,events,bindMethods, etc., and several components also touch class-level singletons likestatic observer = new ResizeObserver(...). Callingdefine()mid-class-body — as we did everywhere, right afterstatic tagName = ...— let that upgrade fire before the rest of the static field initializers had run.Move every
static { this.define() }block to be the last static in its class so registration always happens after the class is fully initialized. Document the requirement onKompElement.define's JSDoc and update theFormFieldexample.Verified with a smoke test: parsing
<komp-dropzone enabled>into the DOM and then importing the module now correctly resolves all three keys ofassignableAttributes(enabled,dragHereOverlay,dragOverOverlay) on the upgraded instance.Drops a leftover empty
if (!window.customElements.get('komp-floater')) {}block at the bottom offloater.js(looks like a leftover from a previous registration refactor).Test plan
npm test— 33 passingnpm run docsbuilds without warnings<komp-dropzone>resolves fullassignableAttributesafter import🤖 Generated with Claude Code