fix some tests, implement event classes for types, and re-implement logging

This commit is contained in:
Kevin Jahns 2017-10-22 19:13:03 +02:00
parent c92f987496
commit 142a5ada60
3 changed files with 97 additions and 0 deletions

24
src/Transaction.js Normal file
View File

@ -0,0 +1,24 @@
export default class Transaction {
constructor (y) {
this.y = y
// types added during transaction
this.newTypes = new Set()
// changed types (does not include new types)
// maps from type to parentSubs (item.parentSub = null for array elements)
this.changedTypes = new Map()
}
}
export function transactionTypeChanged (y, type, sub) {
if (type !== y && !type._deleted) {
const changedTypes = y._transaction.changedTypes
let subs = changedTypes.get(type)
if (subs === undefined) {
// create if it doesn't exist yet
subs = new Set()
changedTypes.set(type, subs)
}
subs.add(sub)
}
}

View File

@ -0,0 +1,16 @@
export default class YXmlEvent {
constructor (target, subs, remote) {
this.target = target
this.childListChanged = false
this.attributesChanged = new Set()
this.remote = remote
subs.forEach((sub) => {
if (sub === null) {
this.childListChanged = true
} else {
this.attributesChanged.add(sub)
}
})
}
}

View File

@ -0,0 +1,57 @@
import ID from '../Util/ID.js'
import ItemJSON from '../Struct/ItemJSON.js'
import ItemString from '../Struct/ItemString.js'
/**
* Try to merge all items in os with their successors.
*
* Some transformations (like delete) fragment items.
* Item(c: 'ab') + Delete(1,1) + Delete(0, 1) -> Item(c: 'a',deleted);Item(c: 'b',deleted)
*
* This functions merges the fragmented nodes together:
* Item(c: 'a',deleted);Item(c: 'b',deleted) -> Item(c: 'ab', deleted)
*
* TODO: The Tree implementation does not support deletions in-spot.
* This is why all deletions must be performed after the traversal.
*
*/
export function defragmentItemContent (y) {
const os = y.os
if (os.length < 2) {
return
}
let deletes = []
let node = os.findSmallestNode()
let next = node.next()
while (next !== null) {
let a = node.val
let b = next.val
if (
(a instanceof ItemJSON || a instanceof ItemString) &&
a.constructor === b.constructor &&
a._deleted === b._deleted &&
a._right === b &&
(new ID(a._id.user, a._id.clock + a._length)).equals(b._id)
) {
a._right = b._right
if (a instanceof ItemJSON) {
a._content = a._content.concat(b._content)
} else if (a instanceof ItemString) {
a._content += b._content
}
// delete b later
deletes.push(b._id)
// do not iterate node!
// !(node = next)
} else {
// not able to merge node, get next node
node = next
}
// update next
next = next.next()
}
for (let i = deletes.length - 1; i >= 0; i--) {
os.delete(deletes[i])
}
}