@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.
- package/README.md +36 -12
- package/dist/src/index.d.ts +24 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/structs/AbstractStruct.d.ts +14 -17
- package/dist/src/structs/AbstractStruct.d.ts.map +1 -1
- package/dist/src/structs/GC.d.ts +9 -14
- package/dist/src/structs/GC.d.ts.map +1 -1
- package/dist/src/structs/Item.d.ts +569 -31
- package/dist/src/structs/Item.d.ts.map +1 -1
- package/dist/src/structs/Skip.d.ts +9 -11
- package/dist/src/structs/Skip.d.ts.map +1 -1
- package/dist/src/utils/BlockSet.d.ts +8 -7
- package/dist/src/utils/BlockSet.d.ts.map +1 -1
- package/dist/src/utils/Doc.d.ts +4 -9
- package/dist/src/utils/Doc.d.ts.map +1 -1
- package/dist/src/utils/ID.d.ts +0 -1
- package/dist/src/utils/ID.d.ts.map +1 -1
- package/dist/src/utils/RelativePosition.d.ts +2 -5
- package/dist/src/utils/RelativePosition.d.ts.map +1 -1
- package/dist/src/utils/Renderer.d.ts +144 -0
- package/dist/src/utils/Renderer.d.ts.map +1 -0
- package/dist/src/utils/Snapshot.d.ts +7 -11
- package/dist/src/utils/Snapshot.d.ts.map +1 -1
- package/dist/src/utils/StructStore.d.ts +45 -23
- package/dist/src/utils/StructStore.d.ts.map +1 -1
- package/dist/src/utils/Transaction.d.ts +11 -21
- package/dist/src/utils/Transaction.d.ts.map +1 -1
- package/dist/src/utils/UndoManager.d.ts +13 -14
- package/dist/src/utils/UndoManager.d.ts.map +1 -1
- package/dist/src/utils/UpdateDecoder.d.ts +1 -1
- package/dist/src/utils/UpdateDecoder.d.ts.map +1 -1
- package/dist/src/utils/UpdateEncoder.d.ts +0 -1
- package/dist/src/utils/UpdateEncoder.d.ts.map +1 -1
- package/dist/src/utils/YEvent.d.ts +22 -26
- package/dist/src/utils/YEvent.d.ts.map +1 -1
- package/dist/src/utils/content-helper.d.ts +2 -0
- package/dist/src/utils/content-helper.d.ts.map +1 -0
- package/dist/src/utils/delta-helpers.d.ts +2 -3
- package/dist/src/utils/delta-helpers.d.ts.map +1 -1
- package/dist/src/utils/encoding-helpers.d.ts +6 -0
- package/dist/src/utils/encoding-helpers.d.ts.map +1 -0
- package/dist/src/utils/encoding.d.ts +16 -18
- package/dist/src/utils/encoding.d.ts.map +1 -1
- package/dist/src/utils/ids.d.ts +331 -0
- package/dist/src/utils/ids.d.ts.map +1 -0
- package/dist/src/utils/isParentOf.d.ts +1 -2
- package/dist/src/utils/isParentOf.d.ts.map +1 -1
- package/dist/src/utils/logging.d.ts +0 -1
- package/dist/src/utils/logging.d.ts.map +1 -1
- package/dist/src/utils/meta.d.ts +15 -64
- package/dist/src/utils/meta.d.ts.map +1 -1
- package/dist/src/utils/renderer-helpers.d.ts +99 -0
- package/dist/src/utils/renderer-helpers.d.ts.map +1 -0
- package/dist/src/utils/schemas.d.ts +3 -0
- package/dist/src/utils/schemas.d.ts.map +1 -0
- package/dist/src/utils/transaction-helpers.d.ts +18 -0
- package/dist/src/utils/transaction-helpers.d.ts.map +1 -0
- package/dist/src/utils/updates.d.ts +19 -25
- package/dist/src/utils/updates.d.ts.map +1 -1
- package/dist/src/ytype.d.ts +144 -41
- package/dist/src/ytype.d.ts.map +1 -1
- package/global.d.ts +53 -0
- package/package.json +12 -16
- package/src/index.js +32 -124
- package/src/structs/AbstractStruct.js +21 -16
- package/src/structs/GC.js +15 -20
- package/src/structs/Item.js +992 -318
- package/src/structs/Skip.js +16 -19
- package/src/utils/BlockSet.js +20 -20
- package/src/utils/Doc.js +18 -29
- package/src/utils/ID.js +0 -2
- package/src/utils/RelativePosition.js +15 -25
- package/src/utils/{AttributionManager.js → Renderer.js} +42 -197
- package/src/utils/Snapshot.js +15 -37
- package/src/utils/StructStore.js +89 -227
- package/src/utils/Transaction.js +89 -321
- package/src/utils/UndoManager.js +157 -19
- package/src/utils/UpdateDecoder.js +2 -3
- package/src/utils/UpdateEncoder.js +0 -4
- package/src/utils/YEvent.js +20 -26
- package/src/utils/content-helper.js +0 -0
- package/src/utils/delta-helpers.js +21 -42
- package/src/utils/encoding-helpers.js +110 -0
- package/src/utils/encoding.js +197 -122
- package/src/utils/ids.js +1527 -0
- package/src/utils/isParentOf.js +2 -4
- package/src/utils/logging.js +0 -4
- package/src/utils/meta.js +57 -46
- package/src/utils/renderer-helpers.js +110 -0
- package/src/utils/schemas.js +3 -0
- package/src/utils/transaction-helpers.js +413 -0
- package/src/utils/updates.js +24 -146
- package/src/ytype.js +626 -255
- package/tests/testHelper.js +10 -12
- package/dist/src/internals.d.ts +0 -36
- package/dist/src/internals.d.ts.map +0 -1
- package/dist/src/structs/ContentAny.d.ts +0 -67
- package/dist/src/structs/ContentAny.d.ts.map +0 -1
- package/dist/src/structs/ContentBinary.d.ts +0 -64
- package/dist/src/structs/ContentBinary.d.ts.map +0 -1
- package/dist/src/structs/ContentDeleted.d.ts +0 -64
- package/dist/src/structs/ContentDeleted.d.ts.map +0 -1
- package/dist/src/structs/ContentDoc.d.ts +0 -72
- package/dist/src/structs/ContentDoc.d.ts.map +0 -1
- package/dist/src/structs/ContentEmbed.d.ts +0 -67
- package/dist/src/structs/ContentEmbed.d.ts.map +0 -1
- package/dist/src/structs/ContentFormat.d.ts +0 -69
- package/dist/src/structs/ContentFormat.d.ts.map +0 -1
- package/dist/src/structs/ContentJSON.d.ts +0 -70
- package/dist/src/structs/ContentJSON.d.ts.map +0 -1
- package/dist/src/structs/ContentString.d.ts +0 -70
- package/dist/src/structs/ContentString.d.ts.map +0 -1
- package/dist/src/structs/ContentType.d.ts +0 -77
- package/dist/src/structs/ContentType.d.ts.map +0 -1
- package/dist/src/utils/AttributionManager.d.ts +0 -238
- package/dist/src/utils/AttributionManager.d.ts.map +0 -1
- package/dist/src/utils/IdMap.d.ts +0 -164
- package/dist/src/utils/IdMap.d.ts.map +0 -1
- package/dist/src/utils/IdSet.d.ts +0 -163
- package/dist/src/utils/IdSet.d.ts.map +0 -1
- package/dist/tests/IdMap.tests.d.ts +0 -9
- package/dist/tests/IdMap.tests.d.ts.map +0 -1
- package/dist/tests/IdSet.tests.d.ts +0 -9
- package/dist/tests/IdSet.tests.d.ts.map +0 -1
- package/dist/tests/attribution.tests.d.ts +0 -9
- package/dist/tests/attribution.tests.d.ts.map +0 -1
- package/dist/tests/compatibility.tests.d.ts +0 -5
- package/dist/tests/compatibility.tests.d.ts.map +0 -1
- package/dist/tests/delta.tests.d.ts +0 -7
- package/dist/tests/delta.tests.d.ts.map +0 -1
- package/dist/tests/doc.tests.d.ts +0 -13
- package/dist/tests/doc.tests.d.ts.map +0 -1
- package/dist/tests/encoding.tests.d.ts +0 -5
- package/dist/tests/encoding.tests.d.ts.map +0 -1
- package/dist/tests/index.d.ts +0 -2
- package/dist/tests/index.d.ts.map +0 -1
- package/dist/tests/relativePositions.tests.d.ts +0 -11
- package/dist/tests/relativePositions.tests.d.ts.map +0 -1
- package/dist/tests/snapshot.tests.d.ts +0 -14
- package/dist/tests/snapshot.tests.d.ts.map +0 -1
- package/dist/tests/testHelper.d.ts +0 -171
- package/dist/tests/testHelper.d.ts.map +0 -1
- package/dist/tests/undo-redo.tests.d.ts +0 -27
- package/dist/tests/undo-redo.tests.d.ts.map +0 -1
- package/dist/tests/updates.tests.d.ts +0 -26
- package/dist/tests/updates.tests.d.ts.map +0 -1
- package/dist/tests/y-array.tests.d.ts +0 -43
- package/dist/tests/y-array.tests.d.ts.map +0 -1
- package/dist/tests/y-map.tests.d.ts +0 -42
- package/dist/tests/y-map.tests.d.ts.map +0 -1
- package/dist/tests/y-text.tests.d.ts +0 -49
- package/dist/tests/y-text.tests.d.ts.map +0 -1
- package/dist/tests/y-xml.tests.d.ts +0 -14
- package/dist/tests/y-xml.tests.d.ts.map +0 -1
- package/src/internals.js +0 -35
- package/src/structs/ContentAny.js +0 -115
- package/src/structs/ContentBinary.js +0 -93
- package/src/structs/ContentDeleted.js +0 -101
- package/src/structs/ContentDoc.js +0 -141
- package/src/structs/ContentEmbed.js +0 -98
- package/src/structs/ContentFormat.js +0 -105
- package/src/structs/ContentJSON.js +0 -119
- package/src/structs/ContentString.js +0 -113
- package/src/structs/ContentType.js +0 -152
- package/src/utils/IdMap.js +0 -673
- package/src/utils/IdSet.js +0 -825
package/src/utils/StructStore.js
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
}
|