[UPDATES]: one update cycle for elements and stores, hooks and watchers run once per batch

Abierto
#191 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
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
javascript
Área
frontend

Línea de trabajo

Start with requestUpdate() and Store.requestUpdate(), then inspect test/unit/updates.test.js and the related store tests to map current scheduling and watcher behavior. The design calls for one shared microtask cycle across elements and stores, covering connection handling, observers, and server rendering. Done means the listed update and store behaviors pass, the element-js-ssr-renderer suite stays green, and docs/features/watch.md and docs/features/store.md describe the cycle and hook arguments.

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

Descripción

enhancement

Goal

A property set schedules one update cycle. Any number of sets on an element or a store in the same tick end in one render, one beforeUpdate, one afterUpdate and one watcher call per changed property. Elements and stores share the cycle: a store notifies each observer once per tick, and a property that a watcher derives from another one lands in the same render.

Context

  • requestUpdate() triggers beforeUpdate on every call and reschedules the requestAnimationFrame. Two property sets call the hook twice for one render. The batching test in test/unit/updates.test.js is commented out because of this.
  • Watchers run in the .finally() of each request's promise, after the render. A property that a watcher sets renders one frame later. Setting the same property three times in a tick calls its watcher three times, with the intermediate values.
  • The frame callback uses the options of the last request. A requestUpdate({ notify: false }) after a notifying request silences the hooks of the whole batch.
  • disconnectedCallback() cancels the frame and drops the pending resolvers. An awaited requestUpdate() on an element that leaves the DOM never settles.
  • Store.requestUpdate() has no batching. Every set notifies every observer, calls observer.watch() twice, passes (store, store) as new and old value (#108) and dispatches one -changed event per set (#91). Store watchers run synchronously before the update, element watchers after it.
  • requestAnimationFrame pauses in background tabs, an awaited update hangs there.

Design

One scheduler for BaseElement and Store, microtask based:

// in the property setter
this._changedProperties ??= new Map();
if (!this._changedProperties.has(property)) this._changedProperties.set(property, oldValue);
this._state[property] = newValue;
this._updatePromise ??= Promise.resolve().then(() => this.performUpdate());

performUpdate() {
    const changed = this._changedProperties;
    this.beforeUpdate(changed);
    for (const [property, oldValue] of changed) this.watch()[property]?.(this[property], oldValue);
    this.update();
    this.afterUpdate(changed);
    // notify events, resolve updateComplete
}
  • changedProperties is a Map<property, oldValue>. The first old value of a tick wins, the last new value is on the instance. beforeUpdate and afterUpdate receive the map as an optional argument, watch() keeps its (newValue, oldValue) signature.
  • The first set of a tick queues performUpdate() as a microtask. requestUpdate() queues the same cycle with no changed property and returns updateComplete, so the current API keeps working.
  • Watchers run before the render, once per changed property. A set made in beforeUpdate or in a watcher joins the running cycle, its watcher runs in the same loop, and the render sees the derived value. A set made in afterUpdate starts a new cycle.
  • updateComplete resolves after the cycle. disconnectedCallback() lets a pending cycle settle, nothing dangles.
  • The first cycle after connect runs connected() and afterUpdate() without beforeUpdate, as it does today. The notify option goes away in favor of this first-cycle rule.
  • performUpdate() returns early for an element that is not connected and keeps the changed properties, the connect runs the cycle. This also covers the server: the dom-shim of element-js-ssr-renderer never connects an element, so property sets during a server render schedule nothing, as the no-op requestAnimationFrame does today.
  • Store runs the same scheduler. A set marks the key as changed and queues one microtask. performUpdate() runs the store's own watchers once per key, then notifies each observer once: an element gets requestUpdate() and its watcher for the store property receives (store, previousState), with previousState a plain object of the state before the tick. A function observer receives the changed map. A parent store marks the nested store's key as changed and joins the same tick. Replacing a nested store unsubscribes the old one.
  • Changes to timing and watcher order go into the changelog. The docs promise "next frame", they describe the microtask cycle instead.

Store lifecycle (#111) stays separate, it sits on subscribe and unsubscribe and does not touch the cycle.

Done when

  • the three commented out tests in test/unit/updates.test.js are enabled and pass: the asynchronous update, one render with one beforeUpdate and one afterUpdate for several sets, a property derived in a watcher rendered in the same cycle
  • a watcher runs once per changed property per cycle, with the first old and the last new value of the tick
  • three sets on a store in one tick notify each observer once, element watchers for store properties receive the previous state, closes #91 and #108
  • await el.requestUpdate() resolves in a background tab and on an element that was disconnected in the meantime
  • the test suite of element-js-ssr-renderer stays green on the dom-shim, no cycle runs on the server
  • docs/features/watch.md and docs/features/store.md describe the cycle and the hook arguments
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.