@y/y 14.0.0-18 → 14.0.0-20
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.
- package/README.md +7 -5
- package/dist/src/index.d.ts +2 -1
- package/dist/src/internals.d.ts +2 -9
- package/dist/src/structs/ContentType.d.ts +6 -12
- package/dist/src/structs/ContentType.d.ts.map +1 -1
- package/dist/src/structs/Item.d.ts +5 -6
- package/dist/src/structs/Item.d.ts.map +1 -1
- package/dist/src/utils/AttributionManager.d.ts +20 -6
- package/dist/src/utils/AttributionManager.d.ts.map +1 -1
- package/dist/src/utils/Doc.d.ts +7 -70
- package/dist/src/utils/Doc.d.ts.map +1 -1
- package/dist/src/utils/ID.d.ts +2 -2
- package/dist/src/utils/ID.d.ts.map +1 -1
- package/dist/src/utils/IdMap.d.ts +19 -16
- package/dist/src/utils/IdMap.d.ts.map +1 -1
- package/dist/src/utils/IdSet.d.ts +2 -2
- package/dist/src/utils/IdSet.d.ts.map +1 -1
- package/dist/src/utils/RelativePosition.d.ts +8 -8
- package/dist/src/utils/RelativePosition.d.ts.map +1 -1
- package/dist/src/utils/Transaction.d.ts +9 -5
- package/dist/src/utils/Transaction.d.ts.map +1 -1
- package/dist/src/utils/UndoManager.d.ts +14 -12
- package/dist/src/utils/UndoManager.d.ts.map +1 -1
- package/dist/src/utils/UpdateEncoder.d.ts +2 -0
- package/dist/src/utils/UpdateEncoder.d.ts.map +1 -1
- package/dist/src/utils/YEvent.d.ts +21 -42
- package/dist/src/utils/YEvent.d.ts.map +1 -1
- package/dist/src/utils/isParentOf.d.ts +1 -1
- package/dist/src/utils/isParentOf.d.ts.map +1 -1
- package/dist/src/utils/logging.d.ts +2 -2
- package/dist/src/utils/logging.d.ts.map +1 -1
- package/dist/src/utils/meta.d.ts +43 -0
- package/dist/src/utils/meta.d.ts.map +1 -0
- package/dist/src/utils/ts.d.ts +4 -0
- package/dist/src/utils/ts.d.ts.map +1 -0
- package/dist/src/utils/updates.d.ts +3 -9
- package/dist/src/utils/updates.d.ts.map +1 -1
- package/dist/src/ytype.d.ts +498 -0
- package/dist/src/ytype.d.ts.map +1 -0
- package/dist/tests/IdMap.tests.d.ts.map +1 -1
- package/dist/tests/attribution.tests.d.ts +1 -0
- package/dist/tests/attribution.tests.d.ts.map +1 -1
- package/dist/tests/compatibility.tests.d.ts.map +1 -1
- package/dist/tests/doc.tests.d.ts.map +1 -1
- package/dist/tests/relativePositions.tests.d.ts +9 -9
- package/dist/tests/relativePositions.tests.d.ts.map +1 -1
- package/dist/tests/snapshot.tests.d.ts.map +1 -1
- package/dist/tests/testHelper.d.ts +28 -27
- package/dist/tests/testHelper.d.ts.map +1 -1
- package/dist/tests/undo-redo.tests.d.ts.map +1 -1
- package/dist/tests/updates.tests.d.ts +1 -1
- package/dist/tests/updates.tests.d.ts.map +1 -1
- package/dist/tests/y-array.tests.d.ts +0 -2
- package/dist/tests/y-array.tests.d.ts.map +1 -1
- package/dist/tests/y-map.tests.d.ts +0 -3
- package/dist/tests/y-map.tests.d.ts.map +1 -1
- package/dist/tests/y-text.tests.d.ts +1 -1
- package/dist/tests/y-text.tests.d.ts.map +1 -1
- package/dist/tests/y-xml.tests.d.ts +0 -1
- package/dist/tests/y-xml.tests.d.ts.map +1 -1
- package/package.json +16 -16
- package/src/index.js +152 -0
- package/src/internals.js +35 -0
- package/src/structs/AbstractStruct.js +59 -0
- package/src/structs/ContentAny.js +115 -0
- package/src/structs/ContentBinary.js +93 -0
- package/src/structs/ContentDeleted.js +101 -0
- package/src/structs/ContentDoc.js +141 -0
- package/src/structs/ContentEmbed.js +98 -0
- package/src/structs/ContentFormat.js +105 -0
- package/src/structs/ContentJSON.js +119 -0
- package/src/structs/ContentString.js +113 -0
- package/src/structs/ContentType.js +152 -0
- package/src/structs/GC.js +80 -0
- package/src/structs/Item.js +841 -0
- package/src/structs/Skip.js +75 -0
- package/src/utils/AttributionManager.js +653 -0
- package/src/utils/Doc.js +266 -0
- package/src/utils/EventHandler.js +87 -0
- package/src/utils/ID.js +89 -0
- package/src/utils/IdMap.js +673 -0
- package/src/utils/IdSet.js +825 -0
- package/src/utils/RelativePosition.js +352 -0
- package/src/utils/Snapshot.js +220 -0
- package/src/utils/StructSet.js +137 -0
- package/src/utils/StructStore.js +289 -0
- package/src/utils/Transaction.js +671 -0
- package/src/utils/UndoManager.js +406 -0
- package/src/utils/UpdateDecoder.js +281 -0
- package/src/utils/UpdateEncoder.js +327 -0
- package/src/utils/YEvent.js +189 -0
- package/src/utils/delta-helpers.js +54 -0
- package/src/utils/encoding.js +623 -0
- package/src/utils/isParentOf.js +21 -0
- package/src/utils/logging.js +21 -0
- package/src/utils/meta.js +97 -0
- package/src/utils/ts.js +3 -0
- package/src/utils/updates.js +711 -0
- package/src/ytype.js +1962 -0
- package/dist/Skip-wRT7BKFP.js +0 -11877
- package/dist/Skip-wRT7BKFP.js.map +0 -1
- package/dist/index-BV-j5wdP.js +0 -163
- package/dist/index-BV-j5wdP.js.map +0 -1
- package/dist/internals.js +0 -25
- package/dist/internals.js.map +0 -1
- package/dist/src/types/AbstractType.d.ts +0 -239
- package/dist/src/types/AbstractType.d.ts.map +0 -1
- package/dist/src/types/YArray.d.ts +0 -128
- package/dist/src/types/YArray.d.ts.map +0 -1
- package/dist/src/types/YMap.d.ts +0 -112
- package/dist/src/types/YMap.d.ts.map +0 -1
- package/dist/src/types/YText.d.ts +0 -216
- package/dist/src/types/YText.d.ts.map +0 -1
- package/dist/src/types/YXmlElement.d.ts +0 -106
- package/dist/src/types/YXmlElement.d.ts.map +0 -1
- package/dist/src/types/YXmlFragment.d.ts +0 -143
- package/dist/src/types/YXmlFragment.d.ts.map +0 -1
- package/dist/src/types/YXmlHook.d.ts +0 -32
- package/dist/src/types/YXmlHook.d.ts.map +0 -1
- package/dist/src/types/YXmlText.d.ts +0 -34
- package/dist/src/types/YXmlText.d.ts.map +0 -1
- package/dist/src/utils/AbstractConnector.d.ts +0 -20
- package/dist/src/utils/AbstractConnector.d.ts.map +0 -1
- package/dist/src/utils/types.d.ts +0 -7
- package/dist/src/utils/types.d.ts.map +0 -1
- package/dist/testHelper.js +0 -617
- package/dist/testHelper.js.map +0 -1
- package/dist/yjs.js +0 -26
- 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
|
+
}
|