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

Đang mở
#1,202 2 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript

Hướng nghiên cứu

Bắt đầu bằng việc đánh giá cách Immer hiện đang tạo patch cho mảng đối với splice, shift và unshift, so với tùy chọn arraySplice được đề xuất và định dạng patch. So sánh thiết kế này với các trường hợp sử dụng của Y.js, Automerge và MobX. Công việc được xem là hoàn tất khi đạt được thống nhất về một biểu diễn splice opt-in, duy trì hành vi JSON Patch mặc định và bao quát các thao tác mảng đã liệt kê.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
JavaScript
Star
29k
Fork
905
Merge trung bình
3 ngày 16 giờ
Pull request đã merge (30 ngày)
22

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của immerjs/immer

Tất cả issue của immerjs/immer

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.