[Feature Request] Support for "Splice" patches in arrays for CRDT/Y.js integration (and others)
維護者通常 3 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 25/100
- Issue 類型
- 功能
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- javascript
研究方向
首先,針對 splice、shift 和 unshift,評估 Immer 目前的陣列修補生成方式,並與提議的 arraySplice 選項和修補格式進行比較。將此設計與 Y.js、Automerge 和 MobX 的使用案例進行比較。當就一種可選擇啟用的 splice 表示法達成共識,且該表示法保留預設的 JSON Patch 行為並涵蓋列出的陣列操作時,即視為完成。
由索引模型根據 Issue 內容生成。
描述
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,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
immerjs/immer 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 35/100
維護者通常 3 天內回覆
-
Docs: Chinese (zh-CN) website translation is outdated vs English docs可能重新可做 @lijiayou0728 於 92 天前認領,目前沒有進行中的 PR。 未關閉
難度 4/5 3-5 天 新手友好度 55/100
維護者通常 3 天內回覆
-
question
難度 5/5 一週以上 新手友好度 35/100
維護者通常 3 天內回覆
-
難度 5/5 一週以上 新手友好度 35/100
維護者通常 3 天內回覆
-
proposal
難度 4/5 3-5 天 新手友好度 45/100
immerjs/immer#1189 · 1 個 reaction ·
維護者通常 3 天內回覆
相似的 Issue
-
status: needs triage
難度 1/5 1 小時以內 新手友好度 68/100
mastra-ai/mastra#29706 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 69/100
daniel-ospina/tortoise#11586 ·
維護者通常 1 天內回覆
-
bug stock
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
druxt: getResource returns an empty object for a failed request, so the status is lost可能已有人在做 @Decipher 於 43 天前認領。 未關閉bug needs triage
難度 2/5 1-3 小時 新手友好度 77/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
Yu-tao-Li/dsh-computer-use-win#76 ·
維護者通常 1 天內回覆