mobx-keystone-yjs 1.4.0 → 1.5.1

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.
@@ -1,249 +1,248 @@
1
- import { IAtom, computed, createAtom, observe, reaction } from "mobx"
2
- import {
3
- Frozen,
4
- Model,
5
- frozen,
6
- getParentToChildPath,
7
- model,
8
- onSnapshot,
9
- tProp,
10
- types,
11
- } from "mobx-keystone"
12
- import * as Y from "yjs"
13
- import { failure } from "../utils/error"
14
- import { YjsBindingContext, yjsBindingContext } from "./yjsBindingContext"
15
- import { resolveYjsPath } from "./resolveYjsPath"
16
-
17
- // Delta[][], since each single change is a Delta[]
18
- // we use frozen so that we can reuse each delta change
19
- const deltaListType = types.array(types.frozen(types.unchecked<unknown[]>()))
20
-
21
- export const yjsTextModelId = "mobx-keystone-yjs/YjsTextModel"
22
-
23
- /**
24
- * A mobx-keystone model that represents a Yjs.Text object.
25
- */
26
- @model(yjsTextModelId)
27
- export class YjsTextModel extends Model({
28
- deltaList: tProp(deltaListType, () => []),
29
- }) {
30
- /**
31
- * Helper function to create a YjsTextModel instance with a simple text.
32
- */
33
- static withText(text: string): YjsTextModel {
34
- return new DecoratedYjsTextModel({
35
- deltaList: [
36
- frozen([
37
- {
38
- insert: text,
39
- },
40
- ]),
41
- ],
42
- })
43
- }
44
-
45
- /**
46
- * The Y.js path from the bound object to the YjsTextModel instance.
47
- */
48
- @computed
49
- private get _yjsObjectPath() {
50
- const ctx = yjsBindingContext.get(this)
51
- if (!ctx || ctx.boundObject == null) {
52
- throw failure(
53
- "the YjsTextModel instance must be part of a bound object before it can be accessed"
54
- )
55
- }
56
-
57
- const path = getParentToChildPath(ctx.boundObject, this)
58
- if (!path) {
59
- throw failure("a path from the bound object to the YjsTextModel instance is not available")
60
- }
61
-
62
- return path
63
- }
64
-
65
- /**
66
- * The Yjs.Text object present at this mobx-keystone node's path.
67
- */
68
- @computed
69
- private get _yjsObjectAtPath(): unknown {
70
- const path = this._yjsObjectPath
71
-
72
- const ctx = yjsBindingContext.get(this)!
73
-
74
- return resolveYjsPath(ctx.yjsObject, path)
75
- }
76
-
77
- /**
78
- * The Yjs.Text object represented by this mobx-keystone node.
79
- */
80
- @computed
81
- get yjsText(): Y.Text {
82
- const yjsObject = this._yjsObjectAtPath
83
-
84
- if (!(yjsObject instanceof Y.Text)) {
85
- throw failure(`Y.Text was expected at path ${JSON.stringify(this._yjsObjectPath)}`)
86
- }
87
-
88
- return yjsObject
89
- }
90
-
91
- /**
92
- * Atom that gets changed when the associated Y.js text changes.
93
- */
94
- yjsTextChangedAtom = createAtom("yjsTextChangedAtom")
95
-
96
- /**
97
- * The text value of the Yjs.Text object.
98
- * Shortcut for `yjsText.toString()`, but computed.
99
- */
100
- @computed
101
- get text(): string {
102
- this.yjsTextChangedAtom.reportObserved()
103
- return this.yjsText.toString()
104
- }
105
-
106
- protected onInit() {
107
- const shouldReplicateToYjs = (ctx: YjsBindingContext | undefined): ctx is YjsBindingContext => {
108
- return !!ctx && !!ctx.boundObject && !ctx.isApplyingYjsChangesToMobxKeystone
109
- }
110
-
111
- let reapplyDeltasToYjsText = false
112
- const newDeltas: Frozen<unknown[]>[] = []
113
-
114
- let disposeObserveDeltaList: (() => void) | undefined
115
-
116
- const disposeReactionToDeltaListRefChange = reaction(
117
- () => this.$.deltaList,
118
- (deltaList) => {
119
- disposeObserveDeltaList?.()
120
- disposeObserveDeltaList = undefined
121
-
122
- if (deltaList) {
123
- disposeObserveDeltaList = observe(this.$.deltaList, (change) => {
124
- if (reapplyDeltasToYjsText) {
125
- // already gonna replace them all
126
- return
127
- }
128
- if (!shouldReplicateToYjs(yjsBindingContext.get(this))) {
129
- // yjs text is already up to date with these changes
130
- return
131
- }
132
-
133
- if (
134
- change.type === "splice" &&
135
- change.removedCount === 0 &&
136
- change.addedCount > 0 &&
137
- change.index === this.deltaList.length
138
- ) {
139
- // optimization, just adding new ones to the end
140
- newDeltas.push(...change.added)
141
- } else {
142
- // any other change, we need to reapply all deltas
143
- reapplyDeltasToYjsText = true
144
- }
145
- })
146
- }
147
- },
148
- { fireImmediately: true }
149
- )
150
-
151
- const disposeOnSnapshot = onSnapshot(this, () => {
152
- try {
153
- if (reapplyDeltasToYjsText) {
154
- const ctx = yjsBindingContext.get(this)
155
-
156
- if (shouldReplicateToYjs(ctx)) {
157
- const { yjsText } = this
158
-
159
- ctx.yjsDoc.transact(() => {
160
- // didn't find a better way than this to reapply all deltas
161
- // without having to re-create the Y.Text object
162
- if (yjsText.length > 0) {
163
- yjsText.delete(0, yjsText.length)
164
- }
165
-
166
- this.deltaList.forEach((frozenDeltas) => {
167
- yjsText.applyDelta(frozenDeltas.data)
168
- })
169
- }, ctx.yjsOrigin)
170
- }
171
- } else if (newDeltas.length > 0) {
172
- const ctx = yjsBindingContext.get(this)
173
-
174
- if (shouldReplicateToYjs(ctx)) {
175
- const { yjsText } = this
176
-
177
- ctx.yjsDoc.transact(() => {
178
- newDeltas.forEach((frozenDeltas) => {
179
- yjsText.applyDelta(frozenDeltas.data)
180
- })
181
- }, ctx.yjsOrigin)
182
- }
183
- }
184
- } finally {
185
- reapplyDeltasToYjsText = false
186
- newDeltas.length = 0
187
- }
188
- })
189
-
190
- const diposeYjsTextChangedAtom = hookYjsTextChangedAtom(
191
- () => this.yjsText,
192
- this.yjsTextChangedAtom
193
- )
194
-
195
- return () => {
196
- disposeOnSnapshot()
197
- disposeReactionToDeltaListRefChange()
198
- disposeObserveDeltaList?.()
199
- disposeObserveDeltaList = undefined
200
-
201
- diposeYjsTextChangedAtom()
202
- }
203
- }
204
- }
205
-
206
- // we use this trick just to avoid a babel bug that causes classes used inside classes not to be overriden
207
- // by the decorator
208
- const DecoratedYjsTextModel = YjsTextModel
209
-
210
- function hookYjsTextChangedAtom(getYjsText: () => Y.Text, textChangedAtom: IAtom) {
211
- let disposeObserveYjsText: (() => void) | undefined
212
-
213
- const observeFn = () => {
214
- textChangedAtom.reportChanged()
215
- }
216
-
217
- const disposeReactionToYTextChange = reaction(
218
- () => {
219
- try {
220
- return getYjsText()
221
- } catch {
222
- return undefined
223
- }
224
- },
225
- (yjsText) => {
226
- disposeObserveYjsText?.()
227
- disposeObserveYjsText = undefined
228
-
229
- if (yjsText) {
230
- yjsText.observe(observeFn)
231
-
232
- disposeObserveYjsText = () => {
233
- yjsText.unobserve(observeFn)
234
- }
235
- }
236
-
237
- textChangedAtom.reportChanged()
238
- },
239
- {
240
- fireImmediately: true,
241
- }
242
- )
243
-
244
- return () => {
245
- disposeReactionToYTextChange()
246
- disposeObserveYjsText?.()
247
- disposeObserveYjsText = undefined
248
- }
249
- }
1
+ import { IAtom, computed, createAtom, observe, reaction } from "mobx"
2
+ import {
3
+ Frozen,
4
+ Model,
5
+ frozen,
6
+ getParentToChildPath,
7
+ model,
8
+ onSnapshot,
9
+ tProp,
10
+ types,
11
+ } from "mobx-keystone"
12
+ import * as Y from "yjs"
13
+ import { failure } from "../utils/error"
14
+ import { YjsBindingContext, yjsBindingContext } from "./yjsBindingContext"
15
+ import { resolveYjsPath } from "./resolveYjsPath"
16
+
17
+ // Delta[][], since each single change is a Delta[]
18
+ // we use frozen so that we can reuse each delta change
19
+ const deltaListType = types.array(types.frozen(types.unchecked<unknown[]>()))
20
+
21
+ export const yjsTextModelId = "mobx-keystone-yjs/YjsTextModel"
22
+
23
+ /**
24
+ * A mobx-keystone model that represents a Yjs.Text object.
25
+ */
26
+ @model(yjsTextModelId)
27
+ export class YjsTextModel extends Model({
28
+ deltaList: tProp(deltaListType, () => []),
29
+ }) {
30
+ /**
31
+ * Helper function to create a YjsTextModel instance with a simple text.
32
+ */
33
+ static withText(text: string): YjsTextModel {
34
+ return new DecoratedYjsTextModel({
35
+ deltaList: [
36
+ frozen([
37
+ {
38
+ insert: text,
39
+ },
40
+ ]),
41
+ ],
42
+ })
43
+ }
44
+
45
+ /**
46
+ * The Y.js path from the bound object to the YjsTextModel instance.
47
+ */
48
+ @computed
49
+ private get _yjsObjectPath() {
50
+ const ctx = yjsBindingContext.get(this)
51
+ if (ctx?.boundObject == null) {
52
+ throw failure(
53
+ "the YjsTextModel instance must be part of a bound object before it can be accessed"
54
+ )
55
+ }
56
+
57
+ const path = getParentToChildPath(ctx.boundObject, this)
58
+ if (!path) {
59
+ throw failure("a path from the bound object to the YjsTextModel instance is not available")
60
+ }
61
+
62
+ return path
63
+ }
64
+
65
+ /**
66
+ * The Yjs.Text object present at this mobx-keystone node's path.
67
+ */
68
+ @computed
69
+ private get _yjsObjectAtPath(): unknown {
70
+ const path = this._yjsObjectPath
71
+
72
+ const ctx = yjsBindingContext.get(this)!
73
+
74
+ return resolveYjsPath(ctx.yjsObject, path)
75
+ }
76
+
77
+ /**
78
+ * The Yjs.Text object represented by this mobx-keystone node.
79
+ */
80
+ @computed
81
+ get yjsText(): Y.Text {
82
+ const yjsObject = this._yjsObjectAtPath
83
+
84
+ if (!(yjsObject instanceof Y.Text)) {
85
+ throw failure(`Y.Text was expected at path ${JSON.stringify(this._yjsObjectPath)}`)
86
+ }
87
+
88
+ return yjsObject
89
+ }
90
+
91
+ /**
92
+ * Atom that gets changed when the associated Y.js text changes.
93
+ */
94
+ yjsTextChangedAtom = createAtom("yjsTextChangedAtom")
95
+
96
+ /**
97
+ * The text value of the Yjs.Text object.
98
+ * Shortcut for `yjsText.toString()`, but computed.
99
+ */
100
+ @computed
101
+ get text(): string {
102
+ this.yjsTextChangedAtom.reportObserved()
103
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
104
+ return this.yjsText.toString()
105
+ }
106
+
107
+ protected onInit() {
108
+ const shouldReplicateToYjs = (ctx: YjsBindingContext | undefined): ctx is YjsBindingContext => {
109
+ return !!ctx && !!ctx.boundObject && !ctx.isApplyingYjsChangesToMobxKeystone
110
+ }
111
+
112
+ let reapplyDeltasToYjsText = false
113
+ const newDeltas: Frozen<unknown[]>[] = []
114
+
115
+ let disposeObserveDeltaList: (() => void) | undefined
116
+
117
+ const disposeReactionToDeltaListRefChange = reaction(
118
+ () => this.$.deltaList,
119
+ (deltaList) => {
120
+ disposeObserveDeltaList?.()
121
+ disposeObserveDeltaList = undefined
122
+
123
+ disposeObserveDeltaList = observe(deltaList, (change) => {
124
+ if (reapplyDeltasToYjsText) {
125
+ // already gonna replace them all
126
+ return
127
+ }
128
+ if (!shouldReplicateToYjs(yjsBindingContext.get(this))) {
129
+ // yjs text is already up to date with these changes
130
+ return
131
+ }
132
+
133
+ if (
134
+ change.type === "splice" &&
135
+ change.removedCount === 0 &&
136
+ change.addedCount > 0 &&
137
+ change.index === this.deltaList.length
138
+ ) {
139
+ // optimization, just adding new ones to the end
140
+ newDeltas.push(...change.added)
141
+ } else {
142
+ // any other change, we need to reapply all deltas
143
+ reapplyDeltasToYjsText = true
144
+ }
145
+ })
146
+ },
147
+ { fireImmediately: true }
148
+ )
149
+
150
+ const disposeOnSnapshot = onSnapshot(this, () => {
151
+ try {
152
+ if (reapplyDeltasToYjsText) {
153
+ const ctx = yjsBindingContext.get(this)
154
+
155
+ if (shouldReplicateToYjs(ctx)) {
156
+ const { yjsText } = this
157
+
158
+ ctx.yjsDoc.transact(() => {
159
+ // didn't find a better way than this to reapply all deltas
160
+ // without having to re-create the Y.Text object
161
+ if (yjsText.length > 0) {
162
+ yjsText.delete(0, yjsText.length)
163
+ }
164
+
165
+ this.deltaList.forEach((frozenDeltas) => {
166
+ yjsText.applyDelta(frozenDeltas.data)
167
+ })
168
+ }, ctx.yjsOrigin)
169
+ }
170
+ } else if (newDeltas.length > 0) {
171
+ const ctx = yjsBindingContext.get(this)
172
+
173
+ if (shouldReplicateToYjs(ctx)) {
174
+ const { yjsText } = this
175
+
176
+ ctx.yjsDoc.transact(() => {
177
+ newDeltas.forEach((frozenDeltas) => {
178
+ yjsText.applyDelta(frozenDeltas.data)
179
+ })
180
+ }, ctx.yjsOrigin)
181
+ }
182
+ }
183
+ } finally {
184
+ reapplyDeltasToYjsText = false
185
+ newDeltas.length = 0
186
+ }
187
+ })
188
+
189
+ const diposeYjsTextChangedAtom = hookYjsTextChangedAtom(
190
+ () => this.yjsText,
191
+ this.yjsTextChangedAtom
192
+ )
193
+
194
+ return () => {
195
+ disposeOnSnapshot()
196
+ disposeReactionToDeltaListRefChange()
197
+ disposeObserveDeltaList?.()
198
+ disposeObserveDeltaList = undefined
199
+
200
+ diposeYjsTextChangedAtom()
201
+ }
202
+ }
203
+ }
204
+
205
+ // we use this trick just to avoid a babel bug that causes classes used inside classes not to be overriden
206
+ // by the decorator
207
+ const DecoratedYjsTextModel = YjsTextModel
208
+
209
+ function hookYjsTextChangedAtom(getYjsText: () => Y.Text, textChangedAtom: IAtom) {
210
+ let disposeObserveYjsText: (() => void) | undefined
211
+
212
+ const observeFn = () => {
213
+ textChangedAtom.reportChanged()
214
+ }
215
+
216
+ const disposeReactionToYTextChange = reaction(
217
+ () => {
218
+ try {
219
+ return getYjsText()
220
+ } catch {
221
+ return undefined
222
+ }
223
+ },
224
+ (yjsText) => {
225
+ disposeObserveYjsText?.()
226
+ disposeObserveYjsText = undefined
227
+
228
+ if (yjsText) {
229
+ yjsText.observe(observeFn)
230
+
231
+ disposeObserveYjsText = () => {
232
+ yjsText.unobserve(observeFn)
233
+ }
234
+ }
235
+
236
+ textChangedAtom.reportChanged()
237
+ },
238
+ {
239
+ fireImmediately: true,
240
+ }
241
+ )
242
+
243
+ return () => {
244
+ disposeReactionToYTextChange()
245
+ disposeObserveYjsText?.()
246
+ disposeObserveYjsText = undefined
247
+ }
248
+ }