reconcile: support "merge" in objects

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

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

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

評価

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

調査の方向性

まず reconcile と applyState の実装を読み、次に setState がオブジェクトの更新をどのように適用するかを追跡します。既存の配列マージの動作を、提案されている第1レベルのオブジェクトマージと比較し、ネストされた場合に期待される動作について issue の例を確認します。説明されている store 更新パターンをカバーし、現在の配列の動作を維持する部分的なオブジェクト更新用の API が確定すれば完了です。

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

説明

enhancement

A common pattern I believe is to reconcile partial state updates in stores.

For example, part of a store is synced to a backend, so after a push-to-backend operation, it returns an up-to-date data for part of the store values. Other values, like UI related ones are not included in this data.

Passing this raw data into setStore works, but triggers unnecessary updates. It triggers every single key's update, which is an array or object.

Here is my 1st idea to tackle this: running reconcile in a for loop on each 1st level key:

for (const key of Object.keys(data)) {
  this.setState(key as any, reconcile(data[key as keyof typeof data]))
}

This works, and when put in a batch() should be performant. Quite a lot of code, so asks for a wrapper function, but should be good.

My 2nd idea is this:

this.setState(reconcile({ ...this.state, ...data }))

This also works. If I understand correctly, it's a tiny bit slower, but the slowness happens in the pure JS code, not in the actual UI updates, so it should still be fast.

When I looked at the source code for reconcile and found that it supports merge, but only for arrays.

I'd like to recommend to add support for merge in objects. To make sure that this does not modify the array behaviours in sub-keys, it should be called something different, "partial" probably.

I believe it'd be as simple as turning off the delete loop in the first level of applyState.

Please tell me if I'm not correct in this. Isn't this a common pattern when using stores?

How do you normally solve this?

主要言語
TypeScript
スター
36.2k
フォーク
1.1k
平均マージ
11時間 5分
マージ済み PR(30日)
475

環境構築

はじめの一歩

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

solidjs/solid のほかの issue

solidjs/solid の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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