[feat]: slotController synchronous `hostUpdated()`
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 55/100
- Type d'issue
- Fonctionnalité
- Clarté
- Plutôt claire
- Activité
- Calme
- Stack technique
- typescript
- Domaine
- frontend
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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, norequestUpdate(), 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:
hostConnectedclears records → next render →hostUpdated()repopulates.
What this fixes that the current PR doesn't
hasSlotted()andisEmpty()have the same timing gap, they also read from #slotRecords and return incorrect results before async init completes. ThehostUpdated()approach fixes all three methods.- Eliminates the
asyncarchitecture 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
- Langage dominant
- TypeScript
- Étoiles
- 394
- Forks
- 107
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de patternfly/patternfly-elements
-
bug
patternfly/patternfly-elements#3158 · 1 personne assignée ·
-
patternfly/patternfly-elements#3136 · 1 commentaire · 1 personne assignée ·
-
docs
patternfly/patternfly-elements#3131 · 1 personne assignée ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 55/100
patternfly/patternfly-elements#3122 ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 38/100
patternfly/patternfly-elements#3118 ·
Toutes les issues de patternfly/patternfly-elements
Issues similaires
-
bug(cli): hapi doctor inline-media prints a fabricated B:\ helper-script path in packaged installs Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
-
Crush Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
catppuccin/catppuccin#3125 ·
-
Add a SECURITY.md Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
ElementsProject/cln-application#167 · 1 commentaire · 1 réaction ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Quantco/pnpm-licenses#17 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100