mobx-keystone-loro 1.0.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 (37) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +119 -0
  4. package/dist/mobx-keystone-loro.esm.js +957 -0
  5. package/dist/mobx-keystone-loro.esm.mjs +957 -0
  6. package/dist/mobx-keystone-loro.umd.js +957 -0
  7. package/dist/types/binding/LoroTextModel.d.ts +72 -0
  8. package/dist/types/binding/applyLoroEventToMobx.d.ts +9 -0
  9. package/dist/types/binding/applyMobxChangeToLoroObject.d.ts +7 -0
  10. package/dist/types/binding/bindLoroToMobxKeystone.d.ts +33 -0
  11. package/dist/types/binding/convertJsonToLoroData.d.ts +51 -0
  12. package/dist/types/binding/convertLoroDataToJson.d.ts +11 -0
  13. package/dist/types/binding/loroBindingContext.d.ts +39 -0
  14. package/dist/types/binding/loroSnapshotTracking.d.ts +26 -0
  15. package/dist/types/binding/moveWithinArray.d.ts +36 -0
  16. package/dist/types/binding/resolveLoroPath.d.ts +11 -0
  17. package/dist/types/index.d.ts +8 -0
  18. package/dist/types/plainTypes.d.ts +6 -0
  19. package/dist/types/utils/error.d.ts +4 -0
  20. package/dist/types/utils/getOrCreateLoroCollectionAtom.d.ts +7 -0
  21. package/dist/types/utils/isBindableLoroContainer.d.ts +9 -0
  22. package/package.json +92 -0
  23. package/src/binding/LoroTextModel.ts +211 -0
  24. package/src/binding/applyLoroEventToMobx.ts +280 -0
  25. package/src/binding/applyMobxChangeToLoroObject.ts +182 -0
  26. package/src/binding/bindLoroToMobxKeystone.ts +353 -0
  27. package/src/binding/convertJsonToLoroData.ts +315 -0
  28. package/src/binding/convertLoroDataToJson.ts +68 -0
  29. package/src/binding/loroBindingContext.ts +46 -0
  30. package/src/binding/loroSnapshotTracking.ts +36 -0
  31. package/src/binding/moveWithinArray.ts +112 -0
  32. package/src/binding/resolveLoroPath.ts +37 -0
  33. package/src/index.ts +16 -0
  34. package/src/plainTypes.ts +7 -0
  35. package/src/utils/error.ts +12 -0
  36. package/src/utils/getOrCreateLoroCollectionAtom.ts +17 -0
  37. package/src/utils/isBindableLoroContainer.ts +13 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Change Log
2
+
3
+ ## 1.0.0 (unreleased)
4
+
5
+ - Refactored internal synchronization to use deep change observation instead of JSON patches. This provides proper array splice detection, avoiding the previous behavior where array operations were converted to individual element patches. The result is more efficient array synchronization and better alignment with how Loro handles array modifications.
6
+ - Added `moveWithinArray(array, fromIndex, toIndex)` helper function for explicit array move operations. When used on a bound array, this translates to a native Loro `move()` operation, preserving item identity and history across clients. This replaces the previous automatic move detection logic which was complex and less predictable.
7
+ - Fixed a synchronization issue where values added to a collection and then mutated within the same action could cause desync. Snapshots are now captured at change time rather than at action completion time.
8
+ - Initial release of `mobx-keystone-loro`.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Javier González Garcés
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # mobx-keystone-loro
2
+
3
+ Loro CRDT bindings for mobx-keystone.
4
+
5
+ This package provides bidirectional synchronization between mobx-keystone models and Loro CRDT documents. It enables real-time collaborative editing of mobx-keystone state trees.
6
+
7
+ ## Features
8
+
9
+ - Uses `LoroMap` for objects (LWW semantics)
10
+ - Uses `LoroMovableList` for arrays with native move operations
11
+ - Uses `LoroText` for rich text editing
12
+ - Automatically detects and uses move operations when reordering array items
13
+ - Full bidirectional sync between mobx-keystone and Loro
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install mobx-keystone-loro
19
+ # or
20
+ yarn add mobx-keystone-loro
21
+ ```
22
+
23
+ ## Peer Dependencies
24
+
25
+ - `mobx` ^4.3.1 || ^5.0.0 || ^6.0.0
26
+ - `mobx-keystone` ^1.16.0
27
+ - `loro-crdt` ^1.0.0
28
+
29
+ ## Usage
30
+
31
+ ```typescript
32
+ import { LoroDoc } from "loro-crdt"
33
+ import { Model, tProp, types, modelAction, registerRootStore } from "mobx-keystone"
34
+ import { bindLoroToMobxKeystone } from "mobx-keystone-loro"
35
+
36
+ // Define your model
37
+ @model("myApp/Todo")
38
+ class Todo extends Model({
39
+ text: tProp(types.string),
40
+ done: tProp(types.boolean, false),
41
+ }) {
42
+ @modelAction
43
+ setText(text: string) {
44
+ this.text = text
45
+ }
46
+
47
+ @modelAction
48
+ toggle() {
49
+ this.done = !this.done
50
+ }
51
+ }
52
+
53
+ // Create a Loro document
54
+ const doc = new LoroDoc()
55
+ const rootMap = doc.getMap("root")
56
+
57
+ // Initialize with snapshot data
58
+ rootMap.set("text", "Hello")
59
+ rootMap.set("done", false)
60
+ doc.commit()
61
+
62
+ // Bind to mobx-keystone
63
+ const { boundObject, dispose } = bindLoroToMobxKeystone({
64
+ loroDoc: doc,
65
+ loroObject: rootMap,
66
+ mobxKeystoneType: Todo,
67
+ })
68
+
69
+ registerRootStore(boundObject)
70
+
71
+ // Changes to boundObject will sync to Loro
72
+ boundObject.setText("World")
73
+
74
+ // Changes from Loro will sync to boundObject
75
+ rootMap.set("done", true)
76
+ doc.commit()
77
+
78
+ // Clean up when done
79
+ dispose()
80
+ ```
81
+
82
+ ## API
83
+
84
+ ### `bindLoroToMobxKeystone(options)`
85
+
86
+ Creates a bidirectional binding between a Loro object and a mobx-keystone model.
87
+
88
+ #### Options
89
+
90
+ - `loroDoc: LoroDoc` - The Loro document
91
+ - `loroObject: LoroMap | LoroMovableList | LoroText` - The Loro object to bind to
92
+ - `mobxKeystoneType: ModelClass<T> | AnyStandardType` - The mobx-keystone model class or type
93
+
94
+ #### Returns
95
+
96
+ - `boundObject: T` - The mobx-keystone model instance
97
+ - `dispose: () => void` - Function to clean up the binding
98
+ - `loroOrigin: string` - The origin string used for Loro commits by the binding
99
+
100
+ ### `LoroTextModel`
101
+
102
+ A mobx-keystone model for representing Loro rich text.
103
+
104
+ ```typescript
105
+ import { LoroTextModel } from "mobx-keystone-loro"
106
+
107
+ // Create a text model
108
+ const textModel = LoroTextModel.withText("Hello, world!")
109
+
110
+ // Access the plain text
111
+ console.log(textModel.text) // "Hello, world!"
112
+
113
+ // Access the delta for rich text (Quill format)
114
+ console.log(textModel.delta)
115
+ ```
116
+
117
+ ## License
118
+
119
+ MIT