You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+142-1Lines changed: 142 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,40 @@ A library for building Yjs collaborative web applications with Mutative.
16
16
- 🚀 **Performance**: Efficient patch-based updates with structural sharing
17
17
- 🔌 **Flexible**: Customizable patch application for advanced use cases
18
18
- 📡 **Reactive**: Built-in subscription system for state changes
19
+
- ⚡ **Explicit Transactions**: Updates to Y.js are batched in transactions, you control the boundary
20
+
- 🪶 **Lightweight**: Simple, small codebase with no magic or vendor lock-in
21
+
- 🎨 **Non-intrusive**: Always opt-in by nature (snapshots are just plain objects)
22
+
23
+
## Why mutative-yjs?
24
+
25
+
**Do:**
26
+
27
+
```typescript
28
+
// any operation supported by mutative
29
+
binder.update((state) => {
30
+
state.nested[0].key= {
31
+
id: 123,
32
+
p1: 'a',
33
+
p2: ['a', 'b', 'c'],
34
+
};
35
+
});
36
+
```
37
+
38
+
**Instead of:**
39
+
40
+
```typescript
41
+
Y.transact(state.doc, () => {
42
+
const val =newY.Map();
43
+
val.set('id', 123);
44
+
val.set('p1', 'a');
45
+
46
+
const arr =newY.Array();
47
+
arr.push(['a', 'b', 'c']);
48
+
val.set('p2', arr);
49
+
50
+
state.get('nested').get(0).set('key', val);
51
+
});
52
+
```
19
53
20
54
## Installation
21
55
@@ -159,6 +193,28 @@ binder.unbind();
159
193
160
194
## Advanced Usage
161
195
196
+
### Structural Sharing
197
+
198
+
Like Mutative, `mutative-yjs` provides efficient structural sharing. Unchanged parts of the state maintain the same reference, which is especially beneficial for React re-renders:
199
+
200
+
```typescript
201
+
const snapshot1 =binder.get();
202
+
203
+
binder.update((state) => {
204
+
state.todos[0].done=true;
205
+
});
206
+
207
+
const snapshot2 =binder.get();
208
+
209
+
// changed properties have new references
210
+
snapshot1.todos!==snapshot2.todos;
211
+
snapshot1.todos[0] !==snapshot2.todos[0];
212
+
213
+
// unchanged properties keep the same reference
214
+
snapshot1.todos[1] ===snapshot2.todos[1];
215
+
snapshot1.todos[2] ===snapshot2.todos[2];
216
+
```
217
+
162
218
### Custom Patch Application
163
219
164
220
You can customize how Mutative patches are applied to Yjs data structures:
0 commit comments