[RENDERER]: keep template instances across switches with a cache() directive

Abierto
#190 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
javascript
Área
frontend

Línea de trabajo

The work sits in the child node part and TemplatePart that #189 is reworking, so start there and read how the part tracks its markers and fragment, plus the existing unsafeHTML() directive as the model for a value wrapper. Add a cache() directive whose part keeps a Map from template strings to TemplatePart instances and moves nodes into the fragment instead of removing them. Done when toggling two templates keeps the same edit-form instance and its typed input value, template to null to template reuses the cached instance, and toString() output matches with and without cache(). Depends on #189 landing first.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement

Goal

A child node value that switches between templates, or between a template and something else, can opt into keeping the DOM of the template that went away. When it comes back the same nodes are reinserted, so custom elements keep their instance and state that lives only in the DOM survives: input values, an opened <details>, scroll position.

Context

Since #189 a child node part that switches from a template to another value and back renders the template fresh. 1.3 kept the TemplatePart and reinserted its nodes. Keeping every template that went away is the wrong default: it holds the DOM in memory for the lifetime of the part, and most switches do not need it. Lit has the same default and offers cache() as the opt-in.

Lists are not in scope, keyed lists are #186.

Design

html`<section>${cache(this.editing ? html`<edit-form .item=${this.item}></edit-form>` : html`<p>${this.item.name}</p>`)}</section>`
  • cache() returns a plain object that carries the value, like unsafeHTML(), and the child node part recognises it
  • the part keeps a Map<strings, TemplatePart> with one instance per literal it has rendered
  • a template that goes away has its nodes moved from between its markers into its fragment instead of being removed, TemplatePart.insertBefore() already inserts the fragment
  • a template that comes back takes its cached instance, gets updated with the new values and is inserted
  • a non template value renders as usual, the cached instances stay in the map
  • the map is released with the part
  • toString() renders the value as if there were no cache(), hydration binds the first value as usual

Builds on the range model of #189, do it after that one.

Done when

  • toggling between two templates keeps the same <edit-form> instance and the value typed into an input inside it
  • template → null → template reuses the cached instance
  • toString() output is the same with and without cache()
  • documented in docs/features/templates.md next to the other directives
Lenguaje dominante
JavaScript
Estrellas
30
Forks
5
Merge medio
1 h 57 min
PR fusionados (30 d)
10

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de webtides/element-js

Todos los issues de webtides/element-js

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.