state-sync-log 0.9.0 β†’ 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +368 -277
  3. package/dist/state-sync-log.esm.js +929 -136
  4. package/dist/state-sync-log.esm.mjs +929 -136
  5. package/dist/state-sync-log.umd.js +928 -135
  6. package/dist/types/createOps/constant.d.ts +6 -0
  7. package/dist/types/createOps/createOps.d.ts +25 -0
  8. package/dist/types/createOps/current.d.ts +13 -0
  9. package/dist/types/createOps/draft.d.ts +14 -0
  10. package/dist/types/createOps/draftify.d.ts +5 -0
  11. package/dist/types/createOps/index.d.ts +12 -0
  12. package/dist/types/createOps/interface.d.ts +74 -0
  13. package/dist/types/createOps/original.d.ts +15 -0
  14. package/dist/types/createOps/pushOp.d.ts +9 -0
  15. package/dist/types/createOps/setHelpers.d.ts +25 -0
  16. package/dist/types/createOps/utils.d.ts +95 -0
  17. package/dist/types/draft.d.ts +2 -2
  18. package/dist/types/index.d.ts +1 -0
  19. package/dist/types/json.d.ts +1 -1
  20. package/dist/types/operations.d.ts +2 -2
  21. package/dist/types/utils.d.ts +5 -0
  22. package/package.json +1 -1
  23. package/src/createOps/constant.ts +10 -0
  24. package/src/createOps/createOps.ts +97 -0
  25. package/src/createOps/current.ts +85 -0
  26. package/src/createOps/draft.ts +606 -0
  27. package/src/createOps/draftify.ts +45 -0
  28. package/src/createOps/index.ts +18 -0
  29. package/src/createOps/interface.ts +95 -0
  30. package/src/createOps/original.ts +24 -0
  31. package/src/createOps/pushOp.ts +42 -0
  32. package/src/createOps/setHelpers.ts +93 -0
  33. package/src/createOps/utils.ts +325 -0
  34. package/src/draft.ts +306 -288
  35. package/src/index.ts +1 -0
  36. package/src/json.ts +1 -1
  37. package/src/operations.ts +33 -11
  38. package/src/utils.ts +67 -55
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.0
4
+
5
+ - Lots of fixes, changes and improvements
6
+ - Added `createOps` API to be able to generate operations from a state mutation.
7
+
3
8
  ## 0.9.0
4
9
 
5
10
  - Initial release
package/README.md CHANGED
@@ -1,277 +1,368 @@
1
- <p align="center">
2
- <img src="./logo.png" height="220" />
3
- </p>
4
- <p align="center">
5
- <i>State synchronization log for collaborative applications. <b>Validate every change before it happens.</b></i>
6
- </p>
7
-
8
- <p align="center">
9
- <a aria-label="NPM version" href="https://www.npmjs.com/package/state-sync-log">
10
- <img src="https://img.shields.io/npm/v/state-sync-log.svg?style=for-the-badge&logo=npm&labelColor=333" />
11
- </a>
12
- <a aria-label="License" href="./LICENSE">
13
- <img src="https://img.shields.io/npm/l/state-sync-log.svg?style=for-the-badge&labelColor=333" />
14
- </a>
15
- <a aria-label="Types" href="./packages/state-sync-log/tsconfig.json">
16
- <img src="https://img.shields.io/npm/types/state-sync-log.svg?style=for-the-badge&logo=typescript&labelColor=333" />
17
- </a>
18
- <br />
19
- <a aria-label="CI" href="https://github.com/xaviergonz/state-sync-log/actions/workflows/main.yml">
20
- <img src="https://img.shields.io/github/actions/workflow/status/xaviergonz/state-sync-log/main.yml?branch=master&label=CI&logo=github&style=for-the-badge&labelColor=333" />
21
- </a>
22
- <a aria-label="Codecov" href="https://codecov.io/gh/xaviergonz/state-sync-log">
23
- <img src="https://img.shields.io/codecov/c/github/xaviergonz/state-sync-log?token=6MLRFUBK8V&label=codecov&logo=codecov&style=for-the-badge&labelColor=333" />
24
- </a>
25
- </p>
26
-
27
- ## The Problem with Standard CRDTs
28
-
29
- Tools like Yjs and Automerge are amazing for text editing because **they never reject a change**β€”they just merge everything.
30
-
31
- But for **business applications**, most often than not we have rules where "merging everything" can result in a bug. For example, if you have a "WIP Limit" of 3 tasks in a Kanban board and users drag two tasks in at once, you end up with 4 tasks.
32
-
33
- ## The Solution: state-sync-log
34
-
35
- `state-sync-log` is a **Validated Replicated State Machine**. It uses the same robust technology as Yjs in its core (networking, offline support), but it fundamentally changes the rules:
36
-
37
- **Every transaction is validated against your business logic before it is applied.**
38
-
39
- If a peer sends an invalid transaction your clients **reject it strictly and deterministically**, even when the change itself was made while offline.
40
-
41
- ### Comparison
42
-
43
- | Feature | state-sync-log | Standard CRDTs (Yjs, Automerge) |
44
- | :--- | :---: | :---: |
45
- | **Conflict Strategy** | 🫸 **Reject Invalid Changes** | πŸ”€ **Merge Everything** |
46
- | **Data Model** | Plain JSON | Specialized Types (Y.Map, Y.Array) |
47
- | **Validation** | βœ… First-class citizen | ❌ Not possible (by design) |
48
- | **Best For** | Business logic, Forms, Games, CRUD, Complex editors | Text editing, Drawing, Notes |
49
-
50
- ---
51
-
52
- ## Example: Kanban Board with WIP Limits
53
-
54
- Imagine a Kanban board where you strictly enforce a limit of **3 tasks** in the "Doing" column.
55
-
56
- ```ts
57
- import { createStateSyncLog } from "state-sync-log"
58
- import * as Y from "yjs"
59
-
60
- type Task = { id: string; title: string; status: "todo" | "doing" | "done" }
61
- type State = { tasks: Task[] }
62
-
63
- // 1. Define your business rules
64
- const validate = (state: State) => {
65
- // RULE: Cannot have more than 3 tasks in 'doing'
66
- const doingCount = state.tasks.filter(t => t.status === "doing").length
67
- if (doingCount > 3) return false
68
-
69
- // RULE: Tasks must always have a title
70
- if (state.tasks.some(t => t.title.trim() === "")) return false
71
-
72
- return true
73
- }
74
-
75
- // 2. Initialize the log
76
- const log = createStateSyncLog<State>({
77
- yDoc: new Y.Doc(),
78
- validate,
79
- // ... other options
80
- })
81
-
82
- // 3. Try to move a 4th task to "doing"
83
- // If another user already filled the slot, this operation
84
- // will be REJECTED on all clients (including this one).
85
- log.emit([
86
- { kind: "set", path: ["tasks", 3], key: "status", value: "doing" }
87
- ])
88
- ```
89
-
90
- ## Features
91
-
92
- - πŸ›‘οΈ **Bulletproof Validation**: Define a single `(state) => boolean` function. If it returns false, the transaction never happened.
93
- - ⏭️ **Replayable History**: Since it's an event log, you can replay history to see exactly *how* a state was reached (up to the nearest checkpoint).
94
- - 🏎️ **Optimistic UI**: Changes apply instantly locally. If they are later rejected (due to a conflict with a remote peer), the state automatically rolls back.
95
- - πŸ“¦ **Plain JSON**: Work with standard JS objects and arrays. No need to learn `ymap.get('foo')` syntax.
96
- - πŸ”Œ **Network Agnostic**: Works with any Yjs provider (WebSockets, WebRTC, IndexedDB).
97
- - πŸ’Ύ **Storage Efficient**: Built-in compaction and retention policies keep your data small and fast.
98
-
99
- ## Contents
100
-
101
- - [Installation](#installation)
102
- - [API Reference](#api-reference)
103
- - [Operations](#operations)
104
- - [Gotchas & Limitations](#gotchas--limitations)
105
- - [Contributing](#contributing)
106
- - [License](#license)
107
-
108
- ## Installation
109
-
110
- ```bash
111
- npm install state-sync-log
112
- # or
113
- pnpm add state-sync-log
114
- # or
115
- yarn add state-sync-log
116
- ```
117
-
118
- ## Storage Efficiency
119
-
120
- Since this is an append-only log, you might worry about it growing forever. We solved that.
121
-
122
- ### πŸ—œοΈ Automatic Compaction & Retention
123
-
124
- `state-sync-log` can periodically be asked to compact the log into a **snapshot checkpoint**.
125
-
126
- - **Checkpoints:** New peers just load the latest snapshot + recent ops. Fast load times!
127
- - **Retention Window:** Old transaction history is automatically pruned after a set time (recommended: 2 weeks).
128
- - **Result:** You get a full audit trail for recent history, without unboundedly growing storage.
129
-
130
- ## Integration with MobX, Signals, etc
131
-
132
- You don't have to replace your existing state manager. `state-sync-log` is designed to drive them.
133
-
134
- Using `applyOps`, you can surgically apply updates to **MobX**, **Preact Signals**, or any mutable store:
135
-
136
- ```ts
137
- import { applyOps } from "state-sync-log"
138
- import { observable } from "mobx"
139
-
140
- // 1. Create your mutable MobX store (init with current state)
141
- const store = observable(log.getState())
142
-
143
- // 2. Sync it!
144
- // 2. Sync it!
145
- log.subscribe((newState, getAppliedOps) => {
146
- // getAppliedOps is a lazy getter (computing reconciliation diffs only when requested)
147
- const appliedOps = getAppliedOps()
148
-
149
- // Apply ONLY the changes (efficient!)
150
- applyOps(appliedOps, store)
151
- })
152
- ```
153
-
154
- By default, `applyOps` deep clones values before inserting them to prevent aliasing. For better performance, you can disable cloning if you guarantee op values won't be mutated:
155
-
156
- ```ts
157
- // Calculate ops first
158
- const appliedOps = getAppliedOps()
159
- applyOps(appliedOps, store, { cloneValues: false })
160
- ```
161
-
162
- ## API Reference
163
-
164
- ### `createStateSyncLog(options)`
165
-
166
- Initializes the synchronization log.
167
-
168
- ```ts
169
- import { createStateSyncLog } from "state-sync-log"
170
-
171
- const log = createStateSyncLog<State>({
172
- yDoc: new Y.Doc(),
173
- validate: (state) => state.inventory >= 0
174
- })
175
- ```
176
-
177
- **Options:**
178
-
179
- | Option | Type | Description |
180
- | --- | --- | --- |
181
- | `yDoc` | `Y.Doc` | **Required.** The Yjs document instance. |
182
- | `validate` | `(state: State) => boolean` | **Required.** The gatekeeper function. If it returns `false`, the transaction is dropped. |
183
- | `clientId` | `string` | Optional unique ID. Auto-generated if omitted. |
184
- | `retentionWindowMs` | `number` | Time to keep transaction history before pruning (recommended: 2 weeks). Helps keep storage small. |
185
-
186
- ### `StateSyncLogController`
187
-
188
- The object returned by `createStateSyncLog`.
189
-
190
- #### `getState(): State`
191
-
192
- Returns the current, validated state. Uses structural sharing for efficient immutable updates.
193
-
194
- #### `emit(ops: Op[]): void`
195
-
196
- Propose a change. The change applies optimistically but may be reverted if it conflicts with a remote change that renders it invalid.
197
-
198
- #### `subscribe(callback): UnsubscribeFn`
199
-
200
- Listen for state changes. The callback receives the new state and a lazy getter function for the operations applied.
201
-
202
- ```ts
203
- log.subscribe((newState, getAppliedOps) => {
204
- const appliedOps = getAppliedOps()
205
- render(newState)
206
- })
207
- ```
208
-
209
- #### `reconcileState(targetState: State): void`
210
-
211
- Automatically calculates the operations needed to turn the current state into `targetState` and emits them. Great for "Reset to Default" features.
212
-
213
- #### `compact(): void`
214
-
215
- Manually triggers a checkpoint. This compresses the history into a single snapshot to save memory and load time.
216
-
217
- #### `dispose(): void`
218
-
219
- Stop listening and cleanup.
220
-
221
- ## Operations
222
-
223
- These are the atomic building blocks of your transactions.
224
-
225
- ### `set` (Objects)
226
-
227
- Sets a property on an object.
228
-
229
- ```ts
230
- { kind: "set", path: ["users", "u1"], key: "name", value: "Alice" }
231
- ```
232
-
233
- ### `delete` (Objects)
234
-
235
- Removes a property (equivalent of setting a property to `undefined`).
236
-
237
- ```ts
238
- { kind: "delete", path: ["users", "u1"], key: "avatarUrl" }
239
- ```
240
-
241
- ### `splice` (Arrays)
242
-
243
- Insert, remove, or replace items in an array.
244
-
245
- ```ts
246
- // Remove 1 item at index 0, insert "New Item"
247
- { kind: "splice", path: ["todoList"], index: 0, deleteCount: 1, inserts: ["New Item"] }
248
- ```
249
-
250
- ### `addToSet` (Arrays)
251
-
252
- Adds an item only if it doesn't exist (like a Set).
253
-
254
- ```ts
255
- { kind: "addToSet", path: ["tags"], value: "urgent" }
256
- ```
257
-
258
- ### `deleteFromSet` (Arrays)
259
-
260
- Removes an item if it exists.
261
-
262
- ```ts
263
- { kind: "deleteFromSet", path: ["tags"], value: "deprecated" }
264
- ```
265
-
266
- ## Gotchas & Limitations
267
-
268
- 1. **Validation must be deterministic:** Your `validate` function must return the same result for the same state input (deterministic). Don't check `Date.now()` or make API calls inside it.
269
- 2. **Not for Text:** Do not use this for collaborative text editing (Google Docs style). Use standard Y.Text for that; you can mix standard Yjs and `state-sync-log` in the same application!
270
-
271
- ## Contributing
272
-
273
- See [CONTRIBUTING.md](./CONTRIBUTING.md).
274
-
275
- ## License
276
-
277
- MIT. See [LICENSE](./LICENSE).
1
+ <p align="center">
2
+ <img src="./logo.png" height="220" />
3
+ </p>
4
+ <p align="center">
5
+ <i>State synchronization log for collaborative applications. <b>Validate every change before it happens.</b></i>
6
+ </p>
7
+
8
+ <p align="center">
9
+ <a aria-label="NPM version" href="https://www.npmjs.com/package/state-sync-log">
10
+ <img src="https://img.shields.io/npm/v/state-sync-log.svg?style=for-the-badge&logo=npm&labelColor=333" />
11
+ </a>
12
+ <a aria-label="License" href="./LICENSE">
13
+ <img src="https://img.shields.io/npm/l/state-sync-log.svg?style=for-the-badge&labelColor=333" />
14
+ </a>
15
+ <a aria-label="Types" href="./packages/state-sync-log/tsconfig.json">
16
+ <img src="https://img.shields.io/npm/types/state-sync-log.svg?style=for-the-badge&logo=typescript&labelColor=333" />
17
+ </a>
18
+ <br />
19
+ <a aria-label="CI" href="https://github.com/xaviergonz/state-sync-log/actions/workflows/main.yml">
20
+ <img src="https://img.shields.io/github/actions/workflow/status/xaviergonz/state-sync-log/main.yml?branch=master&label=CI&logo=github&style=for-the-badge&labelColor=333" />
21
+ </a>
22
+ <a aria-label="Codecov" href="https://codecov.io/gh/xaviergonz/state-sync-log">
23
+ <img src="https://img.shields.io/codecov/c/github/xaviergonz/state-sync-log?token=6MLRFUBK8V&label=codecov&logo=codecov&style=for-the-badge&labelColor=333" />
24
+ </a>
25
+ </p>
26
+
27
+ ## The Problem with Standard CRDTs
28
+
29
+ Tools like Yjs and Automerge are amazing for text editing because **they never reject a change**β€”they just merge everything.
30
+
31
+ But for **business applications**, most often than not we have rules where "merging everything" can result in a bug. For example, if you have a "WIP Limit" of 3 tasks in a Kanban board and users drag two tasks in at once, you end up with 4 tasks.
32
+
33
+ ## The Solution: state-sync-log
34
+
35
+ `state-sync-log` is a **Validated Replicated State Machine**. It uses the same robust technology as Yjs in its core (networking, offline support), but it fundamentally changes the rules:
36
+
37
+ **Every transaction is validated against your business logic before it is applied.**
38
+
39
+ If a peer sends an invalid transaction your clients **reject it strictly and deterministically**, even when the change itself was made while offline.
40
+
41
+ ### Comparison
42
+
43
+ | Feature | state-sync-log | Standard CRDTs (Yjs, Automerge) |
44
+ | :--- | :---: | :---: |
45
+ | **Conflict Strategy** | 🫸 **Reject Invalid Changes** | πŸ”€ **Merge Everything** |
46
+ | **Data Model** | Plain JSON | Specialized Types (Y.Map, Y.Array) |
47
+ | **Validation** | βœ… First-class citizen | ❌ Not possible (by design) |
48
+ | **Best For** | Business logic, Forms, Games, CRUD, Complex editors | Text editing, Drawing, Notes |
49
+
50
+ ---
51
+
52
+ ## Example: Kanban Board with WIP Limits
53
+
54
+ Imagine a Kanban board where you strictly enforce a limit of **3 tasks** in the "Doing" column.
55
+
56
+ ```ts
57
+ import { createStateSyncLog } from "state-sync-log"
58
+ import * as Y from "yjs"
59
+
60
+ type Task = { id: string; title: string; status: "todo" | "doing" | "done" }
61
+ type State = { tasks: Task[] }
62
+
63
+ // 1. Define your business rules
64
+ const validate = (state: State) => {
65
+ // RULE: Cannot have more than 3 tasks in 'doing'
66
+ const doingCount = state.tasks.filter(t => t.status === "doing").length
67
+ if (doingCount > 3) return false
68
+
69
+ // RULE: Tasks must always have a title
70
+ if (state.tasks.some(t => t.title.trim() === "")) return false
71
+
72
+ return true
73
+ }
74
+
75
+ // 2. Initialize the log
76
+ const log = createStateSyncLog<State>({
77
+ yDoc: new Y.Doc(),
78
+ validate,
79
+ // ... other options
80
+ })
81
+
82
+ // 3. Try to move a 4th task to "doing"
83
+ // If another user already filled the slot, this operation
84
+ // will be REJECTED on all clients (including this one).
85
+ log.emit([
86
+ { kind: "set", path: ["tasks", 3], key: "status", value: "doing" }
87
+ ])
88
+ ```
89
+
90
+ ## Features
91
+
92
+ - πŸ›‘οΈ **Bulletproof Validation**: Define a single `(state) => boolean` function. If it returns false, the transaction never happened.
93
+ - ⏭️ **Replayable History**: Since it's an event log, you can replay history to see exactly *how* a state was reached (up to the nearest checkpoint).
94
+ - 🏎️ **Optimistic UI**: Changes apply instantly locally. If they are later rejected (due to a conflict with a remote peer), the state automatically rolls back.
95
+ - πŸ“¦ **Plain JSON**: Work with standard JS objects and arrays. No need to learn `ymap.get('foo')` syntax.
96
+ - πŸ”Œ **Network Agnostic**: Works with any Yjs provider (WebSockets, WebRTC, IndexedDB).
97
+ - πŸ’Ύ **Storage Efficient**: Built-in compaction and retention policies keep your data small and fast.
98
+
99
+ ## Contents
100
+
101
+ - [Installation](#installation)
102
+ - [API Reference](#api-reference)
103
+ - [Operations](#operations)
104
+ - [Generating Operations with createOps](#generating-operations-with-createops)
105
+ - [Gotchas & Limitations](#gotchas--limitations)
106
+ - [Contributing](#contributing)
107
+ - [License](#license)
108
+
109
+ ## Installation
110
+
111
+ ```bash
112
+ npm install state-sync-log
113
+ # or
114
+ pnpm add state-sync-log
115
+ # or
116
+ yarn add state-sync-log
117
+ ```
118
+
119
+ ## Storage Efficiency
120
+
121
+ Since this is an append-only log, you might worry about it growing forever. We solved that.
122
+
123
+ ### πŸ—œοΈ Automatic Compaction & Retention
124
+
125
+ `state-sync-log` can periodically be asked to compact the log into a **snapshot checkpoint**.
126
+
127
+ - **Checkpoints:** New peers just load the latest snapshot + recent ops. Fast load times!
128
+ - **Retention Window:** Old transaction history is automatically pruned after a set time (recommended: 2 weeks).
129
+ - **Result:** You get a full audit trail for recent history, without unboundedly growing storage.
130
+
131
+ ## Integration with MobX, Signals, etc
132
+
133
+ You don't have to replace your existing state manager. `state-sync-log` is designed to drive them.
134
+
135
+ Using `applyOps`, you can surgically apply updates to **MobX**, **Preact Signals**, or any mutable store:
136
+
137
+ ```ts
138
+ import { applyOps } from "state-sync-log"
139
+ import { observable } from "mobx"
140
+
141
+ // 1. Create your mutable MobX store (init with current state)
142
+ const store = observable(log.getState())
143
+
144
+ // 2. Sync it!
145
+ // 2. Sync it!
146
+ log.subscribe((newState, getAppliedOps) => {
147
+ // getAppliedOps is a lazy getter (computing reconciliation diffs only when requested)
148
+ const appliedOps = getAppliedOps()
149
+
150
+ // Apply ONLY the changes (efficient!)
151
+ applyOps(appliedOps, store)
152
+ })
153
+ ```
154
+
155
+ By default, `applyOps` deep clones values before inserting them to prevent aliasing. For better performance, you can disable cloning if you guarantee op values won't be mutated:
156
+
157
+ ```ts
158
+ // Calculate ops first
159
+ const appliedOps = getAppliedOps()
160
+ applyOps(appliedOps, store, { cloneValues: false })
161
+ ```
162
+
163
+ ## API Reference
164
+
165
+ ### `createStateSyncLog(options)`
166
+
167
+ Initializes the synchronization log.
168
+
169
+ ```ts
170
+ import { createStateSyncLog } from "state-sync-log"
171
+
172
+ const log = createStateSyncLog<State>({
173
+ yDoc: new Y.Doc(),
174
+ validate: (state) => state.inventory >= 0
175
+ })
176
+ ```
177
+
178
+ **Options:**
179
+
180
+ | Option | Type | Description |
181
+ | --- | --- | --- |
182
+ | `yDoc` | `Y.Doc` | **Required.** The Yjs document instance. |
183
+ | `validate` | `(state: State) => boolean` | **Required.** The gatekeeper function. If it returns `false`, the transaction is dropped. |
184
+ | `clientId` | `string` | Optional unique ID. Auto-generated if omitted. |
185
+ | `retentionWindowMs` | `number` | Time to keep transaction history before pruning (recommended: 2 weeks). Helps keep storage small. |
186
+
187
+ ### `StateSyncLogController`
188
+
189
+ The object returned by `createStateSyncLog`.
190
+
191
+ #### `getState(): State`
192
+
193
+ Returns the current, validated state. Uses structural sharing for efficient immutable updates.
194
+
195
+ #### `emit(ops: Op[]): void`
196
+
197
+ Propose a change. The change applies optimistically but may be reverted if it conflicts with a remote change that renders it invalid.
198
+
199
+ #### `subscribe(callback): UnsubscribeFn`
200
+
201
+ Listen for state changes. The callback receives the new state and a lazy getter function for the operations applied.
202
+
203
+ ```ts
204
+ log.subscribe((newState, getAppliedOps) => {
205
+ const appliedOps = getAppliedOps()
206
+ render(newState)
207
+ })
208
+ ```
209
+
210
+ #### `reconcileState(targetState: State): void`
211
+
212
+ Automatically calculates the operations needed to turn the current state into `targetState` and emits them. Great for "Reset to Default" features.
213
+
214
+ #### `compact(): void`
215
+
216
+ Manually triggers a checkpoint. This compresses the history into a single snapshot to save memory and load time.
217
+
218
+ #### `dispose(): void`
219
+
220
+ Stop listening and cleanup.
221
+
222
+ ## Operations
223
+
224
+ These are the atomic building blocks of your transactions.
225
+
226
+ ### `set` (Objects)
227
+
228
+ Sets a property on an object.
229
+
230
+ ```ts
231
+ { kind: "set", path: ["users", "u1"], key: "name", value: "Alice" }
232
+ ```
233
+
234
+ ### `delete` (Objects)
235
+
236
+ Removes a property (equivalent of setting a property to `undefined`).
237
+
238
+ ```ts
239
+ { kind: "delete", path: ["users", "u1"], key: "avatarUrl" }
240
+ ```
241
+
242
+ ### `splice` (Arrays)
243
+
244
+ Insert, remove, or replace items in an array.
245
+
246
+ ```ts
247
+ // Remove 1 item at index 0, insert "New Item"
248
+ { kind: "splice", path: ["todoList"], index: 0, deleteCount: 1, inserts: ["New Item"] }
249
+ ```
250
+
251
+ ### `addToSet` (Arrays)
252
+
253
+ Adds an item only if it doesn't exist (like a Set).
254
+
255
+ ```ts
256
+ { kind: "addToSet", path: ["tags"], value: "urgent" }
257
+ ```
258
+
259
+ ### `deleteFromSet` (Arrays)
260
+
261
+ Removes an item if it exists.
262
+
263
+ ```ts
264
+ { kind: "deleteFromSet", path: ["tags"], value: "deprecated" }
265
+ ```
266
+
267
+ ## Generating Operations with `createOps`
268
+
269
+ Writing operations by hand can be tedious and error-prone. The `createOps` utility lets you describe changes using familiar mutable-style JavaScript code, and it automatically generates the corresponding operations.
270
+
271
+ ### Basic Usage
272
+
273
+ ```ts
274
+ import { createOps } from "state-sync-log/createOps"
275
+
276
+ const state = { list: [{ text: "Learn", done: false }] }
277
+
278
+ const { nextState, ops } = createOps(state, (draft) => {
279
+ // Mutate the draft like you would a normal object
280
+ draft.list[0].done = true
281
+ draft.list.push({ text: "Practice", done: false })
282
+ })
283
+
284
+ // ops contains the operations that were performed:
285
+ // [
286
+ // { kind: 'set', path: ['list', 0], key: 'done', value: true },
287
+ // { kind: 'splice', path: ['list'], index: 1, deleteCount: 0, inserts: [{ text: 'Practice', done: false }] }
288
+ // ]
289
+
290
+ // nextState is the new immutable state (original state is unchanged)
291
+ ```
292
+
293
+ ### Supported Mutations
294
+
295
+ - **Object properties**: `draft.user.name = "Alice"` generates a `set` op
296
+ - **Delete properties**: `delete draft.user.avatar` generates a `delete` op
297
+ - **Array methods**: `push`, `pop`, `shift`, `unshift`, `splice`, `fill`, `sort`, `reverse`, `copyWithin` all generate `splice` ops
298
+ - **Array index assignment**: `draft.list[0] = newItem` generates a `set` op
299
+ - **Array length**: `draft.list.length = 5` generates a `set` op for length
300
+
301
+ ### Utility Functions
302
+
303
+ #### `original(draft)`
304
+
305
+ Returns the original (unmodified) value from a draft. Useful for comparisons.
306
+
307
+ ```ts
308
+ import { createOps, original } from "state-sync-log/createOps"
309
+
310
+ createOps(state, (draft) => {
311
+ if (original(draft.user) !== draft.user) {
312
+ console.log("User was modified")
313
+ }
314
+ })
315
+ ```
316
+
317
+ #### `current(draft)`
318
+
319
+ Returns a snapshot of the current state of the draft (deep clone).
320
+
321
+ ```ts
322
+ import { createOps, current } from "state-sync-log/createOps"
323
+
324
+ createOps(state, (draft) => {
325
+ draft.count++
326
+ console.log(current(draft)) // { count: 1 }
327
+ })
328
+ ```
329
+
330
+ #### `isDraft(value)` / `isDraftable(value)`
331
+
332
+ Check if a value is a draft or can be made into one.
333
+
334
+ ```ts
335
+ import { isDraft, isDraftable } from "state-sync-log/createOps"
336
+
337
+ isDraft(someDraft) // true for draft proxies
338
+ isDraftable({ a: 1 }) // true for plain objects/arrays
339
+ isDraftable(new Date()) // false for class instances
340
+ ```
341
+
342
+ #### `addToSet(draft, path, value)` / `deleteFromSet(draft, path, value)`
343
+
344
+ Helpers for treating arrays as sets (no duplicates).
345
+
346
+ ```ts
347
+ import { createOps, addToSet, deleteFromSet } from "state-sync-log/createOps"
348
+
349
+ const { ops } = createOps({ tags: ["a", "b"] }, (draft) => {
350
+ addToSet(draft, ["tags"], "c") // Adds "c" since it doesn't exist
351
+ addToSet(draft, ["tags"], "a") // No-op, "a" already exists
352
+ deleteFromSet(draft, ["tags"], "b") // Removes "b"
353
+ })
354
+ // ops: [{ kind: 'addToSet', ... }, { kind: 'deleteFromSet', ... }]
355
+ ```
356
+
357
+ ## Gotchas & Limitations
358
+
359
+ 1. **Validation must be deterministic:** Your `validate` function must return the same result for the same state input (deterministic). Don't check `Date.now()` or make API calls inside it.
360
+ 2. **Not for Text:** Do not use this for collaborative text editing (Google Docs style). Use standard Y.Text for that; you can mix standard Yjs and `state-sync-log` in the same application!
361
+
362
+ ## Contributing
363
+
364
+ See [CONTRIBUTING.md](./CONTRIBUTING.md).
365
+
366
+ ## License
367
+
368
+ MIT. See [LICENSE](./LICENSE).