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

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

メンテナーはふだん 1 日以内に返信

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
frontend

調査の方向性

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.

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

説明

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
主要言語
JavaScript
スター
30
フォーク
5
平均マージ
1時間 57分
マージ済み PR(30日)
10

環境構築

はじめの一歩

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

webtides/element-js のほかの issue

webtides/element-js の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

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

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