Files
yjs/src/Struct/Delete.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-03-06 05:22:18 +01:00
import { getStructReference } from '../Util/structReferences.js'
import ID from '../Util/ID/ID.js'
2017-10-26 19:12:33 +02:00
import { logID } from '../MessageHandler/messageToString.js'
2017-10-11 03:41:54 +02:00
/**
* @private
* Delete all items in an ID-range
* TODO: implement getItemCleanStartNode for better performance (only one lookup)
*/
2017-10-16 04:53:12 +02:00
export function deleteItemRange (y, user, clock, range) {
2017-12-24 03:18:00 +01:00
const createDelete = y.connector !== null && y.connector._forwardAppliedStructs
let item = y.os.getItemCleanStart(new ID(user, clock))
if (item !== null) {
if (!item._deleted) {
item._splitAt(y, range)
2018-03-29 18:24:14 +02:00
item._delete(y, createDelete, true)
}
let itemLen = item._length
range -= itemLen
clock += itemLen
if (range > 0) {
let node = y.os.findNode(new ID(user, clock))
2018-03-29 18:24:14 +02:00
while (node !== null && node.val !== null && range > 0 && node.val._id.equals(new ID(user, clock))) {
const nodeVal = node.val
if (!nodeVal._deleted) {
nodeVal._splitAt(y, range)
2018-03-29 18:24:14 +02:00
nodeVal._delete(y, createDelete, true)
}
const nodeLen = nodeVal._length
range -= nodeLen
clock += nodeLen
node = node.next()
}
}
2017-10-16 04:53:12 +02:00
}
}
/**
* @private
* A Delete change is not a real Item, but it provides the same interface as an
* Item. The only difference is that it will not be saved in the ItemStore
* (OperationStore), but instead it is safed in the DeleteStore.
2017-10-16 04:53:12 +02:00
*/
2017-10-11 03:41:54 +02:00
export default class Delete {
constructor () {
2017-10-26 20:53:17 +02:00
this._target = null
2017-10-11 03:41:54 +02:00
this._length = null
}
/**
* @private
* Read the next Item in a Decoder and fill this Item with the read data.
*
* This is called when data is received from a remote peer.
*
* @param {Y} y The Yjs instance that this Item belongs to.
* @param {BinaryDecoder} decoder The decoder object to read data from.
*/
2017-10-11 03:41:54 +02:00
_fromBinary (y, decoder) {
2017-10-26 19:12:33 +02:00
// TODO: set target, and add it to missing if not found
// There is an edge case in p2p networks!
2017-10-26 20:53:17 +02:00
const targetID = decoder.readID()
this._targetID = targetID
2017-10-11 03:41:54 +02:00
this._length = decoder.readVarUint()
2017-10-26 20:53:17 +02:00
if (y.os.getItem(targetID) === null) {
return [targetID]
} else {
return []
}
2017-10-11 03:41:54 +02:00
}
/**
* @private
* Transform the properties of this type to binary and write it to an
* BinaryEncoder.
*
* This is called when this Item is sent to a remote peer.
*
* @param {BinaryEncoder} encoder The encoder to write data to.
*/
2017-10-16 04:53:12 +02:00
_toBinary (encoder) {
2018-03-06 05:22:18 +01:00
encoder.writeUint8(getStructReference(this.constructor))
2017-10-16 04:53:12 +02:00
encoder.writeID(this._targetID)
2017-10-11 03:41:54 +02:00
encoder.writeVarUint(this._length)
}
2017-10-16 04:53:12 +02:00
/**
* @private
* Integrates this Item into the shared structure.
*
* This method actually applies the change to the Yjs instance. In the case of
* Delete it marks the delete target as deleted.
*
* * If created remotely (a remote user deleted something),
2017-10-16 04:53:12 +02:00
* this Delete is applied to all structs in id-range.
* * If created lokally (e.g. when y-array deletes a range of elements),
2017-10-16 04:53:12 +02:00
* this struct is broadcasted only (it is already executed)
*/
_integrate (y, locallyCreated = false) {
if (!locallyCreated) {
// from remote
const id = this._targetID
deleteItemRange(y, id.user, id.clock, this._length)
2017-12-24 03:18:00 +01:00
} else if (y.connector !== null) {
2017-10-16 04:53:12 +02:00
// from local
y.connector.broadcastStruct(this)
2017-10-11 03:41:54 +02:00
}
if (y.persistence !== null) {
2017-12-24 03:18:00 +01:00
y.persistence.saveStruct(y, this)
2017-10-11 03:41:54 +02:00
}
}
/**
2018-03-29 11:58:02 +02:00
* Transform this YXml Type to a readable format.
* Useful for logging as all Items and Delete implement this method.
*
* @private
*/
2017-10-11 03:41:54 +02:00
_logString () {
2017-10-26 19:12:33 +02:00
return `Delete - target: ${logID(this._targetID)}, len: ${this._length}`
2017-10-11 03:41:54 +02:00
}
}