Files
yjs/structs/ItemFormat.mjs

57 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
2018-11-25 03:17:00 +01:00
* @module structs
*/
2018-02-15 01:25:08 +01:00
2018-11-25 22:39:30 +01:00
import { Item } from './Item.mjs'
import { logItemHelper } from '../protocols/syncProtocol.mjs'
import * as encoding from '../lib/encoding.mjs'
import * as decoding from '../lib/decoding.mjs'
import { Y } from '../utils/Y.mjs' // eslint-disable-line
2018-11-25 03:17:00 +01:00
export class ItemFormat extends Item {
2018-02-15 01:25:08 +01:00
constructor () {
super()
this.key = null
this.value = null
}
_copy (undeleteChildren, copyPosition) {
let struct = super._copy()
2018-02-15 01:25:08 +01:00
struct.key = this.key
struct.value = this.value
return struct
}
get _length () {
return 1
}
get _countable () {
return false
}
/**
* @param {Y} y
* @param {decoding.Decoder} decoder
*/
2018-02-15 01:25:08 +01:00
_fromBinary (y, decoder) {
2018-02-26 02:18:39 +01:00
const missing = super._fromBinary(y, decoder)
this.key = decoding.readVarString(decoder)
this.value = JSON.parse(decoding.readVarString(decoder))
2018-02-15 01:25:08 +01:00
return missing
}
/**
* @param {encoding.Encoder} encoder
*/
2018-02-15 01:25:08 +01:00
_toBinary (encoder) {
super._toBinary(encoder)
encoding.writeVarString(encoder, this.key)
encoding.writeVarString(encoder, JSON.stringify(this.value))
2018-02-15 01:25:08 +01: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
*/
2018-02-15 01:25:08 +01:00
_logString () {
2018-03-29 11:58:02 +02:00
return logItemHelper('ItemFormat', this, `key:${JSON.stringify(this.key)},value:${JSON.stringify(this.value)}`)
2018-02-15 01:25:08 +01:00
}
}