Files
yjs/structs/Delete.js

105 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-11-25 03:17:00 +01:00
/**
* @module structs
*/
2018-11-27 14:59:12 +01:00
import { getStructReference } from '../utils/structReferences.js'
import * as ID from '../utils/ID.js'
import { stringifyID } from '../protocols/syncProtocol.js'
import { writeStructToTransaction } from '../utils/Transaction.js'
import * as decoding from '../lib/decoding.js'
import * as encoding from '../lib/encoding.js'
import { Item } from './Item.js' // eslint-disable-line
import { Y } from '../utils/Y.js' // eslint-disable-line
import { deleteItemRange } from '../utils/structManipulation.js'
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
*/
2018-11-25 03:17:00 +01:00
export class Delete {
2017-10-11 03:41:54 +02:00
constructor () {
/**
* @type {ID.ID}
*/
this._targetID = null
/**
2018-11-25 03:17:00 +01:00
* @type {Item}
*/
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.
*
2018-11-25 03:17:00 +01:00
* @param {Y} y The Yjs instance that this Item belongs to.
* @param {decoding.Decoder} 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!
/**
* @type {any}
*/
const targetID = ID.decode(decoder)
2017-10-26 20:53:17 +02:00
this._targetID = targetID
this._length = decoding.readVarUint(decoder)
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 {encoding.Encoder} encoder The encoder to write data to.
*/
2017-10-16 04:53:12 +02:00
_toBinary (encoder) {
encoding.writeUint8(encoder, getStructReference(this.constructor))
this._targetID.encode(encoder)
encoding.writeVarUint(encoder, this._length)
2017-10-11 03:41:54 +02:00
}
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, false)
2017-10-11 03:41:54 +02:00
}
writeStructToTransaction(y._transaction, 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 () {
return `Delete - target: ${stringifyID(this._targetID)}, len: ${this._length}`
2017-10-11 03:41:54 +02:00
}
}