all YArray.tests type fixes

This commit is contained in:
Kevin Jahns
2019-04-03 02:30:44 +02:00
parent e23582b1cd
commit 415de1cc4c
17 changed files with 383 additions and 180 deletions

View File

@@ -95,7 +95,7 @@ export class AbstractType {
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
*/
_callObserver (transaction, parentSubs) {
this._callEventHandler(transaction, new YEvent(this))
this._callEventHandler(transaction, new YEvent(this, transaction))
}
/**
@@ -125,7 +125,7 @@ export class AbstractType {
/**
* Observe all events that are created on this type.
*
* @param {Function} f Observer function
* @param {function(YEvent):void} f Observer function
*/
observe (f) {
this._eventHandler.addEventListener(f)
@@ -223,6 +223,7 @@ export const typeArrayMap = (type, f) => {
/**
* @param {AbstractType} type
* @return {{next:function():{done:boolean,value:any|undefined}}}
*/
export const typeArrayCreateIterator = type => {
let n = type._start
@@ -242,7 +243,8 @@ export const typeArrayCreateIterator = type => {
// check if we reached the end, no need to check currentContent, because it does not exist
if (n === null) {
return {
done: true
done: true,
value: undefined
}
}
// currentContent could exist from the last iteration

View File

@@ -18,50 +18,8 @@ export class YArrayEvent extends YEvent {
* @param {Transaction} transaction The transaction object
*/
constructor (yarray, transaction) {
super(yarray)
super(yarray, transaction)
this._transaction = transaction
this._addedElements = null
this._removedElements = null
}
/**
* Child elements that were added in this transaction.
*
* @return {Set<AbstractItem>}
*/
get addedElements () {
if (this._addedElements === null) {
const target = this.target
const transaction = this._transaction
const addedElements = new Set()
transaction.added.forEach(type => {
if (type.parent === target && !transaction.deleted.has(type)) {
addedElements.add(type)
}
})
this._addedElements = addedElements
}
return this._addedElements
}
/**
* Child elements that were removed in this transaction.
*
* @return {Set<AbstractItem>}
*/
get removedElements () {
if (this._removedElements === null) {
const target = this.target
const transaction = this._transaction
const removedElements = new Set()
transaction.deleted.forEach(struct => {
if (struct.parent === target && !transaction.added.has(struct)) {
removedElements.add(struct)
}
})
this._removedElements = removedElements
}
return this._removedElements
}
}

View File

@@ -2,7 +2,6 @@
* @module types
*/
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import { AbstractType, typeMapDelete, typeMapSet, typeMapGet, typeMapHas, createMapIterator } from './AbstractType.js'
import { ItemType } from '../structs/ItemType.js' // eslint-disable-line
import { YEvent } from '../utils/YEvent.js'
@@ -11,20 +10,23 @@ import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
import * as iterator from 'lib0/iterator.js'
/**
* @template T
* Event that describes the changes on a YMap.
*/
export class YMapEvent extends YEvent {
/**
* @param {YMap} ymap The YArray that changed.
* @param {YMap<T>} ymap The YArray that changed.
* @param {Transaction} transaction
* @param {Set<any>} subs The keys that changed.
*/
constructor (ymap, subs) {
super(ymap)
constructor (ymap, transaction, subs) {
super(ymap, transaction)
this.keysChanged = subs
}
}
/**
* @template T number|string|Object|Array|ArrayBuffer
* A shared Map implementation.
*/
export class YMap extends AbstractType {
@@ -62,17 +64,17 @@ export class YMap extends AbstractType {
* @param {Set<null|string>} parentSubs Keys changed on this type. `null` if list was modified.
*/
_callObserver (transaction, parentSubs) {
this._callEventHandler(transaction, new YMapEvent(this, parentSubs))
this._callEventHandler(transaction, new YMapEvent(this, transaction, parentSubs))
}
/**
* Transforms this Shared Type to a JSON object.
*
* @return {Object<string,number|string|Object|Array|ArrayBuffer>}
* @return {Object<string,T>}
*/
toJSON () {
/**
* @type {Object<string,number|string|Object|Array|ArrayBuffer>}
* @type {Object<string,T>}
*/
const map = {}
for (let [key, item] of this._map) {
@@ -94,7 +96,7 @@ export class YMap extends AbstractType {
/**
* Returns the value for each element in the YMap Type.
*
* @return {Iterator<string|number|ArrayBuffer|Object<string,any>|Array<any>>}
* @return {Iterator<T>}
*/
entries () {
return iterator.iteratorMap(createMapIterator(this._map), v => v[1].getContent()[0])
@@ -124,7 +126,7 @@ export class YMap extends AbstractType {
* Adds or updates an element with a specified key and value.
*
* @param {string} key The key of the element to add to this YMap
* @param {Object | string | number | AbstractType | ArrayBuffer } value The value of the element to add
* @param {T} value The value of the element to add
*/
set (key, value) {
if (this._y !== null) {
@@ -142,9 +144,10 @@ export class YMap extends AbstractType {
* Returns a specified element from this YMap.
*
* @param {string} key
* @return {Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType|undefined}
* @return {T|undefined}
*/
get (key) {
// @ts-ignore
return typeMapGet(this, key)
}

View File

@@ -355,8 +355,6 @@ class YTextEvent extends YArrayEvent {
* @type {Array<{delete:number|undefined,retain:number|undefined,insert:string|undefined,attributes:Object<string,any>}>}
*/
const delta = []
const added = this.addedElements
const removed = this.removedElements
const currentAttributes = new Map() // saves all current attributes for insert
const oldAttributes = new Map()
let item = this.target._start
@@ -413,19 +411,19 @@ class YTextEvent extends YArrayEvent {
while (item !== null) {
switch (item.constructor) {
case ItemEmbed:
if (added.has(item)) {
if (this.adds(item)) {
addOp()
action = 'insert'
// @ts-ignore item is ItemFormat
insert = item.embed
addOp()
} else if (removed.has(item)) {
} else if (this.deletes(item)) {
if (action !== 'delete') {
addOp()
action = 'delete'
}
deleteLen += 1
} else if (item.deleted === false) {
} else if (!item.deleted) {
if (action !== 'retain') {
addOp()
action = 'retain'
@@ -434,20 +432,20 @@ class YTextEvent extends YArrayEvent {
}
break
case ItemString:
if (added.has(item)) {
if (this.adds(item)) {
if (action !== 'insert') {
addOp()
action = 'insert'
}
// @ts-ignore
insert += item.string
} else if (removed.has(item)) {
} else if (this.deletes(item)) {
if (action !== 'delete') {
addOp()
action = 'delete'
}
deleteLen += item.length
} else if (item.deleted === false) {
} else if (!item.deleted) {
if (action !== 'retain') {
addOp()
action = 'retain'
@@ -456,7 +454,7 @@ class YTextEvent extends YArrayEvent {
}
break
case ItemFormat:
if (added.has(item)) {
if (this.adds(item)) {
// @ts-ignore item is ItemFormat
const curVal = currentAttributes.get(item.key) || null
// @ts-ignore item is ItemFormat
@@ -475,7 +473,7 @@ class YTextEvent extends YArrayEvent {
} else {
item.delete(transaction)
}
} else if (removed.has(item)) {
} else if (this.deletes(item)) {
// @ts-ignore item is ItemFormat
oldAttributes.set(item.key, item.value)
// @ts-ignore item is ItemFormat
@@ -488,7 +486,7 @@ class YTextEvent extends YArrayEvent {
// @ts-ignore item is ItemFormat
attributes[item.key] = curVal
}
} else if (item.deleted === false) {
} else if (!item.deleted) {
// @ts-ignore item is ItemFormat
oldAttributes.set(item.key, item.value)
// @ts-ignore item is ItemFormat
@@ -512,7 +510,7 @@ class YTextEvent extends YArrayEvent {
}
}
}
if (item.deleted === false) {
if (!item.deleted) {
if (action === 'insert') {
addOp()
}