Skip to content

fix(initializeAttrs): allow manually initializing additional attrs after @controller - #379

Open
Ultroncn wants to merge 2 commits into
github:mainfrom
Ultroncn:fix/initialize-attrs-manual-names
Open

Ultroncn wants to merge 2 commits into
github:mainfrom
Ultroncn:fix/initialize-attrs-manual-names

Conversation

@Ultroncn

Copy link
Copy Markdown

Closes #207

@controller calls initializeAttrs for the @attr-decorated names as soon as the element connects. A later call with additional names — for example, from the element's own connectedCallback, the documented pattern for attrs without the @attr decorator (see docs/_guide/attrs.md, "What about without Decorators?") — was silently ignored, because re-initialization was guarded by a single per-element flag. Those properties were never bound to their data-* attributes and kept their field defaults (in the issue's example, open stayed true despite data-open="false").

This changes the guard to track initialized names per element instead of a single flag, so a later call initializes just the names that have not been initialized yet. Names that were already initialized are left alone, which keeps the guard that attributeChangedCallback relies on (it calls initializeAttrs on every attribute change). Names are marked before their descriptor is defined so a re-entrant call — for example, one triggered by reflecting the default value back to an attribute — cannot define the same name twice.

Testing

  • Added three regression tests in test/attr.ts ("manual initialization"): a manually initialized attr reads its data-* attribute value, reflects its default value when no attribute is present, and repeated calls do not re-initialize it.
  • npx web-test-runner test/attr.ts: 15 passed (12 existing + 3 new).
  • npx eslint src/attr.ts test/attr.ts and npx tsc --noEmit: clean.

…ter @controller

Closes github#207

initializeAttrs guarded re-initialization with a per-element flag, so
once @controller had initialized the @attr-decorated names, a later
call with additional names — for example, from the element's own
connectedCallback, the documented pattern for attrs without the
@attr decorator — was silently ignored and those properties were
never bound to their data-* attributes.

Track initialized names per element instead of a single flag: a later
call now initializes just the names that have not been initialized
yet, while names that were already initialized are left alone (which
keeps the re-initialization guard that attributeChangedCallback
relies on). Names are marked before their descriptor is defined so a
re-entrant call cannot define the same name twice.
@Ultroncn
Ultroncn requested a review from a team as a code owner November 17, 2026 03:21
Copilot AI adaptive review requested due to automatic review settings November 17, 2026 03:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Reflecting missing decorated defaults re-enters initialization through attributeChangedCallback, causing repeated scans of all decorated names and quadratic initialization work.

1 open issue
What changed in this PR

This PR addresses #207 by allowing manual attribute initialization after @controller has initialized decorated properties.

Changes:

  • Tracks initialized property names per element so later calls can add bindings.
  • Marks names before defining descriptors to prevent duplicate initialization during re-entrant calls.
  • Adds regression tests for manual binding, default reflection, and repeated calls.
File Description
test/​attr.ts Adds three tests for manual initialization after @controller.
src/​attr.ts Replaces the per-element guard with per-name tracking and documents incremental initialization.

Review effort: Balanced (adaptive)

Comment thread src/attr.ts Outdated
Comment on lines +59 to +60
if (initialized.has(key)) continue
initialized.add(key)
@Ultroncn

Ultroncn commented Nov 17, 2026 via email

Copy link
Copy Markdown
Author

… done

Reflecting a missing decorated default back to an attribute
synchronously invokes attributeChangedCallback, which calls
initializeAttrs again with no names. Each re-entrant call rescanned
every decorated name, making initialization quadratic with nested
calls, and every later observed mutation rescanned them too.

Track implicit initialization per element (in-progress/completed) and
short-circuit implicit calls on either state, so only the outermost
call scans the decorated names. Explicit calls are never
short-circuited and still initialize the names they are given. If the
loop throws part-way, the state is dropped so a later implicit call
retries the remaining names.

This branch has not been deployed

No deployments
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.

Attrs initialization without @attr decorator

2 participants