implemented xml type for new event system
This commit is contained in:
@@ -5,7 +5,7 @@ import { sendSyncStep1, readSyncStep1 } from './MessageHandler/syncStep1.js'
|
||||
import { readSyncStep2 } from './MessageHandler/syncStep2.js'
|
||||
import { readUpdate } from './MessageHandler/update.js'
|
||||
|
||||
import { debug } from './Y.js'
|
||||
import debug from 'debug'
|
||||
|
||||
export default class AbstractConnector {
|
||||
constructor (y, opts) {
|
||||
@@ -251,9 +251,13 @@ export default class AbstractConnector {
|
||||
// cannot wait for sync step 1 to finish, because we may wait for sync step 2 in sync step 1 (->lock)
|
||||
readSyncStep1(decoder, encoder, this.y, senderConn, sender)
|
||||
} else if (messageType === 'sync step 2' && senderConn.auth === 'write') {
|
||||
readSyncStep2(decoder, encoder, this.y, senderConn, sender)
|
||||
this.y.transact(() => {
|
||||
readSyncStep2(decoder, encoder, this.y, senderConn, sender)
|
||||
})
|
||||
} else if (messageType === 'update' && (skipAuth || senderConn.auth === 'write')) {
|
||||
readUpdate(decoder, encoder, this.y, senderConn, sender)
|
||||
this.y.transact(() => {
|
||||
readUpdate(decoder, encoder, this.y, senderConn, sender)
|
||||
})
|
||||
} else {
|
||||
throw new Error('Unable to receive message')
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { deleteItemRange } from '../Struct/Delete.js'
|
||||
import ID from '../Util/ID.js'
|
||||
|
||||
export function stringifyDeleteSet (y, decoder, strBuilder) {
|
||||
let dsLength = decoder.readUint32()
|
||||
@@ -18,7 +19,7 @@ export function stringifyDeleteSet (y, decoder, strBuilder) {
|
||||
|
||||
export function writeDeleteSet (y, encoder) {
|
||||
let currentUser = null
|
||||
let currentLength = 0
|
||||
let currentLength
|
||||
let lastLenPos
|
||||
|
||||
let numberOfUsers = 0
|
||||
@@ -36,14 +37,17 @@ export function writeDeleteSet (y, encoder) {
|
||||
if (currentUser !== null) { // happens on first iteration
|
||||
encoder.setUint32(lastLenPos, currentLength)
|
||||
}
|
||||
currentUser = user
|
||||
encoder.writeVarUint(user)
|
||||
// pseudo-fill pos
|
||||
lastLenPos = encoder.pos
|
||||
encoder.writeUint32(0)
|
||||
currentLength = 0
|
||||
}
|
||||
encoder.writeVarUint(clock)
|
||||
encoder.writeVarUint(len)
|
||||
encoder.writeUint8(gc ? 1 : 0)
|
||||
currentLength++
|
||||
})
|
||||
if (currentUser !== null) { // happens on first iteration
|
||||
encoder.setUint32(lastLenPos, currentLength)
|
||||
@@ -56,62 +60,64 @@ export function readDeleteSet (y, decoder) {
|
||||
for (let i = 0; i < dsLength; i++) {
|
||||
let user = decoder.readVarUint()
|
||||
let dv = []
|
||||
let dvLength = decoder.readVarUint()
|
||||
let dvLength = decoder.readUint32()
|
||||
for (let j = 0; j < dvLength; j++) {
|
||||
let from = decoder.readVarUint()
|
||||
let len = decoder.readVarUint()
|
||||
let gc = decoder.readUint8() === 1
|
||||
dv.push([from, len, gc])
|
||||
}
|
||||
var pos = 0
|
||||
var d = dv[pos]
|
||||
y.ds.iterate([user, 0], [user, Number.MAX_VALUE], function (n) {
|
||||
// cases:
|
||||
// 1. d deletes something to the right of n
|
||||
// => go to next n (break)
|
||||
// 2. d deletes something to the left of n
|
||||
// => create deletions
|
||||
// => reset d accordingly
|
||||
// *)=> if d doesn't delete anything anymore, go to next d (continue)
|
||||
// 3. not 2) and d deletes something that also n deletes
|
||||
// => reset d so that it doesn't contain n's deletion
|
||||
// *)=> if d does not delete anything anymore, go to next d (continue)
|
||||
while (d != null) {
|
||||
var diff = 0 // describe the diff of length in 1) and 2)
|
||||
if (n.id[1] + n.len <= d[0]) {
|
||||
// 1)
|
||||
break
|
||||
} else if (d[0] < n.id[1]) {
|
||||
// 2)
|
||||
// delete maximum the len of d
|
||||
// else delete as much as possible
|
||||
diff = Math.min(n.id[1] - d[0], d[1])
|
||||
deleteItemRange(y, user, d[0], diff)
|
||||
// deletions.push([user, d[0], diff, d[2]])
|
||||
} else {
|
||||
// 3)
|
||||
diff = n.id[1] + n.len - d[0] // never null (see 1)
|
||||
if (d[2] && !n.gc) {
|
||||
// d marks as gc'd but n does not
|
||||
// then delete either way
|
||||
deleteItemRange(y, user, d[0], Math.min(diff, d[1]))
|
||||
// deletions.push([user, d[0], Math.min(diff, d[1]), d[2]])
|
||||
if (dvLength > 0) {
|
||||
let pos = 0
|
||||
let d = dv[pos]
|
||||
y.ds.iterate(new ID(user, 0), new ID(user, Number.MAX_VALUE), function (n) {
|
||||
// cases:
|
||||
// 1. d deletes something to the right of n
|
||||
// => go to next n (break)
|
||||
// 2. d deletes something to the left of n
|
||||
// => create deletions
|
||||
// => reset d accordingly
|
||||
// *)=> if d doesn't delete anything anymore, go to next d (continue)
|
||||
// 3. not 2) and d deletes something that also n deletes
|
||||
// => reset d so that it doesn't contain n's deletion
|
||||
// *)=> if d does not delete anything anymore, go to next d (continue)
|
||||
while (d != null) {
|
||||
var diff = 0 // describe the diff of length in 1) and 2)
|
||||
if (n._id.clock + n.len <= d[0]) {
|
||||
// 1)
|
||||
break
|
||||
} else if (d[0] < n._id.clock) {
|
||||
// 2)
|
||||
// delete maximum the len of d
|
||||
// else delete as much as possible
|
||||
diff = Math.min(n._id.clock - d[0], d[1])
|
||||
deleteItemRange(y, user, d[0], diff)
|
||||
// deletions.push([user, d[0], diff, d[2]])
|
||||
} else {
|
||||
// 3)
|
||||
diff = n._id.clock + n.len - d[0] // never null (see 1)
|
||||
if (d[2] && !n.gc) {
|
||||
// d marks as gc'd but n does not
|
||||
// then delete either way
|
||||
deleteItemRange(y, user, d[0], Math.min(diff, d[1]))
|
||||
// deletions.push([user, d[0], Math.min(diff, d[1]), d[2]])
|
||||
}
|
||||
}
|
||||
if (d[1] <= diff) {
|
||||
// d doesn't delete anything anymore
|
||||
d = dv[++pos]
|
||||
} else {
|
||||
d[0] = d[0] + diff // reset pos
|
||||
d[1] = d[1] - diff // reset length
|
||||
}
|
||||
}
|
||||
if (d[1] <= diff) {
|
||||
// d doesn't delete anything anymore
|
||||
d = dv[++pos]
|
||||
} else {
|
||||
d[0] = d[0] + diff // reset pos
|
||||
d[1] = d[1] - diff // reset length
|
||||
}
|
||||
})
|
||||
// for the rest.. just apply it
|
||||
for (; pos < dv.length; pos++) {
|
||||
d = dv[pos]
|
||||
deleteItemRange(y, user, d[0], d[1])
|
||||
// deletions.push([user, d[0], d[1], d[2]])
|
||||
}
|
||||
})
|
||||
// for the rest.. just apply it
|
||||
for (; pos < dv.length; pos++) {
|
||||
d = dv[pos]
|
||||
deleteItemRange(y, user, d[0], d[1])
|
||||
// deletions.push([user, d[0], d[1], d[2]])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { getStruct } from '../Util/structReferences.js'
|
||||
import BinaryDecoder from '../Binary/Decoder.js'
|
||||
import Delete from '../Struct/Delete.js'
|
||||
|
||||
class MissingEntry {
|
||||
constructor (decoder, missing, struct) {
|
||||
@@ -16,24 +17,26 @@ class MissingEntry {
|
||||
*/
|
||||
function _integrateRemoteStructHelper (y, struct) {
|
||||
struct._integrate(y)
|
||||
let msu = y._missingStructs.get(struct._id.user)
|
||||
if (msu != null) {
|
||||
let len = struct._length
|
||||
for (let i = 0; i < len; i++) {
|
||||
if (msu.has(struct._id.clock + i)) {
|
||||
let msuc = msu.get(struct._id.clock + i)
|
||||
msuc.forEach(missingDef => {
|
||||
missingDef.missing--
|
||||
if (missingDef.missing === 0) {
|
||||
let missing = missingDef.struct._fromBinary(y, missingDef.decoder)
|
||||
if (missing.length > 0) {
|
||||
console.error('Missing should be empty!')
|
||||
} else {
|
||||
y._readyToIntegrate.push(missingDef.struct)
|
||||
if (!(struct instanceof Delete)) {
|
||||
let msu = y._missingStructs.get(struct._id.user)
|
||||
if (msu != null) {
|
||||
let len = struct._length
|
||||
for (let i = 0; i < len; i++) {
|
||||
if (msu.has(struct._id.clock + i)) {
|
||||
let msuc = msu.get(struct._id.clock + i)
|
||||
msuc.forEach(missingDef => {
|
||||
missingDef.missing--
|
||||
if (missingDef.missing === 0) {
|
||||
let missing = missingDef.struct._fromBinary(y, missingDef.decoder)
|
||||
if (missing.length > 0) {
|
||||
console.error('Missing should be empty!')
|
||||
} else {
|
||||
y._readyToIntegrate.push(missingDef.struct)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
msu.delete(struct._id.clock)
|
||||
})
|
||||
msu.delete(struct._id.clock)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,18 @@ import { stringifyUpdate } from './update.js'
|
||||
import { stringifySyncStep1 } from './syncStep1.js'
|
||||
import { stringifySyncStep2 } from './syncStep2.js'
|
||||
|
||||
export function messageToString (buffer) {
|
||||
export function messageToString (y, buffer) {
|
||||
let decoder = new BinaryDecoder(buffer)
|
||||
decoder.readVarString() // read roomname
|
||||
let type = decoder.readVarString()
|
||||
let strBuilder = []
|
||||
strBuilder.push('\n === ' + type + ' ===\n')
|
||||
if (type === 'update') {
|
||||
stringifyUpdate(decoder, strBuilder)
|
||||
stringifyUpdate(y, decoder, strBuilder)
|
||||
} else if (type === 'sync step 1') {
|
||||
stringifySyncStep1(decoder, strBuilder)
|
||||
stringifySyncStep1(y, decoder, strBuilder)
|
||||
} else if (type === 'sync step 2') {
|
||||
stringifySyncStep2(decoder, strBuilder)
|
||||
stringifySyncStep2(y, decoder, strBuilder)
|
||||
} else {
|
||||
strBuilder.push('-- Unknown message type - probably an encoding issue!!!')
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@ import BinaryEncoder from '../Binary/Encoder.js'
|
||||
import { readStateSet, writeStateSet } from './stateSet.js'
|
||||
import { writeDeleteSet } from './deleteSet.js'
|
||||
import ID from '../Util/ID.js'
|
||||
import { RootFakeUserID } from '../Util/RootID.js'
|
||||
|
||||
export function stringifySyncStep1 (decoder, strBuilder) {
|
||||
export function stringifySyncStep1 (y, decoder, strBuilder) {
|
||||
let auth = decoder.readVarString()
|
||||
let protocolVersion = decoder.readVarUint()
|
||||
strBuilder.push(`
|
||||
@@ -31,10 +32,13 @@ export function sendSyncStep1 (connector, syncUser) {
|
||||
}
|
||||
|
||||
export default function writeStructs (encoder, decoder, y, ss) {
|
||||
for (let [user, clock] of ss) {
|
||||
y.os.iterate(new ID(user, clock), null, function (struct) {
|
||||
struct._toBinary(encoder)
|
||||
})
|
||||
for (let user of y.ss.state.keys()) {
|
||||
let clock = ss.get(user) || 0
|
||||
if (user !== RootFakeUserID) {
|
||||
y.os.iterate(new ID(user, clock), new ID(user, Number.MAX_VALUE), function (struct) {
|
||||
struct._toBinary(encoder)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ import { integrateRemoteStructs } from './integrateRemoteStructs.js'
|
||||
import { stringifyUpdate } from './update.js'
|
||||
import { readDeleteSet } from './deleteSet.js'
|
||||
|
||||
export function stringifySyncStep2 (decoder, strBuilder) {
|
||||
export function stringifySyncStep2 (y, decoder, strBuilder) {
|
||||
strBuilder.push(' - auth: ' + decoder.readVarString() + '\n')
|
||||
strBuilder.push(' == OS: \n')
|
||||
stringifyUpdate(decoder, strBuilder)
|
||||
stringifyUpdate(y, decoder, strBuilder)
|
||||
// write DS to string
|
||||
strBuilder.push(' == DS: \n')
|
||||
let len = decoder.readUint32()
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
|
||||
import { getStruct } from '../Util/structReferences.js'
|
||||
|
||||
export function stringifyUpdate (decoder, strBuilder) {
|
||||
export function stringifyUpdate (y, decoder, strBuilder) {
|
||||
while (decoder.length !== decoder.pos) {
|
||||
let reference = decoder.readVarUint()
|
||||
let Constr = getStruct(reference)
|
||||
let struct = new Constr()
|
||||
let missing = struct._fromBinary(decoder)
|
||||
let missing = struct._fromBinary(y, decoder)
|
||||
let logMessage = struct._logString()
|
||||
if (missing.length > 0) {
|
||||
logMessage += missing.map(m => m._logString()).join(', ')
|
||||
|
||||
@@ -3,25 +3,26 @@ import ID from '../Util/ID.js'
|
||||
|
||||
class DSNode {
|
||||
constructor (id, len, gc) {
|
||||
this.id = id
|
||||
this._id = id
|
||||
this.len = len
|
||||
this.gc = gc
|
||||
}
|
||||
clone () {
|
||||
return new DSNode(this.id, this.len, this.gc)
|
||||
return new DSNode(this._id, this.len, this.gc)
|
||||
}
|
||||
}
|
||||
|
||||
export default class DeleteStore extends Tree {
|
||||
isDeleted (id) {
|
||||
var n = this.ds.findWithUpperBound(id)
|
||||
return n != null && n.id[0] === id[0] && id[1] < n.id[1] + n.len
|
||||
var n = this.findWithUpperBound(id)
|
||||
return n !== null && n._id.user === id.user && id.clock < n._id.clock + n.len
|
||||
}
|
||||
// TODO: put this in function (and all other methods)
|
||||
applyMissingDeletesOnStruct (struct) {
|
||||
const strID = struct._id
|
||||
// find most right delete
|
||||
let n = this.findWithUpperBound(new ID(strID.user, strID.clock + struct.length - 1))
|
||||
if (n === null || n.id.user !== strID.user || n.id.clock + n.length <= strID.clock) {
|
||||
let n = this.findWithUpperBound(new ID(strID.user, strID.clock + struct._length - 1))
|
||||
if (n === null || n._id.user !== strID.user || n._id.clock + n.len <= strID.clock) {
|
||||
// struct is not deleted
|
||||
return null
|
||||
}
|
||||
@@ -37,22 +38,22 @@ export default class DeleteStore extends Tree {
|
||||
throw new Error('length must be defined')
|
||||
}
|
||||
var n = this.findWithUpperBound(id)
|
||||
if (n != null && n.id.user === id.user) {
|
||||
if (n.id.clock <= id.clock && id.clock <= n.id.clock + n.len) {
|
||||
if (n != null && n._id.user === id.user) {
|
||||
if (n._id.clock <= id.clock && id.clock <= n._id.clock + n.len) {
|
||||
// id is in n's range
|
||||
var diff = id.clock + length - (n.id.clock + n.len) // overlapping right
|
||||
var diff = id.clock + length - (n._id.clock + n.len) // overlapping right
|
||||
if (diff > 0) {
|
||||
// id+length overlaps n
|
||||
if (!n.gc) {
|
||||
n.len += diff
|
||||
} else {
|
||||
diff = n.id.clock + n.len - id.clock // overlapping left (id till n.end)
|
||||
diff = n._id.clock + n.len - id.clock // overlapping left (id till n.end)
|
||||
if (diff < length) {
|
||||
// a partial deletion
|
||||
let nId = id.clone()
|
||||
nId.clock += diff
|
||||
n = new DSNode(nId, length - diff, false)
|
||||
this.ds.put(n)
|
||||
this.put(n)
|
||||
} else {
|
||||
// already gc'd
|
||||
throw new Error(
|
||||
@@ -67,21 +68,21 @@ export default class DeleteStore extends Tree {
|
||||
} else {
|
||||
// cannot extend left (there is no left!)
|
||||
n = new DSNode(id, length, false)
|
||||
this.ds.put(n) // TODO: you double-put !!
|
||||
this.put(n) // TODO: you double-put !!
|
||||
}
|
||||
} else {
|
||||
// cannot extend left
|
||||
n = new DSNode(id, length, false)
|
||||
this.ds.put(n)
|
||||
this.put(n)
|
||||
}
|
||||
// can extend right?
|
||||
var next = this.ds.findNext(n.id)
|
||||
var next = this.findNext(n._id)
|
||||
if (
|
||||
next != null &&
|
||||
n.id.user === next.id.user &&
|
||||
n.id.clock + n.len >= next.id.clock
|
||||
n._id.user === next._id.user &&
|
||||
n._id.clock + n.len >= next._id.clock
|
||||
) {
|
||||
diff = n.id.clock + n.len - next.id.clock // from next.start to n.end
|
||||
diff = n._id.clock + n.len - next._id.clock // from next.start to n.end
|
||||
while (diff >= 0) {
|
||||
// n overlaps with next
|
||||
if (next.gc) {
|
||||
@@ -92,7 +93,7 @@ export default class DeleteStore extends Tree {
|
||||
diff = diff - next.len // missing range after next
|
||||
if (diff > 0) {
|
||||
this.put(n) // unneccessary? TODO!
|
||||
this.markDeleted(new ID(next.id.user, next.id.clock + next.len), diff)
|
||||
this.markDeleted(new ID(next._id.user, next._id.clock + next.len), diff)
|
||||
}
|
||||
}
|
||||
break
|
||||
@@ -101,19 +102,19 @@ export default class DeleteStore extends Tree {
|
||||
if (diff > next.len) {
|
||||
// n is even longer than next
|
||||
// get next.next, and try to extend it
|
||||
var _next = this.findNext(next.id)
|
||||
this.delete(next.id)
|
||||
if (_next == null || n.id.user !== _next.id.user) {
|
||||
var _next = this.findNext(next._id)
|
||||
this.delete(next._id)
|
||||
if (_next == null || n._id.user !== _next._id.user) {
|
||||
break
|
||||
} else {
|
||||
next = _next
|
||||
diff = n.id.clock + n.len - next.id.clock // from next.start to n.end
|
||||
diff = n._id.clock + n.len - next._id.clock // from next.start to n.end
|
||||
// continue!
|
||||
}
|
||||
} else {
|
||||
// n just partially overlaps with next. extend n, delete next, and break this loop
|
||||
n.len += next.len - diff
|
||||
this.delete(next.id)
|
||||
this.delete(next._id)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Tree from '../Util/Tree.js'
|
||||
import RootID from '../Util/ID.js'
|
||||
import RootID from '../Util/RootID.js'
|
||||
import { getStruct } from '../Util/structReferences.js'
|
||||
|
||||
export default class OperationStore extends Tree {
|
||||
@@ -10,10 +10,12 @@ export default class OperationStore extends Tree {
|
||||
get (id) {
|
||||
let struct = this.find(id)
|
||||
if (struct === null && id instanceof RootID) {
|
||||
let Constr = getStruct(id.type)
|
||||
const Constr = getStruct(id.type)
|
||||
const y = this.y
|
||||
struct = new Constr()
|
||||
struct._id = id
|
||||
struct._parent = this.y
|
||||
struct._parent = y
|
||||
struct._integrate(y)
|
||||
this.put(struct)
|
||||
}
|
||||
return struct
|
||||
|
||||
@@ -4,12 +4,12 @@ export default class StateStore {
|
||||
constructor (y) {
|
||||
this.y = y
|
||||
this.state = new Map()
|
||||
this.currentClock = 0
|
||||
}
|
||||
getNextID (len) {
|
||||
let id = new ID(this.y.userID, this.currentClock)
|
||||
this.currentClock += len
|
||||
return id
|
||||
const user = this.y.userID
|
||||
const state = this.getState(user)
|
||||
this.setState(user, state + len)
|
||||
return new ID(user, state)
|
||||
}
|
||||
updateRemoteState (struct) {
|
||||
let user = struct._id.user
|
||||
@@ -27,4 +27,8 @@ export default class StateStore {
|
||||
}
|
||||
return state
|
||||
}
|
||||
setState (user, state) {
|
||||
// TODO: modify missingi structs here
|
||||
this.state.set(user, state)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,35 @@
|
||||
import { getReference } from '../Util/structReferences.js'
|
||||
import ID from '../Util/ID.js'
|
||||
|
||||
/**
|
||||
* Delete all items in an ID-range
|
||||
* TODO: implement getItemCleanStartNode for better performance (only one lookup)
|
||||
*/
|
||||
export function deleteItemRange (y, user, clock, range) {
|
||||
let items = y.os.getItems(this._target, this._length)
|
||||
for (let i = items.length - 1; i >= 0; i--) {
|
||||
items[i]._delete(y, false)
|
||||
const createDelete = y.connector._forwardAppliedStructs
|
||||
let item = y.os.getItemCleanStart(new ID(user, clock))
|
||||
if (item !== null) {
|
||||
if (!item._deleted) {
|
||||
item._splitAt(y, range)
|
||||
item._delete(y, createDelete)
|
||||
}
|
||||
let itemLen = item._length
|
||||
range -= itemLen
|
||||
clock += itemLen
|
||||
if (range > 0) {
|
||||
let node = y.os.findNode(new ID(user, clock))
|
||||
while (node !== null && range > 0 && node.val._id.equals(new ID(user, clock))) {
|
||||
const nodeVal = node.val
|
||||
if (!nodeVal._deleted) {
|
||||
nodeVal._splitAt(y, range)
|
||||
nodeVal._delete(y, createDelete)
|
||||
}
|
||||
const nodeLen = nodeVal._length
|
||||
range -= nodeLen
|
||||
clock += nodeLen
|
||||
node = node.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +44,7 @@ export default class Delete {
|
||||
_fromBinary (y, decoder) {
|
||||
this._targetID = decoder.readID()
|
||||
this._length = decoder.readVarUint()
|
||||
return []
|
||||
}
|
||||
_toBinary (encoder) {
|
||||
encoder.writeUint8(getReference(this.constructor))
|
||||
|
||||
@@ -34,6 +34,9 @@ export default class Item {
|
||||
this._parentSub = null
|
||||
this._deleted = false
|
||||
}
|
||||
get _lastId () {
|
||||
return new ID(this._id.user, this._id.clock + this._length - 1)
|
||||
}
|
||||
get _length () {
|
||||
return 1
|
||||
}
|
||||
@@ -61,6 +64,17 @@ export default class Item {
|
||||
del._length = this._length
|
||||
del._integrate(y, true)
|
||||
}
|
||||
const parent = this._parent
|
||||
if (parent !== y && !parent._deleted) {
|
||||
y._transactionChangedTypes.set(parent, this._parentSub)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This is called right before this struct receives any children.
|
||||
* It can be overwritten to apply pending changes before applying remote changes
|
||||
*/
|
||||
_beforeChange () {
|
||||
// nop
|
||||
}
|
||||
/*
|
||||
* - Integrate the struct so that other types/structs can see it
|
||||
@@ -68,12 +82,26 @@ export default class Item {
|
||||
* - Check if this is struct deleted
|
||||
*/
|
||||
_integrate (y) {
|
||||
const parent = this._parent
|
||||
const selfID = this._id
|
||||
const userState = selfID === null ? 0 : y.ss.getState(selfID.user)
|
||||
if (selfID === null) {
|
||||
this._id = y.ss.getNextID(this._length)
|
||||
} else if (selfID.clock < y.ss.getState(selfID.user)) {
|
||||
} else if (selfID.user === RootFakeUserID) {
|
||||
// nop
|
||||
} else if (selfID.clock < userState) {
|
||||
// already applied..
|
||||
return []
|
||||
} else if (selfID.clock === userState) {
|
||||
y.ss.setState(selfID.user, userState + this._length)
|
||||
} else {
|
||||
// missing content from user
|
||||
throw new Error('Can not apply yet!')
|
||||
}
|
||||
if (!parent._deleted && !y._transactionChangedTypes.has(parent) && !y._transactionNewTypes.has(parent)) {
|
||||
// this is the first time parent is updated
|
||||
// or this types is new
|
||||
this._parent._beforeChange()
|
||||
}
|
||||
/*
|
||||
# $this has to find a unique position between origin and the next known character
|
||||
@@ -96,7 +124,7 @@ export default class Item {
|
||||
if (this._left !== null) {
|
||||
o = this._left._right
|
||||
} else if (this._parentSub !== null) {
|
||||
o = this._parent._map.get(this._parentSub)
|
||||
o = this._parent._map.get(this._parentSub) || null
|
||||
} else {
|
||||
o = this._parent._start
|
||||
}
|
||||
@@ -124,14 +152,36 @@ export default class Item {
|
||||
}
|
||||
o = o._right
|
||||
}
|
||||
// reconnect left/right + update parent map/start if necessary
|
||||
const parentSub = this._parentSub
|
||||
if (this._left === null) {
|
||||
if (this._parentSub !== null) {
|
||||
this._parent._map.set(this._parentSub, this)
|
||||
let right
|
||||
if (parentSub !== null) {
|
||||
const pmap = parent._map
|
||||
right = pmap.get(parentSub) || null
|
||||
pmap.set(parentSub, this)
|
||||
} else {
|
||||
this._parent._start = this
|
||||
right = parent._start
|
||||
parent._start = this
|
||||
}
|
||||
this._right = right
|
||||
if (right !== null) {
|
||||
right._left = this
|
||||
}
|
||||
} else {
|
||||
const left = this._left
|
||||
const right = left._right
|
||||
this._right = right
|
||||
left._right = this
|
||||
if (right !== null) {
|
||||
right._left = this
|
||||
}
|
||||
}
|
||||
y.os.put(this)
|
||||
if (parent !== y && !parent._deleted) {
|
||||
y._transactionChangedTypes.set(parent, parentSub)
|
||||
}
|
||||
|
||||
if (this._id.user !== RootFakeUserID) {
|
||||
if (y.connector._forwardAppliedStructs || this._id.user === y.userID) {
|
||||
y.connector.broadcastStruct(this)
|
||||
@@ -160,10 +210,10 @@ export default class Item {
|
||||
encoder.writeUint8(info)
|
||||
encoder.writeID(this._id)
|
||||
if (info & 0b1) {
|
||||
encoder.writeID(this._origin._id)
|
||||
encoder.writeID(this._origin._lastId)
|
||||
}
|
||||
if (info & 0b10) {
|
||||
encoder.writeID(this._left._id)
|
||||
encoder.writeID(this._left._lastId)
|
||||
}
|
||||
if (info & 0b100) {
|
||||
encoder.writeID(this._right_origin._id)
|
||||
@@ -179,7 +229,8 @@ export default class Item {
|
||||
_fromBinary (y, decoder) {
|
||||
let missing = []
|
||||
const info = decoder.readUint8()
|
||||
this._id = decoder.readID()
|
||||
const id = decoder.readID()
|
||||
this._id = id
|
||||
// read origin
|
||||
if (info & 0b1) {
|
||||
// origin != null
|
||||
@@ -214,9 +265,9 @@ export default class Item {
|
||||
// right != null
|
||||
const rightID = decoder.readID()
|
||||
if (this._right_origin === null) {
|
||||
const right = y.os.getCleanStart(rightID)
|
||||
const right = y.os.getItemCleanStart(rightID)
|
||||
if (right === null) {
|
||||
missing.push(right)
|
||||
missing.push(rightID)
|
||||
} else {
|
||||
this._right = right
|
||||
this._right_origin = right
|
||||
@@ -230,7 +281,7 @@ export default class Item {
|
||||
if (this._parent === null) {
|
||||
const parent = y.os.get(parentID)
|
||||
if (parent === null) {
|
||||
missing.push(parent)
|
||||
missing.push(parentID)
|
||||
} else {
|
||||
this._parent = parent
|
||||
}
|
||||
@@ -239,11 +290,15 @@ export default class Item {
|
||||
if (this._origin !== null) {
|
||||
this._parent = this._origin._parent
|
||||
} else if (this._right_origin !== null) {
|
||||
this._parent = this._origin._parent
|
||||
this._parent = this._right_origin._parent
|
||||
}
|
||||
}
|
||||
if (info & 0b1000) {
|
||||
this._parentSub = decoder.readVarString()
|
||||
// TODO: maybe put this in read parent condition (you can also read parentsub from left/right)
|
||||
this._parentSub = JSON.parse(decoder.readVarString())
|
||||
}
|
||||
if (y.ss.getState(id.user) < id.clock) {
|
||||
missing.push(new ID(id.user, id.clock - 1))
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ export default class ItemJSON extends Item {
|
||||
let len = decoder.readVarUint()
|
||||
this._content = new Array(len)
|
||||
for (let i = 0; i < len; i++) {
|
||||
this._content[i] = JSON.parse(decoder.readVarString())
|
||||
const ctnt = decoder.readVarString()
|
||||
this._content[i] = JSON.parse(ctnt)
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
@@ -1,4 +1,18 @@
|
||||
import Item from './Item.js'
|
||||
import EventHandler from '../Util/EventHandler.js'
|
||||
|
||||
// restructure children as if they were inserted one after another
|
||||
function integrateChildren (y, start) {
|
||||
let right
|
||||
do {
|
||||
right = start._right
|
||||
start._right = null
|
||||
start._right_origin = null
|
||||
start._origin = start._left
|
||||
start._integrate(y)
|
||||
start = right
|
||||
} while (right !== null)
|
||||
}
|
||||
|
||||
export default class Type extends Item {
|
||||
constructor () {
|
||||
@@ -6,24 +20,46 @@ export default class Type extends Item {
|
||||
this._map = new Map()
|
||||
this._start = null
|
||||
this._y = null
|
||||
this._eventHandler = new EventHandler()
|
||||
}
|
||||
observe (f) {
|
||||
this._eventHandler.addEventListener(f)
|
||||
}
|
||||
unobserve (f) {
|
||||
this._eventHandler.removeEventListener(f)
|
||||
}
|
||||
_integrate (y) {
|
||||
y._transactionNewTypes.add(this)
|
||||
super._integrate(y)
|
||||
this._y = y
|
||||
// when integrating children we must make sure to
|
||||
// integrate start
|
||||
const start = this._start
|
||||
if (start !== null) {
|
||||
this._start = null
|
||||
integrateChildren(y, start)
|
||||
}
|
||||
// integrate map children
|
||||
const map = this._map
|
||||
for (let [key, t] of map) {
|
||||
map.delete(key)
|
||||
integrateChildren(y, t)
|
||||
}
|
||||
}
|
||||
_delete (y) {
|
||||
super._delete(y)
|
||||
_delete (y, createDelete) {
|
||||
super._delete(y, createDelete)
|
||||
y._transactionChangedTypes.delete(this)
|
||||
// delete map types
|
||||
for (let value of this._map.values()) {
|
||||
if (value instanceof Item && !value._deleted) {
|
||||
value._delete()
|
||||
value._delete(y, false)
|
||||
}
|
||||
}
|
||||
// delete array types
|
||||
let t = this._start
|
||||
while (t !== null) {
|
||||
if (!t._deleted) {
|
||||
t._delete()
|
||||
t._delete(y, false)
|
||||
}
|
||||
t = t._right
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -3,6 +3,11 @@ import Item from '../Struct/Item.js'
|
||||
import ItemJSON from '../Struct/ItemJSON.js'
|
||||
|
||||
export default class YMap extends Type {
|
||||
_callObserver (parentSub) {
|
||||
this._eventHandler.callEventListeners({
|
||||
name: parentSub
|
||||
})
|
||||
}
|
||||
toJSON () {
|
||||
const map = {}
|
||||
for (let [key, item] of this._map) {
|
||||
@@ -22,22 +27,40 @@ export default class YMap extends Type {
|
||||
}
|
||||
return map
|
||||
}
|
||||
delete (key) {
|
||||
this._y.transact(() => {
|
||||
let c = this._map.get(key)
|
||||
if (c !== undefined) {
|
||||
c._delete(this._y)
|
||||
}
|
||||
})
|
||||
}
|
||||
set (key, value) {
|
||||
let old = this._map.get(key)
|
||||
let v
|
||||
if (value instanceof Item) {
|
||||
v = value
|
||||
} else {
|
||||
let v = new ItemJSON()
|
||||
v._content = JSON.stringify(value)
|
||||
}
|
||||
v._right = old
|
||||
v._parent = this
|
||||
v._parentSub = key
|
||||
v._integrate()
|
||||
this._y.transact(() => {
|
||||
const old = this._map.get(key) || null
|
||||
if (old !== null) {
|
||||
old._delete(this._y)
|
||||
}
|
||||
let v
|
||||
if (value instanceof Item) {
|
||||
v = value
|
||||
} else {
|
||||
v = new ItemJSON()
|
||||
v._content = [value]
|
||||
}
|
||||
v._right = old
|
||||
v._right_origin = old
|
||||
v._parent = this
|
||||
v._parentSub = key
|
||||
v._integrate(this._y)
|
||||
})
|
||||
return value
|
||||
}
|
||||
get (key) {
|
||||
let v = this._map.get(key)
|
||||
if (v === undefined || v._deleted) {
|
||||
return undefined
|
||||
}
|
||||
if (v instanceof Type) {
|
||||
return v
|
||||
} else {
|
||||
|
||||
@@ -1,4 +1,53 @@
|
||||
import ItemString from '../Struct/ItemString.js'
|
||||
import YArray from './YArray.js'
|
||||
|
||||
export default class YText extends YArray {
|
||||
constructor (string) {
|
||||
super()
|
||||
if (typeof string === 'string') {
|
||||
const start = new ItemString()
|
||||
start._parent = this
|
||||
start._content = string
|
||||
this._start = start
|
||||
}
|
||||
}
|
||||
toString () {
|
||||
const strBuilder = []
|
||||
let n = this._start
|
||||
while (n !== null) {
|
||||
if (!n._deleted) {
|
||||
strBuilder.push(n._content)
|
||||
}
|
||||
n = n._right
|
||||
}
|
||||
return strBuilder.join('')
|
||||
}
|
||||
insert (pos, text) {
|
||||
this._y.transact(() => {
|
||||
let left = null
|
||||
let right = this._start
|
||||
let count = 0
|
||||
while (right !== null) {
|
||||
if (count <= pos && pos < count + right._length) {
|
||||
right = right._splitAt(this._y, pos - count)
|
||||
left = right._left
|
||||
break
|
||||
}
|
||||
count += right._length
|
||||
left = right
|
||||
right = right._right
|
||||
}
|
||||
if (pos > count) {
|
||||
throw new Error('Position exceeds array range!')
|
||||
}
|
||||
let item = new ItemString()
|
||||
item._origin = left
|
||||
item._left = left
|
||||
item._right = right
|
||||
item._right_origin = right
|
||||
item._parent = this
|
||||
item._content = text
|
||||
item._integrate(this._y)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import YArray from './YArray.js'
|
||||
|
||||
export default class YXml extends YArray {
|
||||
setDomFilter () {
|
||||
// TODO
|
||||
}
|
||||
toString () {
|
||||
return '<nodeName></nodeName>'
|
||||
}
|
||||
}
|
||||
117
src/Type/y-xml/YXmlElement.js
Normal file
117
src/Type/y-xml/YXmlElement.js
Normal file
@@ -0,0 +1,117 @@
|
||||
/* global MutationObserver */
|
||||
|
||||
// import diff from 'fast-diff'
|
||||
import { defaultDomFilter } from './utils.js'
|
||||
|
||||
import YMap from '../YMap.js'
|
||||
import YXmlFragment from './YXmlFragment.js'
|
||||
|
||||
export default class YXmlElement extends YXmlFragment {
|
||||
constructor (arg1, arg2) {
|
||||
super()
|
||||
this.nodeName = null
|
||||
this._scrollElement = null
|
||||
if (typeof arg1 === 'string') {
|
||||
this.nodeName = arg1.toUpperCase()
|
||||
} else if (arg1 != null && arg1.nodeType != null && arg1.nodeType === document.ELEMENT_NODE) {
|
||||
this.nodeName = arg1.nodeName
|
||||
this._setDom(arg1)
|
||||
} else {
|
||||
this.nodeName = 'UNDEFINED'
|
||||
}
|
||||
if (typeof arg2 === 'function') {
|
||||
this._domFilter = arg2
|
||||
}
|
||||
}
|
||||
_setDom (dom) {
|
||||
if (this._dom != null) {
|
||||
throw new Error('Only call this method if you know what you are doing ;)')
|
||||
} else if (dom.__yxml != null) { // TODO do i need to check this? - no.. but for dev purps..
|
||||
throw new Error('Already bound to an YXml type')
|
||||
} else {
|
||||
dom.__yxml = this
|
||||
// tag is already set in constructor
|
||||
// set attributes
|
||||
let attrNames = []
|
||||
for (let i = 0; i < dom.attributes.length; i++) {
|
||||
attrNames.push(dom.attributes[i].name)
|
||||
}
|
||||
attrNames = this._domFilter(dom, attrNames)
|
||||
for (let i = 0; i < attrNames.length; i++) {
|
||||
let attrName = attrNames[i]
|
||||
let attrValue = dom.getAttribute(attrName)
|
||||
this.setAttribute(attrName, attrValue)
|
||||
}
|
||||
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes))
|
||||
if (MutationObserver != null) {
|
||||
this._dom = this._bindToDom(dom)
|
||||
}
|
||||
return dom
|
||||
}
|
||||
}
|
||||
_fromBinary (y, decoder) {
|
||||
const missing = super._fromBinary(y, decoder)
|
||||
this.nodeName = decoder.readVarString()
|
||||
return missing
|
||||
}
|
||||
_toBinary (encoder) {
|
||||
super._toBinary(encoder)
|
||||
encoder.writeVarString(this.nodeName)
|
||||
}
|
||||
_integrate (y) {
|
||||
if (this.nodeName === null) {
|
||||
throw new Error('nodeName must be defined!')
|
||||
}
|
||||
if (this._domFilter === defaultDomFilter && this._parent instanceof YXmlFragment) {
|
||||
this._domFilter = this._parent._domFilter
|
||||
}
|
||||
super._integrate(y)
|
||||
}
|
||||
toString () {
|
||||
const attrs = this.getAttributes()
|
||||
const stringBuilder = []
|
||||
for (let key in attrs) {
|
||||
stringBuilder.push(key + '="' + attrs[key] + '"')
|
||||
}
|
||||
const nodeName = this.nodeName.toLocaleLowerCase()
|
||||
const attrsString = stringBuilder.length > 0 ? ' ' + stringBuilder.join(' ') : ''
|
||||
return `<${nodeName}${attrsString}>${super.toString()}</${nodeName}>`
|
||||
}
|
||||
removeAttribute () {
|
||||
return YMap.prototype.delete.apply(this, arguments)
|
||||
}
|
||||
|
||||
setAttribute () {
|
||||
return YMap.prototype.set.apply(this, arguments)
|
||||
}
|
||||
|
||||
getAttribute () {
|
||||
return YMap.prototype.get.apply(this, arguments)
|
||||
}
|
||||
|
||||
getAttributes () {
|
||||
const obj = {}
|
||||
for (let [key, value] of this._map) {
|
||||
obj[key] = value._content[0]
|
||||
}
|
||||
return obj
|
||||
}
|
||||
getDom () {
|
||||
let dom = this._dom
|
||||
if (dom == null) {
|
||||
dom = document.createElement(this.nodeName)
|
||||
dom.__yxml = this
|
||||
let attrs = this.getAttributes()
|
||||
for (let key in attrs) {
|
||||
dom.setAttribute(key, attrs[key])
|
||||
}
|
||||
this.forEach(yxml => {
|
||||
dom.appendChild(yxml.getDom())
|
||||
})
|
||||
if (MutationObserver !== null) {
|
||||
this._dom = this._bindToDom(dom)
|
||||
}
|
||||
}
|
||||
return dom
|
||||
}
|
||||
}
|
||||
167
src/Type/y-xml/YXmlFragment.js
Normal file
167
src/Type/y-xml/YXmlFragment.js
Normal file
@@ -0,0 +1,167 @@
|
||||
/* global MutationObserver */
|
||||
|
||||
import { defaultDomFilter, applyChangesFromDom, reflectChangesOnDom } from './utils.js'
|
||||
|
||||
import YArray from '../YArray.js'
|
||||
import YXmlText from './YXmlText.js'
|
||||
|
||||
function domToYXml (parent, doms) {
|
||||
const types = []
|
||||
doms.forEach(d => {
|
||||
if (d.__yxml != null && d.__yxml !== false) {
|
||||
d.__yxml._unbindFromDom()
|
||||
}
|
||||
if (parent._domFilter(d, []) !== null) {
|
||||
let type
|
||||
if (d.nodeType === document.TEXT_NODE) {
|
||||
type = new YXmlText(d)
|
||||
} else if (d.nodeType === document.ELEMENT_NODE) {
|
||||
type = new YXmlFragment._YXmlElement(d, parent._domFilter)
|
||||
} else {
|
||||
throw new Error('Unsupported node!')
|
||||
}
|
||||
type.enableSmartScrolling(parent._scrollElement)
|
||||
types.push(type)
|
||||
} else {
|
||||
d.__yxml = false
|
||||
}
|
||||
})
|
||||
return types
|
||||
}
|
||||
|
||||
export default class YXmlFragment extends YArray {
|
||||
constructor () {
|
||||
super()
|
||||
this._dom = null
|
||||
this._domFilter = defaultDomFilter
|
||||
this._domObserver = null
|
||||
// this function makes sure that either the
|
||||
// dom event is executed, or the yjs observer is executed
|
||||
var token = true
|
||||
this._mutualExclude = f => {
|
||||
if (token) {
|
||||
token = false
|
||||
try {
|
||||
f()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
this._domObserver.takeRecords()
|
||||
token = true
|
||||
}
|
||||
}
|
||||
// Apply Y.Xml events to dom
|
||||
this.observe(reflectChangesOnDom)
|
||||
}
|
||||
enableSmartScrolling (scrollElement) {
|
||||
this._scrollElement = scrollElement
|
||||
this.forEach(xml => {
|
||||
xml.enableSmartScrolling(scrollElement)
|
||||
})
|
||||
}
|
||||
setDomFilter (f) {
|
||||
this._domFilter = f
|
||||
this.forEach(xml => {
|
||||
xml.setDomFilter(f)
|
||||
})
|
||||
}
|
||||
_callObserver (parentSub) {
|
||||
let event
|
||||
if (parentSub !== null) {
|
||||
event = {
|
||||
type: 'attributeChanged',
|
||||
name: parentSub,
|
||||
value: this.getAttribute(parentSub),
|
||||
target: this
|
||||
}
|
||||
} else {
|
||||
event = {
|
||||
type: 'contentChanged',
|
||||
target: this
|
||||
}
|
||||
}
|
||||
this._eventHandler.callEventListeners(event)
|
||||
}
|
||||
toString () {
|
||||
return this.map(xml => xml.toString()).join('')
|
||||
}
|
||||
_unbindFromDom () {
|
||||
if (this._domObserver != null) {
|
||||
this._domObserver.disconnect()
|
||||
this._domObserver = null
|
||||
}
|
||||
if (this._dom != null) {
|
||||
this._dom.__yxml = null
|
||||
this._dom = null
|
||||
}
|
||||
}
|
||||
insertDomElementsAfter (prev, doms) {
|
||||
const types = domToYXml(this, doms)
|
||||
return this.insertAfter(prev, types)
|
||||
}
|
||||
insertDomElements (pos, doms) {
|
||||
const types = domToYXml(this, doms)
|
||||
this.insert(pos, types)
|
||||
return types.length
|
||||
}
|
||||
bindToDom (dom) {
|
||||
if (this._dom != null) {
|
||||
this._unbindFromDom()
|
||||
}
|
||||
if (dom.__yxml != null) {
|
||||
dom.__yxml._unbindFromDom()
|
||||
}
|
||||
if (MutationObserver == null) {
|
||||
throw new Error('Not able to bind to a DOM element, because MutationObserver is not available!')
|
||||
}
|
||||
dom.innerHTML = ''
|
||||
this.forEach(t => {
|
||||
dom.insertBefore(t.getDom(), null)
|
||||
})
|
||||
this._dom = dom
|
||||
dom.__yxml = this
|
||||
this._bindToDom(dom)
|
||||
}
|
||||
// binds to a dom element
|
||||
// Only call if dom and YXml are isomorph
|
||||
_bindToDom (dom) {
|
||||
this._domObserverListener = mutations => {
|
||||
this._mutualExclude(() => {
|
||||
let diffChildren = false
|
||||
mutations.forEach(mutation => {
|
||||
if (mutation.type === 'attributes') {
|
||||
let name = mutation.attributeName
|
||||
// check if filter accepts attribute
|
||||
if (this._domFilter(this._dom, [name]).length > 0) {
|
||||
var val = mutation.target.getAttribute(name)
|
||||
if (this.getAttribute(name) !== val) {
|
||||
if (val == null) {
|
||||
this.removeAttribute(name)
|
||||
} else {
|
||||
this.setAttribute(name, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (mutation.type === 'childList') {
|
||||
diffChildren = true
|
||||
}
|
||||
})
|
||||
if (diffChildren) {
|
||||
applyChangesFromDom(this)
|
||||
}
|
||||
})
|
||||
}
|
||||
this._domObserver = new MutationObserver(this._domObserverListener)
|
||||
const observeOptions = { childList: true }
|
||||
if (this instanceof YXmlFragment._YXmlElement) {
|
||||
observeOptions.attributes = true
|
||||
}
|
||||
this._domObserver.observe(dom, observeOptions)
|
||||
return dom
|
||||
}
|
||||
_beforeChange () {
|
||||
if (this._domObserver != null) {
|
||||
this._domObserverListener(this._domObserver.takeRecords())
|
||||
}
|
||||
}
|
||||
}
|
||||
157
src/Type/y-xml/YXmlText.js
Normal file
157
src/Type/y-xml/YXmlText.js
Normal file
@@ -0,0 +1,157 @@
|
||||
/* global getSelection, MutationObserver */
|
||||
|
||||
import diff from 'fast-diff'
|
||||
import YText from '../YText.js'
|
||||
import { getAnchorViewPosition, fixScrollPosition, getBoundingClientRect } from './utils.js'
|
||||
|
||||
function fixPosition (event, pos) {
|
||||
if (event.index <= pos) {
|
||||
if (event.type === 'delete') {
|
||||
return pos - Math.min(pos - event.index, event.length)
|
||||
} else {
|
||||
return pos + 1
|
||||
}
|
||||
} else {
|
||||
return pos
|
||||
}
|
||||
}
|
||||
|
||||
export default class YXmlText extends YText {
|
||||
constructor (arg1) {
|
||||
let dom = null
|
||||
let initialText = null
|
||||
if (arg1 != null && arg1.nodeType === document.TEXT_NODE) {
|
||||
dom = arg1
|
||||
initialText = dom.nodeValue
|
||||
}
|
||||
super(initialText)
|
||||
this._dom = null
|
||||
this._domObserver = null
|
||||
this._domObserverListener = null
|
||||
this._scrollElement = null
|
||||
if (dom !== null) {
|
||||
this._setDom(arg1)
|
||||
}
|
||||
var token = true
|
||||
this._mutualExclude = f => {
|
||||
if (token) {
|
||||
token = false
|
||||
try {
|
||||
f()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
this._domObserver.takeRecords()
|
||||
token = true
|
||||
}
|
||||
}
|
||||
this.observe(event => {
|
||||
if (this._dom != null) {
|
||||
const dom = this._dom
|
||||
this._mutualExclude(() => {
|
||||
let selection = null
|
||||
let shouldUpdateSelection = false
|
||||
let anchorNode = null
|
||||
let anchorOffset = null
|
||||
let focusNode = null
|
||||
let focusOffset = null
|
||||
if (typeof getSelection !== 'undefined') {
|
||||
selection = getSelection()
|
||||
if (selection.anchorNode === dom) {
|
||||
anchorNode = selection.anchorNode
|
||||
anchorOffset = fixPosition(event, selection.anchorOffset)
|
||||
shouldUpdateSelection = true
|
||||
}
|
||||
if (selection.focusNode === dom) {
|
||||
focusNode = selection.focusNode
|
||||
focusOffset = fixPosition(event, selection.focusOffset)
|
||||
shouldUpdateSelection = true
|
||||
}
|
||||
}
|
||||
let anchorViewPosition = getAnchorViewPosition(this._scrollElement)
|
||||
let anchorViewFix
|
||||
if (anchorViewPosition !== null && (anchorViewPosition.anchor !== null || getBoundingClientRect(this._dom).top <= 0)) {
|
||||
anchorViewFix = anchorViewPosition
|
||||
} else {
|
||||
anchorViewFix = null
|
||||
}
|
||||
dom.nodeValue = this.toString()
|
||||
fixScrollPosition(this._scrollElement, anchorViewFix)
|
||||
|
||||
if (shouldUpdateSelection) {
|
||||
selection.setBaseAndExtent(
|
||||
anchorNode || selection.anchorNode,
|
||||
anchorOffset || selection.anchorOffset,
|
||||
focusNode || selection.focusNode,
|
||||
focusOffset || selection.focusOffset
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
setDomFilter () {}
|
||||
enableSmartScrolling (scrollElement) {
|
||||
this._scrollElement = scrollElement
|
||||
}
|
||||
_setDom (dom) {
|
||||
if (this._dom != null) {
|
||||
this._unbindFromDom()
|
||||
}
|
||||
if (dom.__yxml != null) {
|
||||
dom.__yxml._unbindFromDom()
|
||||
}
|
||||
// set marker
|
||||
this._dom = dom
|
||||
dom.__yxml = this
|
||||
if (typeof MutationObserver === 'undefined') {
|
||||
return
|
||||
}
|
||||
this._domObserverListener = () => {
|
||||
this._mutualExclude(() => {
|
||||
var diffs = diff(this.toString(), dom.nodeValue)
|
||||
var pos = 0
|
||||
for (var i = 0; i < diffs.length; i++) {
|
||||
var d = diffs[i]
|
||||
if (d[0] === 0) { // EQUAL
|
||||
pos += d[1].length
|
||||
} else if (d[0] === -1) { // DELETE
|
||||
this.delete(pos, d[1].length)
|
||||
} else { // INSERT
|
||||
this.insert(pos, d[1])
|
||||
pos += d[1].length
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
this._domObserver = new MutationObserver(this._domObserverListener)
|
||||
this._domObserver.observe(this._dom, { characterData: true })
|
||||
}
|
||||
getDom () {
|
||||
if (this._dom == null) {
|
||||
const dom = document.createTextNode(this.toString())
|
||||
this._setDom(dom)
|
||||
return dom
|
||||
}
|
||||
return this._dom
|
||||
}
|
||||
_beforeChange () {
|
||||
if (this._domObserver != null && this._y !== null) { // TODO: do I need th y condition
|
||||
this._domObserverListener(this._domObserver.takeRecords())
|
||||
}
|
||||
}
|
||||
_delete (y, createDelete) {
|
||||
this._unbindFromDom()
|
||||
super._delete(y, createDelete)
|
||||
}
|
||||
_unbindFromDom () {
|
||||
if (this._domObserver != null) {
|
||||
this._domObserver.disconnect()
|
||||
this._domObserver = null
|
||||
}
|
||||
if (this._dom != null) {
|
||||
this._dom.__yxml = null
|
||||
this._dom = null
|
||||
}
|
||||
}
|
||||
}
|
||||
206
src/Type/y-xml/utils.js
Normal file
206
src/Type/y-xml/utils.js
Normal file
@@ -0,0 +1,206 @@
|
||||
|
||||
export function defaultDomFilter (node, attributes) {
|
||||
return attributes
|
||||
}
|
||||
|
||||
export function getAnchorViewPosition (scrollElement) {
|
||||
if (scrollElement == null) {
|
||||
return null
|
||||
}
|
||||
let anchor = document.getSelection().anchorNode
|
||||
if (anchor != null) {
|
||||
let top = getBoundingClientRect(anchor).top
|
||||
if (top >= 0 && top <= document.documentElement.clientHeight) {
|
||||
return {
|
||||
anchor: anchor,
|
||||
top: top
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
anchor: null,
|
||||
scrollTop: scrollElement.scrollTop,
|
||||
scrollHeight: scrollElement.scrollHeight
|
||||
}
|
||||
}
|
||||
|
||||
// get BoundingClientRect that works on text nodes
|
||||
export function getBoundingClientRect (element) {
|
||||
if (element.getBoundingClientRect != null) {
|
||||
// is element node
|
||||
return element.getBoundingClientRect()
|
||||
} else {
|
||||
// is text node
|
||||
if (element.parentNode == null) {
|
||||
// range requires that text nodes have a parent
|
||||
let span = document.createElement('span')
|
||||
span.appendChild(element)
|
||||
}
|
||||
let range = document.createRange()
|
||||
range.selectNode(element)
|
||||
return range.getBoundingClientRect()
|
||||
}
|
||||
}
|
||||
|
||||
export function fixScrollPosition (scrollElement, fix) {
|
||||
if (scrollElement !== null && fix !== null) {
|
||||
if (fix.anchor === null) {
|
||||
if (scrollElement.scrollTop === fix.scrollTop) {
|
||||
scrollElement.scrollTop = scrollElement.scrollHeight - fix.scrollHeight
|
||||
}
|
||||
} else {
|
||||
scrollElement.scrollTop = getBoundingClientRect(fix.anchor).top - fix.top
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function iterateUntilUndeleted (item) {
|
||||
while (item !== null && item._deleted) {
|
||||
item = item._right
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
/*
|
||||
* 1. Check if any of the nodes was deleted
|
||||
* 2. Iterate over the children.
|
||||
* 2.1 If a node exists without __yxml property, insert a new node
|
||||
* 2.2 If _contents.length < dom.childNodes.length, fill the
|
||||
* rest of _content with childNodes
|
||||
* 2.3 If a node was moved, delete it and
|
||||
* recreate a new yxml element that is bound to that node.
|
||||
* You can detect that a node was moved because expectedId
|
||||
* !== actualId in the list
|
||||
*/
|
||||
export function applyChangesFromDom (yxml) {
|
||||
const y = yxml._y
|
||||
let knownChildren =
|
||||
new Set(
|
||||
Array.prototype.map.call(yxml._dom.childNodes, child => child.__yxml)
|
||||
.filter(id => id !== undefined)
|
||||
)
|
||||
// 1. Check if any of the nodes was deleted
|
||||
yxml.forEach(function (childType, i) {
|
||||
if (!knownChildren.has(childType)) {
|
||||
childType._delete(y)
|
||||
}
|
||||
})
|
||||
// 2. iterate
|
||||
let childNodes = yxml._dom.childNodes
|
||||
let len = childNodes.length
|
||||
let prevExpectedNode = null
|
||||
let expectedNode = iterateUntilUndeleted(yxml._start)
|
||||
for (let domCnt = 0; domCnt < len; domCnt++) {
|
||||
const child = childNodes[domCnt]
|
||||
const childYXml = child.__yxml
|
||||
if (childYXml != null) {
|
||||
if (childYXml === false) {
|
||||
// should be ignored or is going to be deleted
|
||||
continue
|
||||
}
|
||||
if (expectedNode !== null) {
|
||||
if (expectedNode !== childYXml) {
|
||||
// 2.3 Not expected node
|
||||
if (childYXml._parent !== this) {
|
||||
// element is going to be deleted by its previous parent
|
||||
child.__yxml = null
|
||||
} else {
|
||||
childYXml._delete(y)
|
||||
}
|
||||
prevExpectedNode = yxml.insertDomElementsAfter(prevExpectedNode, [child])[0]
|
||||
} else {
|
||||
prevExpectedNode = expectedNode
|
||||
expectedNode = iterateUntilUndeleted(expectedNode._right)
|
||||
}
|
||||
// if this is the expected node id, just continue
|
||||
} else {
|
||||
// 2.2 fill _conten with child nodes
|
||||
prevExpectedNode = yxml.insertDomElementsAfter(prevExpectedNode, [child])[0]
|
||||
}
|
||||
} else {
|
||||
// 2.1 A new node was found
|
||||
prevExpectedNode = yxml.insertDomElementsAfter(prevExpectedNode, [child])[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function reflectChangesOnDom (event) {
|
||||
const yxml = event.target
|
||||
const dom = yxml._dom
|
||||
if (dom != null) {
|
||||
yxml._mutualExclude(() => {
|
||||
// TODO: do this once before applying stuff
|
||||
// let anchorViewPosition = getAnchorViewPosition(yxml._scrollElement)
|
||||
if (event.type === 'attributeChanged') {
|
||||
if (event.value === undefined) {
|
||||
dom.removeAttribute(event.name)
|
||||
} else {
|
||||
dom.setAttribute(event.name, event.value)
|
||||
}
|
||||
} else if (event.type === 'contentChanged') {
|
||||
// create fragment of undeleted nodes
|
||||
const fragment = document.createDocumentFragment()
|
||||
yxml.forEach(function (t) {
|
||||
fragment.append(t.getDom())
|
||||
})
|
||||
// remove remainding nodes
|
||||
let lastChild = dom.lastChild
|
||||
while (lastChild !== null) {
|
||||
dom.removeChild(lastChild)
|
||||
lastChild = dom.lastChild
|
||||
}
|
||||
// insert fragment of undeleted nodes
|
||||
dom.append(fragment)
|
||||
}
|
||||
/* TODO: smartscrolling
|
||||
.. else if (event.type === 'childInserted' || event.type === 'insert') {
|
||||
let nodes = event.values
|
||||
for (let i = nodes.length - 1; i >= 0; i--) {
|
||||
let node = nodes[i]
|
||||
node.setDomFilter(yxml._domFilter)
|
||||
node.enableSmartScrolling(yxml._scrollElement)
|
||||
let dom = node.getDom()
|
||||
let fixPosition = null
|
||||
let nextDom = null
|
||||
if (yxml._content.length > event.index + i + 1) {
|
||||
nextDom = yxml.get(event.index + i + 1).getDom()
|
||||
}
|
||||
yxml._dom.insertBefore(dom, nextDom)
|
||||
if (anchorViewPosition === null) {
|
||||
// nop
|
||||
} else if (anchorViewPosition.anchor !== null) {
|
||||
// no scrolling when current selection
|
||||
if (!dom.contains(anchorViewPosition.anchor) && !anchorViewPosition.anchor.contains(dom)) {
|
||||
fixPosition = anchorViewPosition
|
||||
}
|
||||
} else if (getBoundingClientRect(dom).top <= 0) {
|
||||
// adjust scrolling if modified element is out of view,
|
||||
// there is no anchor element, and the browser did not adjust scrollTop (this is checked later)
|
||||
fixPosition = anchorViewPosition
|
||||
}
|
||||
fixScrollPosition(yxml._scrollElement, fixPosition)
|
||||
}
|
||||
} else if (event.type === 'childRemoved' || event.type === 'delete') {
|
||||
for (let i = event.values.length - 1; i >= 0; i--) {
|
||||
let dom = event.values[i]._dom
|
||||
let fixPosition = null
|
||||
if (anchorViewPosition === null) {
|
||||
// nop
|
||||
} else if (anchorViewPosition.anchor !== null) {
|
||||
// no scrolling when current selection
|
||||
if (!dom.contains(anchorViewPosition.anchor) && !anchorViewPosition.anchor.contains(dom)) {
|
||||
fixPosition = anchorViewPosition
|
||||
}
|
||||
} else if (getBoundingClientRect(dom).top <= 0) {
|
||||
// adjust scrolling if modified element is out of view,
|
||||
// there is no anchor element, and the browser did not adjust scrollTop (this is checked later)
|
||||
fixPosition = anchorViewPosition
|
||||
}
|
||||
dom.remove()
|
||||
fixScrollPosition(yxml._scrollElement, fixPosition)
|
||||
}
|
||||
}
|
||||
*/
|
||||
})
|
||||
}
|
||||
}
|
||||
9
src/Type/y-xml/y-xml.js
Normal file
9
src/Type/y-xml/y-xml.js
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
import YXmlFragment from './YXmlFragment.js'
|
||||
import YXmlElement from './YXmlElement.js'
|
||||
|
||||
export { default as YXmlFragment } from './YXmlFragment.js'
|
||||
export { default as YXmlElement } from './YXmlElement.js'
|
||||
export { default as YXmlText } from './YXmlText.js'
|
||||
|
||||
YXmlFragment._YXmlElement = YXmlElement
|
||||
@@ -1,9 +0,0 @@
|
||||
import Delete from '../Struct/Delete'
|
||||
import ID from './ID'
|
||||
|
||||
export function deleteItemRange (y, user, clock, length) {
|
||||
let del = new Delete()
|
||||
del._target = new ID(user, clock)
|
||||
del._length = length
|
||||
del._integrate(y)
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
import YArray from '../Type/YArray.js'
|
||||
import YMap from '../Type/YMap.js'
|
||||
import YText from '../Type/YText.js'
|
||||
import YXml from '../Type/YXml.js'
|
||||
import YXmlFragment from '../Type/y-xml/YXmlFragment.js'
|
||||
import YXmlElement from '../Type/y-xml/YXmlElement.js'
|
||||
import YXmlText from '../Type/y-xml/YXmlText.js'
|
||||
|
||||
import Delete from '../Struct/Delete.js'
|
||||
import ItemJSON from '../Struct/ItemJSON.js'
|
||||
import ItemString from '../Struct/ItemString.js'
|
||||
|
||||
@@ -22,9 +25,13 @@ export function getReference (typeConstructor) {
|
||||
return references.get(typeConstructor)
|
||||
}
|
||||
|
||||
addStruct(0, YArray)
|
||||
addStruct(1, YMap)
|
||||
addStruct(2, YText)
|
||||
addStruct(3, YXml)
|
||||
addStruct(4, ItemJSON)
|
||||
addStruct(5, ItemString)
|
||||
addStruct(0, ItemJSON)
|
||||
addStruct(1, ItemString)
|
||||
addStruct(2, Delete)
|
||||
|
||||
addStruct(3, YArray)
|
||||
addStruct(4, YMap)
|
||||
addStruct(5, YText)
|
||||
addStruct(6, YXmlFragment)
|
||||
addStruct(7, YXmlElement)
|
||||
addStruct(8, YXmlText)
|
||||
|
||||
40
src/Y.js
40
src/Y.js
@@ -12,15 +12,22 @@ import Persistence from './Persistence.js'
|
||||
import YArray from './Type/YArray.js'
|
||||
import YMap from './Type/YMap.js'
|
||||
import YText from './Type/YText.js'
|
||||
import YXml from './Type/YXml.js'
|
||||
import { YXmlFragment, YXmlElement, YXmlText } from './Type/y-xml/y-xml.js'
|
||||
import BinaryDecoder from './Binary/Decoder.js'
|
||||
|
||||
import debug from 'debug'
|
||||
|
||||
function callTypesAfterTransaction (y) {
|
||||
y._transactionChangedTypes.forEach(function (parentSub, type) {
|
||||
type._callObserver(parentSub)
|
||||
})
|
||||
}
|
||||
|
||||
export default class Y extends NamedEventHandler {
|
||||
constructor (opts) {
|
||||
super()
|
||||
this._opts = opts
|
||||
this.userID = generateUserID()
|
||||
this.userID = opts._userID != null ? opts._userID : generateUserID()
|
||||
this.ds = new DeleteStore(this)
|
||||
this.os = new OperationStore(this)
|
||||
this.ss = new StateStore(this)
|
||||
@@ -34,6 +41,27 @@ export default class Y extends NamedEventHandler {
|
||||
this.connected = true
|
||||
this._missingStructs = new Map()
|
||||
this._readyToIntegrate = []
|
||||
this._transactionsInProgress = 0
|
||||
// types added during transaction
|
||||
this._transactionNewTypes = new Set()
|
||||
// changed types (does not include new types)
|
||||
this._transactionChangedTypes = new Map()
|
||||
this.on('afterTransaction', callTypesAfterTransaction)
|
||||
}
|
||||
_beforeChange () {}
|
||||
transact (f) {
|
||||
this._transactionsInProgress++
|
||||
try {
|
||||
f()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
this._transactionsInProgress--
|
||||
if (this._transactionsInProgress === 0) {
|
||||
this.emit('afterTransaction', this)
|
||||
this._transactionChangedTypes = new Map()
|
||||
this._transactionNewTypes = new Set()
|
||||
}
|
||||
}
|
||||
// fake _start for root properties (y.set('name', type))
|
||||
get _start () {
|
||||
@@ -102,9 +130,13 @@ Y.Persisence = Persistence
|
||||
Y.Array = YArray
|
||||
Y.Map = YMap
|
||||
Y.Text = YText
|
||||
Y.Xml = YXml
|
||||
Y.XmlElement = YXmlElement
|
||||
Y.XmlFragment = YXmlFragment
|
||||
Y.XmlText = YXmlText
|
||||
|
||||
export { default as debug } from 'debug'
|
||||
Y.utils = {
|
||||
BinaryDecoder
|
||||
}
|
||||
|
||||
Y.debug = debug
|
||||
debug.formatters.Y = messageToString
|
||||
|
||||
3
src/y-dist.cjs.js
Normal file
3
src/y-dist.cjs.js
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
import Y from './Y.js'
|
||||
export default Y
|
||||
Reference in New Issue
Block a user