fixed inserting large xml portions

This commit is contained in:
Kevin Jahns 2017-10-27 22:28:32 +02:00
parent 1dea8f394f
commit c619aa33d9
6 changed files with 58 additions and 20 deletions

View File

@ -22,6 +22,14 @@ export default class Type extends Item {
this._y = null this._y = null
this._eventHandler = new EventHandler() this._eventHandler = new EventHandler()
} }
_transact (f) {
const y = this._y
if (y !== null) {
y.transact(f)
} else {
f(y)
}
}
observe (f) { observe (f) {
this._eventHandler.addEventListener(f) this._eventHandler.addEventListener(f)
} }
@ -41,8 +49,9 @@ export default class Type extends Item {
} }
// integrate map children // integrate map children
const map = this._map const map = this._map
for (let [key, t] of map) { this._map = new Map()
map.delete(key) for (let t of map.values()) {
// TODO make sure that right elements are deleted!
integrateChildren(y, t) integrateChildren(y, t)
} }
} }

View File

@ -123,8 +123,7 @@ export default class YArray extends Type {
} }
} }
insertAfter (left, content) { insertAfter (left, content) {
const y = this._y this._transact(y => {
const apply = () => {
let right let right
if (left === null) { if (left === null) {
right = this._start right = this._start
@ -154,6 +153,8 @@ export default class YArray extends Type {
c._integrate(y) c._integrate(y)
} else if (left === null) { } else if (left === null) {
this._start = c this._start = c
} else {
left._right = c
} }
left = c left = c
} else { } else {
@ -172,12 +173,7 @@ export default class YArray extends Type {
if (prevJsonIns !== null && y !== null) { if (prevJsonIns !== null && y !== null) {
prevJsonIns._integrate(y) prevJsonIns._integrate(y)
} }
} })
if (y !== null) {
y.transact(apply)
} else {
apply()
}
} }
insert (pos, content) { insert (pos, content) {
let left = null let left = null

View File

@ -35,16 +35,15 @@ export default class YMap extends Type {
return map return map
} }
delete (key) { delete (key) {
this._y.transact(() => { this._transact((y) => {
let c = this._map.get(key) let c = this._map.get(key)
if (c !== undefined) { if (y !== null && c !== undefined) {
c._delete(this._y) c._delete(y)
} }
}) })
} }
set (key, value) { set (key, value) {
const y = this._y this._transact(y => {
y.transact(() => {
const old = this._map.get(key) || null const old = this._map.get(key) || null
if (old !== null) { if (old !== null) {
if (old instanceof ItemJSON && old._content[0] === value) { if (old instanceof ItemJSON && old._content[0] === value) {
@ -52,7 +51,9 @@ export default class YMap extends Type {
// break here // break here
return value return value
} }
old._delete(y) if (y !== null) {
old._delete(y)
}
} }
let v let v
if (typeof value === 'function') { if (typeof value === 'function') {
@ -67,7 +68,11 @@ export default class YMap extends Type {
v._right_origin = old v._right_origin = old
v._parent = this v._parent = this
v._parentSub = key v._parentSub = key
v._integrate(y) if (y !== null) {
v._integrate(y)
} else {
this._map.set(key, v)
}
}) })
return value return value
} }

View File

@ -24,14 +24,16 @@ export default class YText extends YArray {
return strBuilder.join('') return strBuilder.join('')
} }
insert (pos, text) { insert (pos, text) {
this._y.transact(() => { this._transact(y => {
let left = null let left = null
let right = this._start let right = this._start
let count = 0 let count = 0
while (right !== null) { while (right !== null) {
if (count <= pos && pos < count + right._length) { if (count <= pos && pos < count + right._length) {
const splitDiff = pos - count
right = right._splitAt(this._y, pos - count) right = right._splitAt(this._y, pos - count)
left = right._left left = right._left
count += splitDiff
break break
} }
count += right._length count += right._length
@ -48,7 +50,13 @@ export default class YText extends YArray {
item._right_origin = right item._right_origin = right
item._parent = this item._parent = this
item._content = text item._content = text
item._integrate(this._y) if (y !== null) {
item._integrate(this._y)
} else if (left === null) {
this._start = item
} else {
left._right = item
}
}) })
} }
_logString () { _logString () {

View File

@ -46,7 +46,7 @@ export default class Y extends NamedEventHandler {
this._transaction = new Transaction(this) this._transaction = new Transaction(this)
} }
try { try {
f() f(this)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }

View File

@ -267,6 +267,26 @@ test('filter attribute', async function xml15 (t) {
await compareUsers(t, users) await compareUsers(t, users)
}) })
test('deep element insert', async function xml16 (t) {
var { users, xml0, xml1 } = await initArrays(t, { users: 3 })
let dom0 = xml0.getDom()
let dom1 = xml1.getDom()
let deepElement = document.createElement('p')
let boldElement = document.createElement('b')
let attrElement = document.createElement('img')
attrElement.setAttribute('src', 'http:localhost:8080/nowhere')
boldElement.append(document.createTextNode('hi'))
deepElement.append(boldElement)
deepElement.append(attrElement)
dom0.append(deepElement)
console.log(dom0.outerHTML)
let str0 = dom0.outerHTML
await flushAll(t, users)
let str1 = dom1.outerHTML
t.compare(str0, str1, 'Dom string representation matches')
await compareUsers(t, users)
})
// TODO: move elements // TODO: move elements
var xmlTransactions = [ var xmlTransactions = [
function attributeChange (t, user, chance) { function attributeChange (t, user, chance) {