@y/y 14.0.0-19 → 14.0.0-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 (135) hide show
  1. package/README.md +7 -5
  2. package/dist/src/index.d.ts +2 -1
  3. package/dist/src/internals.d.ts +2 -9
  4. package/dist/src/structs/ContentType.d.ts +6 -12
  5. package/dist/src/structs/ContentType.d.ts.map +1 -1
  6. package/dist/src/structs/Item.d.ts +5 -6
  7. package/dist/src/structs/Item.d.ts.map +1 -1
  8. package/dist/src/utils/AttributionManager.d.ts +16 -14
  9. package/dist/src/utils/AttributionManager.d.ts.map +1 -1
  10. package/dist/src/utils/Doc.d.ts +7 -70
  11. package/dist/src/utils/Doc.d.ts.map +1 -1
  12. package/dist/src/utils/ID.d.ts +2 -2
  13. package/dist/src/utils/ID.d.ts.map +1 -1
  14. package/dist/src/utils/IdMap.d.ts +22 -19
  15. package/dist/src/utils/IdMap.d.ts.map +1 -1
  16. package/dist/src/utils/IdSet.d.ts +6 -6
  17. package/dist/src/utils/IdSet.d.ts.map +1 -1
  18. package/dist/src/utils/RelativePosition.d.ts +8 -8
  19. package/dist/src/utils/RelativePosition.d.ts.map +1 -1
  20. package/dist/src/utils/Snapshot.d.ts +3 -3
  21. package/dist/src/utils/Snapshot.d.ts.map +1 -1
  22. package/dist/src/utils/Transaction.d.ts +9 -5
  23. package/dist/src/utils/Transaction.d.ts.map +1 -1
  24. package/dist/src/utils/UndoManager.d.ts +14 -12
  25. package/dist/src/utils/UndoManager.d.ts.map +1 -1
  26. package/dist/src/utils/UpdateDecoder.d.ts +8 -4
  27. package/dist/src/utils/UpdateDecoder.d.ts.map +1 -1
  28. package/dist/src/utils/UpdateEncoder.d.ts +2 -0
  29. package/dist/src/utils/UpdateEncoder.d.ts.map +1 -1
  30. package/dist/src/utils/YEvent.d.ts +21 -42
  31. package/dist/src/utils/YEvent.d.ts.map +1 -1
  32. package/dist/src/utils/encoding.d.ts +3 -3
  33. package/dist/src/utils/encoding.d.ts.map +1 -1
  34. package/dist/src/utils/isParentOf.d.ts +1 -1
  35. package/dist/src/utils/isParentOf.d.ts.map +1 -1
  36. package/dist/src/utils/logging.d.ts +2 -2
  37. package/dist/src/utils/logging.d.ts.map +1 -1
  38. package/dist/src/utils/meta.d.ts +71 -0
  39. package/dist/src/utils/meta.d.ts.map +1 -0
  40. package/dist/src/utils/ts.d.ts +4 -0
  41. package/dist/src/utils/ts.d.ts.map +1 -0
  42. package/dist/src/utils/updates.d.ts +11 -11
  43. package/dist/src/utils/updates.d.ts.map +1 -1
  44. package/dist/src/ytype.d.ts +498 -0
  45. package/dist/src/ytype.d.ts.map +1 -0
  46. package/dist/tests/IdMap.tests.d.ts.map +1 -1
  47. package/dist/tests/attribution.tests.d.ts +1 -0
  48. package/dist/tests/attribution.tests.d.ts.map +1 -1
  49. package/dist/tests/compatibility.tests.d.ts.map +1 -1
  50. package/dist/tests/doc.tests.d.ts.map +1 -1
  51. package/dist/tests/relativePositions.tests.d.ts +9 -9
  52. package/dist/tests/relativePositions.tests.d.ts.map +1 -1
  53. package/dist/tests/snapshot.tests.d.ts.map +1 -1
  54. package/dist/tests/testHelper.d.ts +28 -27
  55. package/dist/tests/testHelper.d.ts.map +1 -1
  56. package/dist/tests/undo-redo.tests.d.ts.map +1 -1
  57. package/dist/tests/updates.tests.d.ts +2 -1
  58. package/dist/tests/updates.tests.d.ts.map +1 -1
  59. package/dist/tests/y-array.tests.d.ts +0 -2
  60. package/dist/tests/y-array.tests.d.ts.map +1 -1
  61. package/dist/tests/y-map.tests.d.ts +0 -3
  62. package/dist/tests/y-map.tests.d.ts.map +1 -1
  63. package/dist/tests/y-text.tests.d.ts +1 -1
  64. package/dist/tests/y-text.tests.d.ts.map +1 -1
  65. package/dist/tests/y-xml.tests.d.ts +0 -1
  66. package/dist/tests/y-xml.tests.d.ts.map +1 -1
  67. package/package.json +16 -16
  68. package/src/index.js +156 -0
  69. package/src/internals.js +35 -0
  70. package/src/structs/AbstractStruct.js +59 -0
  71. package/src/structs/ContentAny.js +115 -0
  72. package/src/structs/ContentBinary.js +93 -0
  73. package/src/structs/ContentDeleted.js +101 -0
  74. package/src/structs/ContentDoc.js +141 -0
  75. package/src/structs/ContentEmbed.js +98 -0
  76. package/src/structs/ContentFormat.js +105 -0
  77. package/src/structs/ContentJSON.js +119 -0
  78. package/src/structs/ContentString.js +113 -0
  79. package/src/structs/ContentType.js +152 -0
  80. package/src/structs/GC.js +80 -0
  81. package/src/structs/Item.js +841 -0
  82. package/src/structs/Skip.js +75 -0
  83. package/src/utils/AttributionManager.js +653 -0
  84. package/src/utils/Doc.js +266 -0
  85. package/src/utils/EventHandler.js +87 -0
  86. package/src/utils/ID.js +89 -0
  87. package/src/utils/IdMap.js +673 -0
  88. package/src/utils/IdSet.js +825 -0
  89. package/src/utils/RelativePosition.js +352 -0
  90. package/src/utils/Snapshot.js +220 -0
  91. package/src/utils/StructSet.js +137 -0
  92. package/src/utils/StructStore.js +289 -0
  93. package/src/utils/Transaction.js +671 -0
  94. package/src/utils/UndoManager.js +406 -0
  95. package/src/utils/UpdateDecoder.js +285 -0
  96. package/src/utils/UpdateEncoder.js +327 -0
  97. package/src/utils/YEvent.js +189 -0
  98. package/src/utils/delta-helpers.js +54 -0
  99. package/src/utils/encoding.js +623 -0
  100. package/src/utils/isParentOf.js +21 -0
  101. package/src/utils/logging.js +21 -0
  102. package/src/utils/meta.js +190 -0
  103. package/src/utils/ts.js +3 -0
  104. package/src/utils/updates.js +802 -0
  105. package/src/ytype.js +1962 -0
  106. package/dist/Skip-CE05BUF8.js +0 -11875
  107. package/dist/Skip-CE05BUF8.js.map +0 -1
  108. package/dist/index-C21sDQ5u.js +0 -163
  109. package/dist/index-C21sDQ5u.js.map +0 -1
  110. package/dist/internals.js +0 -25
  111. package/dist/internals.js.map +0 -1
  112. package/dist/src/types/AbstractType.d.ts +0 -239
  113. package/dist/src/types/AbstractType.d.ts.map +0 -1
  114. package/dist/src/types/YArray.d.ts +0 -128
  115. package/dist/src/types/YArray.d.ts.map +0 -1
  116. package/dist/src/types/YMap.d.ts +0 -112
  117. package/dist/src/types/YMap.d.ts.map +0 -1
  118. package/dist/src/types/YText.d.ts +0 -216
  119. package/dist/src/types/YText.d.ts.map +0 -1
  120. package/dist/src/types/YXmlElement.d.ts +0 -106
  121. package/dist/src/types/YXmlElement.d.ts.map +0 -1
  122. package/dist/src/types/YXmlFragment.d.ts +0 -143
  123. package/dist/src/types/YXmlFragment.d.ts.map +0 -1
  124. package/dist/src/types/YXmlHook.d.ts +0 -32
  125. package/dist/src/types/YXmlHook.d.ts.map +0 -1
  126. package/dist/src/types/YXmlText.d.ts +0 -34
  127. package/dist/src/types/YXmlText.d.ts.map +0 -1
  128. package/dist/src/utils/AbstractConnector.d.ts +0 -20
  129. package/dist/src/utils/AbstractConnector.d.ts.map +0 -1
  130. package/dist/src/utils/types.d.ts +0 -7
  131. package/dist/src/utils/types.d.ts.map +0 -1
  132. package/dist/testHelper.js +0 -617
  133. package/dist/testHelper.js.map +0 -1
  134. package/dist/yjs.js +0 -26
  135. package/dist/yjs.js.map +0 -1
@@ -0,0 +1,289 @@
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'
14
+
15
+ export class StructStore {
16
+ constructor () {
17
+ /**
18
+ * @type {Map<number,Array<GC|Item>>}
19
+ */
20
+ this.clients = new Map()
21
+ // this.ds = new IdSet()
22
+ /**
23
+ * @type {null | { missing: Map<number, number>, update: Uint8Array<ArrayBuffer> }}
24
+ */
25
+ this.pendingStructs = null
26
+ /**
27
+ * @type {null | Uint8Array<ArrayBuffer>}
28
+ */
29
+ this.pendingDs = null
30
+ this.skips = createIdSet()
31
+ }
32
+
33
+ get ds () {
34
+ return createDeleteSetFromStructStore(this)
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Return the states as a Map<client,clock>.
40
+ * Note that clock refers to the next expected clock id.
41
+ *
42
+ * @param {StructStore} store
43
+ * @return {Map<number,number>}
44
+ *
45
+ * @public
46
+ * @function
47
+ */
48
+ export const getStateVector = store => {
49
+ const sm = new Map()
50
+ store.clients.forEach((structs, client) => {
51
+ const struct = structs[structs.length - 1]
52
+ sm.set(client, struct.id.clock + struct.length)
53
+ })
54
+ store.skips.clients.forEach((range, client) => {
55
+ sm.set(client, range.getIds()[0].clock)
56
+ })
57
+ return sm
58
+ }
59
+
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
+ /**
78
+ * @param {StructStore} store
79
+ *
80
+ * @private
81
+ * @function
82
+ */
83
+ export const integrityCheck = store => {
84
+ store.clients.forEach(structs => {
85
+ for (let i = 1; i < structs.length; i++) {
86
+ const l = structs[i - 1]
87
+ const r = structs[i]
88
+ if (l.id.clock + l.length !== r.id.clock) {
89
+ throw new Error('StructStore failed integrity check')
90
+ }
91
+ }
92
+ })
93
+ }
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
+ }