[Feature Request] Support for "Splice" patches in arrays for CRDT/Y.js integration (and others)

未關閉
#1,202 2 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 3 天內回覆

還沒有人認領這個 Issue。

評估

難度
5/5
預估耗時
一週以上
新手友好度
25/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
javascript

研究方向

首先,針對 splice、shift 和 unshift,評估 Immer 目前的陣列修補生成方式,並與提議的 arraySplice 選項和修補格式進行比較。將此設計與 Y.js、Automerge 和 MobX 的使用案例進行比較。當就一種可選擇啟用的 splice 表示法達成共識,且該表示法保留預設的 JSON Patch 行為並涵蓋列出的陣列操作時,即視為完成。

由索引模型根據 Issue 內容生成。

描述

proposal

Currently, when performing array operations like splice(), shift(), or unshift(), Immer generates standard JSON patches (RFC 6902). While spec-compliant, this approach is inefficient for arrays when items are added or removed from the middle.

For example, given [1, 2, 3, 4], calling .splice(1, 1) results in:

  • replace index 1 with 3
  • replace index 2 with 4
  • remove index 3

The Problem
This "shifting" behavior is problematic for CRDTs (like Y.js) or observation-based libraries (like MobX).

Performance: Large arrays trigger a cascade of patches for a single deletion.

Convergence: In Y.js, "replacing" an element is different from "shifting" it. If Immer reports that every element after the splice point has been replaced, the CRDT loses the identity of those objects, breaking the ability to merge concurrent changes effectively.

Proposed Solution

I propose an optional configuration (e.g., arraySplice: true) that generates a non-standard splice operation for array mutations. This would group push, pop, shift, unshift, and splice into a single descriptive patch.

Example Proposed Patch Format:

{
  "op": "splice",
  "path": "/arr",
  "index": 1,
  "deleteCount": 1,
  "items": [...]
}

Use Case

Y.js / Automerge integration: Directly mapping a splice patch to yArray.delete(index, count) and yArray.insert(index, items).

In MobX / other libs it is just calling a splice operation instead.

Performance Optimization: Reducing the payload size when syncing state across WebSockets.

Identity Preservation: Ensuring that elements not being deleted maintain their reference/identity in the eyes of the consumer.

Additional Context

Libraries like mobx use this observation pattern successfully. While I understand Immer aims for JSON Patch compatibility, providing an opt-in for "Rich Patches" or "Splice Patches" would make it the go-to library for distributed state management.

主要語言
JavaScript
星號
29k
分支
905
平均合併
3 天 7 小時
30 天內合併 PR
21

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

immerjs/immer 的其他 Issue

查看 immerjs/immer 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。