implemented xml type for new event system

This commit is contained in:
Kevin Jahns
2017-10-19 17:36:28 +02:00
parent 1311c7a0d8
commit 755c9eb16e
38 changed files with 1272 additions and 322 deletions

View File

@@ -2,6 +2,16 @@ import Type from '../Struct/Type.js'
import ItemJSON from '../Struct/ItemJSON.js'
export default class YArray extends Type {
_callObserver () {
this._eventHandler.callEventListeners({})
}
get (i) {
// TODO: This can be improved!
return this.toArray()[i]
}
toArray () {
return this.map(c => c)
}
toJSON () {
return this.map(c => {
if (c instanceof Type) {
@@ -11,6 +21,7 @@ export default class YArray extends Type {
return c.toString()
}
}
return c
})
}
map (f) {
@@ -25,11 +36,15 @@ export default class YArray extends Type {
let n = this._start
while (n !== null) {
if (!n._deleted) {
const content = n._content
const contentLen = content.length
for (let i = 0; i < contentLen; i++) {
pos++
f(content[i], pos, this)
if (n instanceof Type) {
f(n, pos++, this)
} else {
const content = n._content
const contentLen = content.length
for (let i = 0; i < contentLen; i++) {
pos++
f(content[i], pos, this)
}
}
}
n = n._right
@@ -42,14 +57,14 @@ export default class YArray extends Type {
if (!n._deleted) {
length += n._length
}
n = n._next
n = n._right
}
return length
}
[Symbol.iterator] () {
return {
next: function () {
while (this._item !== null && (this._item._deleted || this._item._content.length <= this._itemElement)) {
while (this._item !== null && (this._item._deleted || this._item._length <= this._itemElement)) {
// item is deleted or itemElement does not exist (is deleted)
this._item = this._item._right
this._itemElement = 0
@@ -58,11 +73,16 @@ export default class YArray extends Type {
return {
done: true
}
}
let content
if (this._item instanceof Type) {
content = this._item
} else {
return {
value: [this._count, this._item._content[this._itemElement++]],
done: false
}
content = this._item._content[this._itemElement++]
}
return {
value: [this._count, content],
done: false
}
},
_item: this._start,
@@ -71,68 +91,99 @@ export default class YArray extends Type {
}
}
delete (pos, length = 1) {
let item = this._start
let count = 0
while (item !== null && length > 0) {
if (count < pos && pos < count + item._length) {
const diffDel = pos - count
item = item
._splitAt(this._y, diffDel)
._splitAt(this._y, length)
length -= item._length
item._delete(this._y)
this._y.transact(() => {
let item = this._start
let count = 0
while (item !== null && length > 0) {
if (count <= pos && pos < count + item._length) {
const diffDel = pos - count
item = item._splitAt(this._y, diffDel)
item._splitAt(this._y, length)
length -= item._length
item._delete(this._y)
}
if (!item._deleted) {
count += item._length
}
item = item._right
}
if (!item._deleted) {
count += item._length
if (length > 0) {
throw new Error('Delete exceeds the range of the YArray')
}
})
}
insertAfter (left, content) {
const apply = () => {
let right
if (left === null) {
right = this._start
} else {
right = left._right
}
let prevJsonIns = null
for (let i = 0; i < content.length; i++) {
let c = content[i]
if (c instanceof Type) {
if (prevJsonIns !== null) {
if (this._y !== null) {
prevJsonIns._integrate(this._y)
}
left = prevJsonIns
prevJsonIns = null
}
c._origin = left
c._left = left
c._right = right
c._right_origin = right
c._parent = this
if (this._y !== null) {
c._integrate(this._y)
} else if (left === null) {
this._start = c
}
left = c
} else {
if (prevJsonIns === null) {
prevJsonIns = new ItemJSON()
prevJsonIns._origin = left
prevJsonIns._left = left
prevJsonIns._right = right
prevJsonIns._right_origin = right
prevJsonIns._parent = this
prevJsonIns._content = []
}
prevJsonIns._content.push(c)
}
}
if (prevJsonIns !== null && this._y !== null) {
prevJsonIns._integrate(this._y)
}
item = item._right
}
if (length > 0) {
throw new Error('Delete exceeds the range of the YArray')
if (this._y !== null) {
this._y.transact(apply)
} else {
apply()
}
return content
}
insert (pos, content) {
let left = this._start
let right = null
let left = null
let right = this._start
let count = 0
while (left !== null) {
if (count <= pos && pos < count + left._content.length) {
right = left._splitAt(this.y, pos - count)
while (right !== null) {
if (count <= pos && pos < count + right._length) {
right = right._splitAt(this._y, pos - count)
left = right._left
break
}
count += left._length
left = left.right
count += right._length
left = right
right = right._right
}
if (pos > count) {
throw new Error('Position exceeds array range!')
}
let prevJsonIns = null
for (let i = 0; i < content.length; i++) {
let c = content[i]
if (c instanceof Type) {
if (prevJsonIns === null) {
prevJsonIns._integrate(this._y)
prevJsonIns = null
}
c._left = left
c._origin = left
c._right = right
c._parent = this
} else {
if (prevJsonIns === null) {
prevJsonIns = new ItemJSON()
prevJsonIns._origin = left
prevJsonIns._left = left
prevJsonIns._right = right
prevJsonIns._parent = this
prevJsonIns._content = []
}
prevJsonIns._content.push(c)
}
}
if (prevJsonIns !== null) {
prevJsonIns._integrate(this._y)
}
this.insertAfter(left, content)
}
_logString () {
let s = super._logString()