/
dimamir
/
yjs
Обзор
Документация
Войти
/
dimamir
/
yjs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/structs/ContentJSON.js
118 строк
2 KB
Kevin Jahns
test with all encoders
29 дек 2020, 18:59
29 дек 2020, 18:59
0a40b54
Код
Авторство
О чём код?
import { UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Transaction, Item, StructStore // eslint-disable-line } from '../internals.js' /** * @private */ export class ContentJSON { /** * @param {Array<any>} arr */ constructor (arr) { /** * @type {Array<any>} */ this.arr = arr } /** * @return {number} */ getLength () { return this.arr.length } /** * @return {Array<any>} */ getContent () { return this.arr } /** * @return {boolean} */ isCountable () { return true } /** * @return {ContentJSON} */ copy () { return new ContentJSON(this.arr) } /** * @param {number} offset * @return {ContentJSON} */ splice (offset) { const right = new ContentJSON(this.arr.slice(offset)) this.arr = this.arr.slice(0, offset) return right } /** * @param {ContentJSON} right * @return {boolean} */ mergeWith (right) { this.arr = this.arr.concat(right.arr) return true } /** * @param {Transaction} transaction * @param {Item} item */ integrate (transaction, item) {} /** * @param {Transaction} transaction */ delete (transaction) {} /** * @param {StructStore} store */ gc (store) {} /** * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder * @param {number} offset */ write (encoder, offset) { const len = this.arr.length encoder.writeLen(len - offset) for (let i = offset; i < len; i++) { const c = this.arr[i] encoder.writeString(c === undefined ? 'undefined' : JSON.stringify(c)) } } /** * @return {number} */ getRef () { return 2 } } /** * @private * * @param {UpdateDecoderV1 | UpdateDecoderV2} decoder * @return {ContentJSON} */ export const readContentJSON = decoder => { const len = decoder.readLen() const cs = [] for (let i = 0; i < len; i++) { const c = decoder.readString() if (c === 'undefined') { cs.push(undefined) } else { cs.push(JSON.parse(c)) } } return new ContentJSON(cs) }