after refactor - some tests are working again

This commit is contained in:
Kevin Jahns
2019-04-05 00:37:09 +02:00
parent 30bf3742c9
commit e56899a02c
23 changed files with 234 additions and 152 deletions

View File

@@ -68,12 +68,12 @@ export class AbstractType {
* * This type is sent to other client
* * Observer functions are fired
*
* @param {Transaction} transaction The Yjs instance
* @param {ItemType} item
* @param {Y} y The Yjs instance
* @param {ItemType|null} item
* @private
*/
_integrate (transaction, item) {
this._y = transaction.y
_integrate (y, item) {
this._y = y
this._item = item
}
@@ -87,9 +87,7 @@ export class AbstractType {
/**
* @param {encoding.Encoder} encoder
*/
_write (encoder) {
throw new Error('unimplemented')
}
_write (encoder) { }
/**
* The first non-deleted item
@@ -329,12 +327,19 @@ export const typeArrayGet = (type, index) => {
* @param {Array<Object<string,any>|Array<any>|number|string|ArrayBuffer>} content
*/
export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem, content) => {
let left = referenceItem
const left = referenceItem
const right = referenceItem === null ? parent._start : referenceItem.right
/**
* @type {Array<Object|Array|number>}
*/
let jsonContent = []
const packJsonContent = () => {
if (jsonContent.length > 0) {
const item = new ItemJSON(nextID(transaction), left, right, parent, null, jsonContent)
item.integrate(transaction)
jsonContent = []
}
}
content.forEach(c => {
switch (c.constructor) {
case Number:
@@ -344,11 +349,7 @@ export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem,
jsonContent.push(c)
break
default:
if (jsonContent.length > 0) {
const item = new ItemJSON(nextID(transaction), left, right, parent, null, jsonContent)
item.integrate(transaction)
jsonContent = []
}
packJsonContent()
switch (c.constructor) {
case ArrayBuffer:
// @ts-ignore c is definitely an ArrayBuffer
@@ -363,6 +364,7 @@ export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem,
}
}
})
packJsonContent()
}
/**
@@ -373,20 +375,22 @@ export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem,
*/
export const typeArrayInsertGenerics = (transaction, parent, index, content) => {
if (index === 0) {
typeArrayInsertGenericsAfter(transaction, parent, null, content)
return typeArrayInsertGenericsAfter(transaction, parent, null, content)
}
for (let n = parent._start; n !== null; n = n.right) {
let n = parent._start
for (; n !== null; n = n.right) {
if (!n.deleted && n.countable) {
if (index <= n.length) {
if (index < n.length) {
// insert in-between
getItemCleanStart(transaction.y.store, transaction, createID(n.id.client, n.id.clock + index))
}
return typeArrayInsertGenericsAfter(transaction, parent, n, content)
break
}
index -= n.length
}
}
throw new Error('Index exceeds array range')
return typeArrayInsertGenericsAfter(transaction, parent, n, content)
}
/**
@@ -440,6 +444,10 @@ export const typeMapDelete = (transaction, parent, key) => {
*/
export const typeMapSet = (transaction, parent, key, value) => {
const right = parent._map.get(key) || null
if (value == null) {
new ItemJSON(nextID(transaction), null, right, parent, key, [value]).integrate(transaction)
return
}
switch (value.constructor) {
case Number:
case Object:

View File

@@ -12,10 +12,12 @@ import {
typeArrayInsertGenerics,
typeArrayDelete,
typeArrayMap,
Transaction, ItemType, // eslint-disable-line
YArrayRefID,
Y, Transaction, ItemType, // eslint-disable-line
} from '../internals.js'
import * as decoding from 'lib0/decoding.js' // eslint-disable-line
import * as encoding from 'lib0/encoding.js'
/**
* Event that describes the changes on a YArray
@@ -52,12 +54,12 @@ export class YArray extends AbstractType {
* * This type is sent to other client
* * Observer functions are fired
*
* @param {Transaction} transaction The Yjs instance
* @param {Y} y The Yjs instance
* @param {ItemType} item
* @private
*/
_integrate (transaction, item) {
super._integrate(transaction, item)
_integrate (y, item) {
super._integrate(y, item)
// @ts-ignore
this.insert(0, this._prelimContent)
this._prelimContent = null
@@ -183,6 +185,13 @@ export class YArray extends AbstractType {
push (content) {
this.insert(this.length, content)
}
/**
* @param {encoding.Encoder} encoder
*/
_write (encoder) {
encoding.writeVarUint(encoder, YArrayRefID)
}
}
/**

View File

@@ -10,9 +10,11 @@ import {
typeMapGet,
typeMapHas,
createMapIterator,
Transaction, ItemType, // eslint-disable-line
YMapRefID,
Y, Transaction, ItemType, // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js' // eslint-disable-line
import * as iterator from 'lib0/iterator.js'
@@ -53,12 +55,12 @@ export class YMap extends AbstractType {
* * This type is sent to other client
* * Observer functions are fired
*
* @param {Transaction} transaction The Yjs instance
* @param {Y} y The Yjs instance
* @param {ItemType} item
* @private
*/
_integrate (transaction, item) {
super._integrate(transaction, item)
_integrate (y, item) {
super._integrate(y, item)
// @ts-ignore
for (let [key, value] of this._prelimContent) {
this.set(key, value)
@@ -88,7 +90,8 @@ export class YMap extends AbstractType {
const map = {}
for (let [key, item] of this._map) {
if (!item.deleted) {
map[key] = item.getContent()[0]
const v = item.getContent()[0]
map[key] = v instanceof AbstractType ? v.toJSON() : v
}
}
return map
@@ -169,6 +172,13 @@ export class YMap extends AbstractType {
has (key) {
return typeMapHas(this, key)
}
/**
* @param {encoding.Encoder} encoder
*/
_write (encoder) {
encoding.writeVarUint(encoder, YMapRefID)
}
}
/**

View File

@@ -12,10 +12,12 @@ import {
createID,
getItemCleanStart,
isVisible,
ItemType, AbstractItem, Snapshot, StructStore, Transaction // eslint-disable-line
YTextRefID,
Y, ItemType, AbstractItem, Snapshot, StructStore, Transaction // eslint-disable-line
} from '../internals.js'
import * as decoding from 'lib0/decoding.js' // eslint-disable-line
import * as encoding from 'lib0/encoding.js'
/**
* @private
@@ -566,11 +568,11 @@ export class YText extends AbstractType {
}
/**
* @param {Transaction} transaction
* @param {Y} y
* @param {ItemType} item
*/
_integrate (transaction, item) {
super._integrate(transaction, item)
_integrate (y, item) {
super._integrate(y, item)
// @ts-ignore this._prelimContent is still defined
this.insert(0, this._prelimContent.join(''))
this._prelimContent = null
@@ -839,6 +841,13 @@ export class YText extends AbstractType {
})
}
}
/**
* @param {encoding.Encoder} encoder
*/
_write (encoder) {
encoding.writeVarUint(encoder, YTextRefID)
}
}
/**

View File

@@ -13,7 +13,8 @@ import {
typeArrayDelete,
typeMapSet,
typeMapDelete,
Transaction, ItemType, YXmlText, YXmlHook, Snapshot // eslint-disable-line
YXmlElementRefID,
Y, Transaction, ItemType, YXmlText, YXmlHook, Snapshot // eslint-disable-line
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
@@ -259,12 +260,12 @@ export class YXmlElement extends YXmlFragment {
* * This type is sent to other client
* * Observer functions are fired
*
* @param {Transaction} transaction The Yjs instance
* @param {Y} y The Yjs instance
* @param {ItemType} item
* @private
*/
_integrate (transaction, item) {
super._integrate(transaction, item)
_integrate (y, item) {
super._integrate(y, item)
// @ts-ignore
this.insert(0, this._prelimContent)
this._prelimContent = null
@@ -285,19 +286,6 @@ export class YXmlElement extends YXmlFragment {
return new YXmlElement(this.nodeName)
}
/**
* 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.
*
* @private
* @param {encoding.Encoder} encoder The encoder to write data to.
*/
_write (encoder) {
encoding.writeVarString(encoder, this.nodeName)
}
toString () {
return this.toDomString()
}
@@ -460,6 +448,20 @@ export class YXmlElement extends YXmlFragment {
}
return dom
}
/**
* 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.
*
* @private
* @param {encoding.Encoder} encoder The encoder to write data to.
*/
_write (encoder) {
encoding.writeVarUint(encoder, YXmlElementRefID)
encoding.writeVarString(encoder, this.nodeName)
}
}
/**

View File

@@ -2,7 +2,10 @@
* @module types
*/
import { YMap } from '../internals.js'
import {
YMap,
YXmlHookRefID
} from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js'
@@ -71,6 +74,7 @@ export class YXmlHook extends YMap {
*/
_write (encoder) {
super._write(encoder)
encoding.writeVarUint(encoder, YXmlHookRefID)
encoding.writeVarString(encoder, this.hookName)
}
}

View File

@@ -2,8 +2,9 @@
* @module types
*/
import { YText } from '../internals.js'
import { YText, YXmlTextRefID } from '../internals.js'
import * as encoding from 'lib0/encoding.js'
import * as decoding from 'lib0/decoding.js' // eslint-disable-line
/**
@@ -33,6 +34,12 @@ export class YXmlText extends YText {
}
return dom
}
/**
* @param {encoding.Encoder} encoder
*/
_write (encoder) {
encoding.writeVarUint(encoder, YXmlTextRefID)
}
}
/**