@y/y 14.0.0-rc.2 → 14.0.0-rc.21

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 (166) hide show
  1. package/README.md +36 -12
  2. package/dist/src/index.d.ts +24 -1
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/structs/AbstractStruct.d.ts +14 -17
  5. package/dist/src/structs/AbstractStruct.d.ts.map +1 -1
  6. package/dist/src/structs/GC.d.ts +9 -14
  7. package/dist/src/structs/GC.d.ts.map +1 -1
  8. package/dist/src/structs/Item.d.ts +569 -31
  9. package/dist/src/structs/Item.d.ts.map +1 -1
  10. package/dist/src/structs/Skip.d.ts +9 -11
  11. package/dist/src/structs/Skip.d.ts.map +1 -1
  12. package/dist/src/utils/BlockSet.d.ts +8 -7
  13. package/dist/src/utils/BlockSet.d.ts.map +1 -1
  14. package/dist/src/utils/Doc.d.ts +4 -9
  15. package/dist/src/utils/Doc.d.ts.map +1 -1
  16. package/dist/src/utils/ID.d.ts +0 -1
  17. package/dist/src/utils/ID.d.ts.map +1 -1
  18. package/dist/src/utils/RelativePosition.d.ts +2 -5
  19. package/dist/src/utils/RelativePosition.d.ts.map +1 -1
  20. package/dist/src/utils/Renderer.d.ts +144 -0
  21. package/dist/src/utils/Renderer.d.ts.map +1 -0
  22. package/dist/src/utils/Snapshot.d.ts +7 -11
  23. package/dist/src/utils/Snapshot.d.ts.map +1 -1
  24. package/dist/src/utils/StructStore.d.ts +45 -23
  25. package/dist/src/utils/StructStore.d.ts.map +1 -1
  26. package/dist/src/utils/Transaction.d.ts +11 -21
  27. package/dist/src/utils/Transaction.d.ts.map +1 -1
  28. package/dist/src/utils/UndoManager.d.ts +13 -14
  29. package/dist/src/utils/UndoManager.d.ts.map +1 -1
  30. package/dist/src/utils/UpdateDecoder.d.ts +1 -1
  31. package/dist/src/utils/UpdateDecoder.d.ts.map +1 -1
  32. package/dist/src/utils/UpdateEncoder.d.ts +0 -1
  33. package/dist/src/utils/UpdateEncoder.d.ts.map +1 -1
  34. package/dist/src/utils/YEvent.d.ts +22 -26
  35. package/dist/src/utils/YEvent.d.ts.map +1 -1
  36. package/dist/src/utils/content-helper.d.ts +2 -0
  37. package/dist/src/utils/content-helper.d.ts.map +1 -0
  38. package/dist/src/utils/delta-helpers.d.ts +2 -3
  39. package/dist/src/utils/delta-helpers.d.ts.map +1 -1
  40. package/dist/src/utils/encoding-helpers.d.ts +6 -0
  41. package/dist/src/utils/encoding-helpers.d.ts.map +1 -0
  42. package/dist/src/utils/encoding.d.ts +16 -18
  43. package/dist/src/utils/encoding.d.ts.map +1 -1
  44. package/dist/src/utils/ids.d.ts +331 -0
  45. package/dist/src/utils/ids.d.ts.map +1 -0
  46. package/dist/src/utils/isParentOf.d.ts +1 -2
  47. package/dist/src/utils/isParentOf.d.ts.map +1 -1
  48. package/dist/src/utils/logging.d.ts +0 -1
  49. package/dist/src/utils/logging.d.ts.map +1 -1
  50. package/dist/src/utils/meta.d.ts +15 -64
  51. package/dist/src/utils/meta.d.ts.map +1 -1
  52. package/dist/src/utils/renderer-helpers.d.ts +99 -0
  53. package/dist/src/utils/renderer-helpers.d.ts.map +1 -0
  54. package/dist/src/utils/schemas.d.ts +3 -0
  55. package/dist/src/utils/schemas.d.ts.map +1 -0
  56. package/dist/src/utils/transaction-helpers.d.ts +18 -0
  57. package/dist/src/utils/transaction-helpers.d.ts.map +1 -0
  58. package/dist/src/utils/updates.d.ts +19 -25
  59. package/dist/src/utils/updates.d.ts.map +1 -1
  60. package/dist/src/ytype.d.ts +144 -41
  61. package/dist/src/ytype.d.ts.map +1 -1
  62. package/global.d.ts +53 -0
  63. package/package.json +12 -16
  64. package/src/index.js +32 -124
  65. package/src/structs/AbstractStruct.js +21 -16
  66. package/src/structs/GC.js +15 -20
  67. package/src/structs/Item.js +992 -318
  68. package/src/structs/Skip.js +16 -19
  69. package/src/utils/BlockSet.js +20 -20
  70. package/src/utils/Doc.js +18 -29
  71. package/src/utils/ID.js +0 -2
  72. package/src/utils/RelativePosition.js +15 -25
  73. package/src/utils/{AttributionManager.js → Renderer.js} +42 -197
  74. package/src/utils/Snapshot.js +15 -37
  75. package/src/utils/StructStore.js +89 -227
  76. package/src/utils/Transaction.js +89 -321
  77. package/src/utils/UndoManager.js +157 -19
  78. package/src/utils/UpdateDecoder.js +2 -3
  79. package/src/utils/UpdateEncoder.js +0 -4
  80. package/src/utils/YEvent.js +20 -26
  81. package/src/utils/content-helper.js +0 -0
  82. package/src/utils/delta-helpers.js +21 -42
  83. package/src/utils/encoding-helpers.js +110 -0
  84. package/src/utils/encoding.js +197 -122
  85. package/src/utils/ids.js +1527 -0
  86. package/src/utils/isParentOf.js +2 -4
  87. package/src/utils/logging.js +0 -4
  88. package/src/utils/meta.js +57 -46
  89. package/src/utils/renderer-helpers.js +110 -0
  90. package/src/utils/schemas.js +3 -0
  91. package/src/utils/transaction-helpers.js +413 -0
  92. package/src/utils/updates.js +24 -146
  93. package/src/ytype.js +626 -255
  94. package/tests/testHelper.js +10 -12
  95. package/dist/src/internals.d.ts +0 -36
  96. package/dist/src/internals.d.ts.map +0 -1
  97. package/dist/src/structs/ContentAny.d.ts +0 -67
  98. package/dist/src/structs/ContentAny.d.ts.map +0 -1
  99. package/dist/src/structs/ContentBinary.d.ts +0 -64
  100. package/dist/src/structs/ContentBinary.d.ts.map +0 -1
  101. package/dist/src/structs/ContentDeleted.d.ts +0 -64
  102. package/dist/src/structs/ContentDeleted.d.ts.map +0 -1
  103. package/dist/src/structs/ContentDoc.d.ts +0 -72
  104. package/dist/src/structs/ContentDoc.d.ts.map +0 -1
  105. package/dist/src/structs/ContentEmbed.d.ts +0 -67
  106. package/dist/src/structs/ContentEmbed.d.ts.map +0 -1
  107. package/dist/src/structs/ContentFormat.d.ts +0 -69
  108. package/dist/src/structs/ContentFormat.d.ts.map +0 -1
  109. package/dist/src/structs/ContentJSON.d.ts +0 -70
  110. package/dist/src/structs/ContentJSON.d.ts.map +0 -1
  111. package/dist/src/structs/ContentString.d.ts +0 -70
  112. package/dist/src/structs/ContentString.d.ts.map +0 -1
  113. package/dist/src/structs/ContentType.d.ts +0 -77
  114. package/dist/src/structs/ContentType.d.ts.map +0 -1
  115. package/dist/src/utils/AttributionManager.d.ts +0 -238
  116. package/dist/src/utils/AttributionManager.d.ts.map +0 -1
  117. package/dist/src/utils/IdMap.d.ts +0 -164
  118. package/dist/src/utils/IdMap.d.ts.map +0 -1
  119. package/dist/src/utils/IdSet.d.ts +0 -163
  120. package/dist/src/utils/IdSet.d.ts.map +0 -1
  121. package/dist/tests/IdMap.tests.d.ts +0 -9
  122. package/dist/tests/IdMap.tests.d.ts.map +0 -1
  123. package/dist/tests/IdSet.tests.d.ts +0 -9
  124. package/dist/tests/IdSet.tests.d.ts.map +0 -1
  125. package/dist/tests/attribution.tests.d.ts +0 -9
  126. package/dist/tests/attribution.tests.d.ts.map +0 -1
  127. package/dist/tests/compatibility.tests.d.ts +0 -5
  128. package/dist/tests/compatibility.tests.d.ts.map +0 -1
  129. package/dist/tests/delta.tests.d.ts +0 -7
  130. package/dist/tests/delta.tests.d.ts.map +0 -1
  131. package/dist/tests/doc.tests.d.ts +0 -13
  132. package/dist/tests/doc.tests.d.ts.map +0 -1
  133. package/dist/tests/encoding.tests.d.ts +0 -5
  134. package/dist/tests/encoding.tests.d.ts.map +0 -1
  135. package/dist/tests/index.d.ts +0 -2
  136. package/dist/tests/index.d.ts.map +0 -1
  137. package/dist/tests/relativePositions.tests.d.ts +0 -11
  138. package/dist/tests/relativePositions.tests.d.ts.map +0 -1
  139. package/dist/tests/snapshot.tests.d.ts +0 -14
  140. package/dist/tests/snapshot.tests.d.ts.map +0 -1
  141. package/dist/tests/testHelper.d.ts +0 -171
  142. package/dist/tests/testHelper.d.ts.map +0 -1
  143. package/dist/tests/undo-redo.tests.d.ts +0 -27
  144. package/dist/tests/undo-redo.tests.d.ts.map +0 -1
  145. package/dist/tests/updates.tests.d.ts +0 -26
  146. package/dist/tests/updates.tests.d.ts.map +0 -1
  147. package/dist/tests/y-array.tests.d.ts +0 -43
  148. package/dist/tests/y-array.tests.d.ts.map +0 -1
  149. package/dist/tests/y-map.tests.d.ts +0 -42
  150. package/dist/tests/y-map.tests.d.ts.map +0 -1
  151. package/dist/tests/y-text.tests.d.ts +0 -49
  152. package/dist/tests/y-text.tests.d.ts.map +0 -1
  153. package/dist/tests/y-xml.tests.d.ts +0 -14
  154. package/dist/tests/y-xml.tests.d.ts.map +0 -1
  155. package/src/internals.js +0 -35
  156. package/src/structs/ContentAny.js +0 -115
  157. package/src/structs/ContentBinary.js +0 -93
  158. package/src/structs/ContentDeleted.js +0 -101
  159. package/src/structs/ContentDoc.js +0 -141
  160. package/src/structs/ContentEmbed.js +0 -98
  161. package/src/structs/ContentFormat.js +0 -105
  162. package/src/structs/ContentJSON.js +0 -119
  163. package/src/structs/ContentString.js +0 -113
  164. package/src/structs/ContentType.js +0 -152
  165. package/src/utils/IdMap.js +0 -673
  166. package/src/utils/IdSet.js +0 -825
@@ -1,21 +1,12 @@
1
- import {
2
- GC,
3
- splitItem,
4
- createDeleteSetFromStructStore,
5
- createIdSet,
6
- Transaction, ID, Item, // eslint-disable-line
7
- Skip,
8
- createID,
9
- splitStruct
10
- } from '../internals.js'
11
-
12
- import * as math from 'lib0/math'
13
- import * as error from 'lib0/error'
1
+ import { Skip } from '../structs/Skip.js'
2
+ import { createID } from './ID.js'
3
+ import { createDeleteSetFromStructStore, createIdSet } from './ids.js'
4
+ import { findIndexSS } from './transaction-helpers.js'
14
5
 
15
6
  export class StructStore {
16
7
  constructor () {
17
8
  /**
18
- * @type {Map<number,Array<GC|Item>>}
9
+ * @type {Map<number,Array<GC|Item|Skip>>}
19
10
  */
20
11
  this.clients = new Map()
21
12
  // this.ds = new IdSet()
@@ -33,6 +24,90 @@ export class StructStore {
33
24
  get ds () {
34
25
  return createDeleteSetFromStructStore(this)
35
26
  }
27
+
28
+ /**
29
+ * @param {GC|Item|Skip} struct
30
+ * @function
31
+ */
32
+ add (struct) {
33
+ let structs = this.clients.get(struct.id.client)
34
+ if (structs === undefined) {
35
+ structs = []
36
+ this.clients.set(struct.id.client, structs)
37
+ } else {
38
+ const lastStruct = structs[structs.length - 1]
39
+ if (lastStruct.id.clock + lastStruct.length !== struct.id.clock) {
40
+ // this replaces an integrated skip
41
+ let index = findIndexSS(structs, struct.id.clock)
42
+ const skip = structs[index]
43
+ const diffStart = struct.id.clock - skip.id.clock
44
+ const diffEnd = skip.id.clock + skip.length - struct.id.clock - struct.length
45
+ if (diffStart > 0) {
46
+ structs.splice(index++, 0, new Skip(createID(struct.id.client, skip.id.clock), diffStart))
47
+ }
48
+ if (diffEnd > 0) {
49
+ structs.splice(index + 1, 0, new Skip(createID(struct.id.client, struct.id.clock + struct.length), diffEnd))
50
+ }
51
+ structs[index] = struct
52
+ this.skips.delete(struct.id.client, struct.id.clock, struct.length)
53
+ return
54
+ }
55
+ }
56
+ structs.push(struct)
57
+ }
58
+
59
+ /**
60
+ * Expects that id is actually in store. This function throws or is an infinite loop otherwise.
61
+ *
62
+ * @param {ID} id
63
+ * @return {GC|Item}
64
+ */
65
+ get (id) {
66
+ const structs = /** @type {Array<GC|Item>} */ (this.clients.get(id.client))
67
+ return structs[findIndexSS(structs, id.clock)]
68
+ }
69
+
70
+ /**
71
+ * Expects that id is actually in store. This function throws or is an infinite loop otherwise.
72
+ *
73
+ * @param {ID} id
74
+ * @return {Item}
75
+ */
76
+ getItem (id) {
77
+ const structs = /** @type {Array<GC|Item>} */ (this.clients.get(id.client))
78
+ return /** @type {Item} */ (structs[findIndexSS(structs, id.clock)])
79
+ }
80
+
81
+ /**
82
+ * Get the next expected clock for a specific client.
83
+ *
84
+ * @param {number} client
85
+ * @return {number}
86
+ *
87
+ * @public
88
+ * @function
89
+ */
90
+ getClock (client) {
91
+ const structs = this.clients.get(client)
92
+ if (structs === undefined) {
93
+ return 0
94
+ }
95
+ const lastStruct = structs[structs.length - 1]
96
+ return lastStruct.id.clock + lastStruct.length
97
+ }
98
+
99
+ /**
100
+ * Perform a binary search on a sorted array
101
+ * @param {ID} id
102
+ * @return {{ structs: Array<GC|Item|Skip>, index: number }}
103
+ *
104
+ * @function
105
+ */
106
+ getIndex (id) {
107
+ const structs = this.clients.get(id.client) || []
108
+ const index = findIndexSS(structs, id.clock)
109
+ return { structs, index }
110
+ }
36
111
  }
37
112
 
38
113
  /**
@@ -57,23 +132,6 @@ export const getStateVector = store => {
57
132
  return sm
58
133
  }
59
134
 
60
- /**
61
- * @param {StructStore} store
62
- * @param {number} client
63
- * @return {number}
64
- *
65
- * @public
66
- * @function
67
- */
68
- export const getState = (store, client) => {
69
- const structs = store.clients.get(client)
70
- if (structs === undefined) {
71
- return 0
72
- }
73
- const lastStruct = structs[structs.length - 1]
74
- return lastStruct.id.clock + lastStruct.length
75
- }
76
-
77
135
  /**
78
136
  * @param {StructStore} store
79
137
  *
@@ -91,199 +149,3 @@ export const integrityCheck = store => {
91
149
  }
92
150
  })
93
151
  }
94
-
95
- /**
96
- * @param {StructStore} store
97
- * @param {GC|Item} struct
98
- *
99
- * @private
100
- * @function
101
- */
102
- export const addStruct = (store, struct) => {
103
- let structs = store.clients.get(struct.id.client)
104
- if (structs === undefined) {
105
- structs = []
106
- store.clients.set(struct.id.client, structs)
107
- } else {
108
- const lastStruct = structs[structs.length - 1]
109
- if (lastStruct.id.clock + lastStruct.length !== struct.id.clock) {
110
- // this replaces an integrated skip
111
- let index = findIndexSS(structs, struct.id.clock)
112
- const skip = structs[index]
113
- const diffStart = struct.id.clock - skip.id.clock
114
- const diffEnd = skip.id.clock + skip.length - struct.id.clock - struct.length
115
- if (diffStart > 0) {
116
- structs.splice(index++, 0, new Skip(createID(struct.id.client, skip.id.clock), diffStart))
117
- }
118
- if (diffEnd > 0) {
119
- structs.splice(index + 1, 0, new Skip(createID(struct.id.client, struct.id.clock + struct.length), diffEnd))
120
- }
121
- structs[index] = struct
122
- store.skips.delete(struct.id.client, struct.id.clock, struct.length)
123
- return
124
- }
125
- }
126
- structs.push(struct)
127
- }
128
-
129
- /**
130
- * Perform a binary search on a sorted array
131
- * @param {Array<Item|GC>} structs
132
- * @param {number} clock
133
- * @return {number}
134
- *
135
- * @private
136
- * @function
137
- */
138
- export const findIndexSS = (structs, clock) => {
139
- let left = 0
140
- let right = structs.length - 1
141
- let mid = structs[right]
142
- let midclock = mid.id.clock
143
- if (midclock === clock) {
144
- return right
145
- }
146
- // @todo does it even make sense to pivot the search?
147
- // If a good split misses, it might actually increase the time to find the correct item.
148
- // Currently, the only advantage is that search with pivoting might find the item on the first try.
149
- let midindex = math.floor((clock / (midclock + mid.length - 1)) * right) // pivoting the search
150
- while (left <= right) {
151
- mid = structs[midindex]
152
- midclock = mid.id.clock
153
- if (midclock <= clock) {
154
- if (clock < midclock + mid.length) {
155
- return midindex
156
- }
157
- left = midindex + 1
158
- } else {
159
- right = midindex - 1
160
- }
161
- midindex = math.floor((left + right) / 2)
162
- }
163
- // Always check state before looking for a struct in StructStore
164
- // Therefore the case of not finding a struct is unexpected
165
- throw error.unexpectedCase()
166
- }
167
-
168
- /**
169
- * Expects that id is actually in store. This function throws or is an infinite loop otherwise.
170
- *
171
- * @param {StructStore} store
172
- * @param {ID} id
173
- * @return {GC|Item}
174
- *
175
- * @private
176
- * @function
177
- */
178
- export const find = (store, id) => {
179
- /**
180
- * @type {Array<GC|Item>}
181
- */
182
- // @ts-ignore
183
- const structs = store.clients.get(id.client)
184
- return structs[findIndexSS(structs, id.clock)]
185
- }
186
-
187
- /**
188
- * Expects that id is actually in store. This function throws or is an infinite loop otherwise.
189
- * @private
190
- * @function
191
- */
192
- export const getItem = /** @type {function(StructStore,ID):Item} */ (find)
193
-
194
- /**
195
- * @param {Transaction?} transaction
196
- * @param {Array<Item|GC>} structs
197
- * @param {number} clock
198
- */
199
- export const findIndexCleanStart = (transaction, structs, clock) => {
200
- const index = findIndexSS(structs, clock)
201
- const struct = structs[index]
202
- if (struct.id.clock < clock) {
203
- structs.splice(index + 1, 0, splitStruct(transaction, struct, clock - struct.id.clock))
204
- return index + 1
205
- }
206
- return index
207
- }
208
-
209
- /**
210
- * Expects that id is actually in store. This function throws or is an infinite loop otherwise.
211
- *
212
- * @param {Transaction} transaction
213
- * @param {ID} id
214
- * @return {Item}
215
- *
216
- * @private
217
- * @function
218
- */
219
- export const getItemCleanStart = (transaction, id) => {
220
- const structs = /** @type {Array<Item>} */ (transaction.doc.store.clients.get(id.client))
221
- return structs[findIndexCleanStart(transaction, structs, id.clock)]
222
- }
223
-
224
- /**
225
- * Expects that id is actually in store. This function throws or is an infinite loop otherwise.
226
- *
227
- * @param {Transaction} transaction
228
- * @param {StructStore} store
229
- * @param {ID} id
230
- * @return {Item}
231
- *
232
- * @private
233
- * @function
234
- */
235
- export const getItemCleanEnd = (transaction, store, id) => {
236
- /**
237
- * @type {Array<Item>}
238
- */
239
- // @ts-ignore
240
- const structs = store.clients.get(id.client)
241
- const index = findIndexSS(structs, id.clock)
242
- const struct = structs[index]
243
- if (id.clock !== struct.id.clock + struct.length - 1 && struct.constructor !== GC) {
244
- structs.splice(index + 1, 0, splitItem(transaction, struct, id.clock - struct.id.clock + 1))
245
- }
246
- return struct
247
- }
248
-
249
- /**
250
- * Replace `item` with `newitem` in store
251
- * @param {Transaction} tr
252
- * @param {GC|Item} struct
253
- * @param {GC|Item} newStruct
254
- *
255
- * @private
256
- * @function
257
- */
258
- export const replaceStruct = (tr, struct, newStruct) => {
259
- const structs = /** @type {Array<GC|Item>} */ (tr.doc.store.clients.get(struct.id.client))
260
- structs[findIndexSS(structs, struct.id.clock)] = newStruct
261
- tr._mergeStructs.push(newStruct)
262
- }
263
-
264
- /**
265
- * Iterate over a range of structs
266
- *
267
- * @param {Transaction} transaction
268
- * @param {Array<Item|GC>} structs
269
- * @param {number} clockStart Inclusive start
270
- * @param {number} len
271
- * @param {function(GC|Item):void} f
272
- *
273
- * @function
274
- */
275
- export const iterateStructs = (transaction, structs, clockStart, len, f) => {
276
- if (len === 0) {
277
- return
278
- }
279
- const clockEnd = clockStart + len
280
- let index = findIndexCleanStart(transaction, structs, clockStart)
281
- let struct
282
- do {
283
- struct = structs[index++]
284
- if (clockEnd < struct.id.clock + struct.length) {
285
- findIndexCleanStart(transaction, structs, clockEnd)
286
- }
287
- f(struct)
288
- } while (index < structs.length && structs[index].id.clock < clockEnd)
289
- }