Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[feat]: slotController synchronous `hostUpdated()`

オープン
#3,103 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
静か
技術スタック
typescript
領域
frontend

調査の方向性

Start by locating the slotController implementation and the SlotControllerPublicAPI declaration, then trace hostConnected, #initSlotMap, the MutationObserver, and the hasSlotted(), isEmpty(), and getSlotted() methods. Implement the synchronous lifecycle described in the issue and verify initial connection, reconnection, and child mutations produce correct slot state without an async timing gap.

索引モデルが issue の本文から書いたものです。

説明

Alternative approach:

synchronous hostUpdated() instead of async fallback

The current fix adds a fallback in getSlotted() to query slot elements directly when #slotRecords hasn't been populated yet. This addresses the symptom but leaves the root cause: hostConnected and #initSlotMap are async, creating a timing gap where no data source exists.

The gap also affects hasSlotted() and isEmpty(), which aren't addressed here.

Why hostConnected is async

Both hostConnected and #initSlotMap await host.updateComplete just to ensure the shadow DOM exists so they can query for elements. But Lit already provides a synchronous hook that guarantees this: hostUpdated(), which runs after every render when the DOM has been committed.

Suggested change

Make hostConnected synchronous (just sets up the MutationObserver), move record initialization to hostUpdated(), and have the MutationObserver trigger a re-render instead of calling #initSlotMap directly:

  hostConnected(): void {
    this.#mo.observe(this.host, { childList: true });
    this.#slotRecords.clear();
  }

  hostUpdated(): void {
    if (this.#slotRecords.size === 0) {
      for (let slotName of this.#slotNames.concat(Object.values(this.#deprecations))) {
        slotName ||= SlotController.default;
        const slot = this.#getSlotElement(slotName);
        if (slot) {
          this.#slotRecords.set(slotName, new SlotRecord(slot, slotName, this.host));
        }
      }
      if (this.#slotRecords.size > 0) {
        this.host.requestUpdate();
      }
    }
  }

The MutationObserver callback becomes:

#mo = new MutationObserver(() => this.host.requestUpdate());

And #initSlotMap can be removed entirely.

This also requires updating the SlotControllerPublicAPI declaration to match:

hostConnected?(): void;

Why this works

  • First render: Records empty, isEmpty() returns true (matches SSR server behavior). hostUpdated() populates records and calls requestUpdate().
  • Second render: Records exist, isEmpty()/getSlotted() read live DOM via SlotRecord getters, correct results. hostUpdated() sees records exist, no requestUpdate(), no infinite loop.
  • MutationObserver: Child changes trigger requestUpdate() → re-render → SlotRecord getters reflect new DOM state. No record rebuild needed since they query live DOM.
  • Reconnection: hostConnected clears records → next render → hostUpdated() repopulates.

What this fixes that the current PR doesn't

  • hasSlotted() and isEmpty() have the same timing gap, they also read from #slotRecords and return incorrect results before async init completes. The hostUpdated() approach fixes all three methods.
  • Eliminates the async architecture that caused the issue rather than working around it.

Trade-off

The two-render pattern on initial connection is inherent <slot> elements are created by render(), so the first render can't know about them. This was already the case with the async approach.

We can spin this off to another issue if we feel the need

Originally posted by @zeroedin in https://github.com/patternfly/patternfly-elements/issues/3093#issuecomment-4252752632

主要言語
TypeScript
スター
394
フォーク
107
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

patternfly/patternfly-elements のほかの issue

patternfly/patternfly-elements の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。