implement getMap, getArray, getXml, ..

This commit is contained in:
Kevin Jahns 2019-04-03 03:08:10 +02:00
parent 415de1cc4c
commit 92ca001cdc
23 changed files with 159 additions and 103 deletions

View File

@ -64,7 +64,7 @@ export class AbstractItem extends AbstractStruct {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType | null} parent * @param {AbstractType<any> | null} parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
constructor (id, left, right, parent, parentSub) { constructor (id, left, right, parent, parentSub) {
@ -102,7 +102,7 @@ export class AbstractItem extends AbstractStruct {
this.rightOrigin = right this.rightOrigin = right
/** /**
* The parent type. * The parent type.
* @type {AbstractType} * @type {AbstractType<any>}
* @readonly * @readonly
*/ */
this.parent = parent this.parent = parent
@ -266,7 +266,7 @@ export class AbstractItem extends AbstractStruct {
* @param {ID} id * @param {ID} id
* @param {AbstractItem|null} left * @param {AbstractItem|null} left
* @param {AbstractItem|null} right * @param {AbstractItem|null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string|null} parentSub * @param {string|null} parentSub
* @return {AbstractItem} * @return {AbstractItem}
*/ */

View File

@ -20,7 +20,7 @@ export class ItemBinary extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {ArrayBuffer} content * @param {ArrayBuffer} content
*/ */
@ -35,7 +35,7 @@ export class ItemBinary extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
copy (id, left, right, parent, parentSub) { copy (id, left, right, parent, parentSub) {

View File

@ -19,7 +19,7 @@ export class ItemDeleted extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {number} length * @param {number} length
*/ */
@ -31,7 +31,7 @@ export class ItemDeleted extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
copy (id, left, right, parent, parentSub) { copy (id, left, right, parent, parentSub) {

View File

@ -19,7 +19,7 @@ export class ItemEmbed extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {Object} embed * @param {Object} embed
*/ */
@ -31,7 +31,7 @@ export class ItemEmbed extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
copy (id, left, right, parent, parentSub) { copy (id, left, right, parent, parentSub) {

View File

@ -19,7 +19,7 @@ export class ItemFormat extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {string} key * @param {string} key
* @param {any} value * @param {any} value
@ -33,7 +33,7 @@ export class ItemFormat extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any> parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
copy (id, left, right, parent, parentSub) { copy (id, left, right, parent, parentSub) {

View File

@ -19,7 +19,7 @@ export class ItemJSON extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {Array<any>} content * @param {Array<any>} content
*/ */
@ -34,7 +34,7 @@ export class ItemJSON extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any> parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
copy (id, left, right, parent, parentSub) { copy (id, left, right, parent, parentSub) {

View File

@ -18,7 +18,7 @@ export class ItemString extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {string} string * @param {string} string
*/ */
@ -33,7 +33,7 @@ export class ItemString extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any> parent
* @param {string | null} parentSub * @param {string | null} parentSub
*/ */
copy (id, left, right, parent, parentSub) { copy (id, left, right, parent, parentSub) {

View File

@ -35,7 +35,7 @@ const gcChildren = (y, item) => {
export const structTypeRefNumber = 7 export const structTypeRefNumber = 7
/** /**
* @type {Array<function(decoding.Decoder):AbstractType>} * @type {Array<function(decoding.Decoder):AbstractType<any>>}
*/ */
export const typeRefs = [ export const typeRefs = [
readYArray, readYArray,
@ -52,9 +52,9 @@ export class ItemType extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @param {AbstractType} type * @param {AbstractType<any>} type
*/ */
constructor (id, left, right, parent, parentSub, type) { constructor (id, left, right, parent, parentSub, type) {
super(id, left, right, parent, parentSub) super(id, left, right, parent, parentSub)
@ -67,7 +67,7 @@ export class ItemType extends AbstractItem {
* @param {ID} id * @param {ID} id
* @param {AbstractItem | null} left * @param {AbstractItem | null} left
* @param {AbstractItem | null} right * @param {AbstractItem | null} right
* @param {AbstractType} parent * @param {AbstractType<any> parent
* @param {string | null} parentSub * @param {string | null} parentSub
* @return {AbstractItem} TODO, returns itemtype * @return {AbstractItem} TODO, returns itemtype
*/ */
@ -150,7 +150,7 @@ export class ItemTypeRef extends AbstractItemRef {
super(decoder, id, info) super(decoder, id, info)
const typeRef = decoding.readVarUint(decoder) const typeRef = decoding.readVarUint(decoder)
/** /**
* @type {AbstractType} * @type {AbstractType<any>
*/ */
this.type = typeRefs[typeRef](decoder) this.type = typeRefs[typeRef](decoder)
} }

View File

@ -18,6 +18,7 @@ import { getItemCleanStart, getItemCleanEnd } from '../utils/StructStore.js'
import * as iterator from 'lib0/iterator.js' import * as iterator from 'lib0/iterator.js'
/** /**
* @template EventType
* Abstract Yjs Type class * Abstract Yjs Type class
*/ */
export class AbstractType { export class AbstractType {
@ -63,7 +64,7 @@ export class AbstractType {
} }
/** /**
* @return {AbstractType} * @return {AbstractType<EventType>}
*/ */
_copy () { _copy () {
throw new Error('unimplemented') throw new Error('unimplemented')
@ -110,10 +111,11 @@ export class AbstractType {
const changedParentTypes = transaction.changedParentTypes const changedParentTypes = transaction.changedParentTypes
this._eventHandler.callEventListeners(transaction, event) this._eventHandler.callEventListeners(transaction, event)
/** /**
* @type {AbstractType} * @type {AbstractType<EventType>}
*/ */
let type = this let type = this
while (true) { while (true) {
// @ts-ignore
map.setIfUndefined(changedParentTypes, type, () => []).push(event) map.setIfUndefined(changedParentTypes, type, () => []).push(event)
if (type._item === null) { if (type._item === null) {
break break
@ -125,7 +127,7 @@ export class AbstractType {
/** /**
* Observe all events that are created on this type. * Observe all events that are created on this type.
* *
* @param {function(YEvent):void} f Observer function * @param {function(EventType):void} f Observer function
*/ */
observe (f) { observe (f) {
this._eventHandler.addEventListener(f) this._eventHandler.addEventListener(f)
@ -134,7 +136,7 @@ export class AbstractType {
/** /**
* Observe all events that are created by this type and its children. * Observe all events that are created by this type and its children.
* *
* @param {Function} f Observer function * @param {function(Array<YEvent>):void} f Observer function
*/ */
observeDeep (f) { observeDeep (f) {
this._deepEventHandler.addEventListener(f) this._deepEventHandler.addEventListener(f)
@ -166,7 +168,7 @@ export class AbstractType {
} }
/** /**
* @param {AbstractType} type * @param {AbstractType<any>} type
* @return {Array<any>} * @return {Array<any>}
*/ */
export const typeArrayToArray = type => { export const typeArrayToArray = type => {
@ -187,8 +189,8 @@ export const typeArrayToArray = type => {
/** /**
* Executes a provided function on once on overy element of this YArray. * Executes a provided function on once on overy element of this YArray.
* *
* @param {AbstractType} type * @param {AbstractType<any>} type
* @param {function(any,number,AbstractType):void} f A function to execute on every element of this YArray. * @param {function(any,number,AbstractType<any>):void} f A function to execute on every element of this YArray.
*/ */
export const typeArrayForEach = (type, f) => { export const typeArrayForEach = (type, f) => {
let index = 0 let index = 0
@ -206,8 +208,8 @@ export const typeArrayForEach = (type, f) => {
/** /**
* @template C,R * @template C,R
* @param {AbstractType} type * @param {AbstractType<any>} type
* @param {function(C,number,AbstractType):R} f * @param {function(C,number,AbstractType<any>):R} f
* @return {Array<R>} * @return {Array<R>}
*/ */
export const typeArrayMap = (type, f) => { export const typeArrayMap = (type, f) => {
@ -222,7 +224,7 @@ export const typeArrayMap = (type, f) => {
} }
/** /**
* @param {AbstractType} type * @param {AbstractType<any>} type
* @return {{next:function():{done:boolean,value:any|undefined}}} * @return {{next:function():{done:boolean,value:any|undefined}}}
*/ */
export const typeArrayCreateIterator = type => { export const typeArrayCreateIterator = type => {
@ -270,8 +272,8 @@ export const typeArrayCreateIterator = type => {
* Executes a provided function on once on overy element of this YArray. * Executes a provided function on once on overy element of this YArray.
* Operates on a snapshotted state of the document. * Operates on a snapshotted state of the document.
* *
* @param {AbstractType} type * @param {AbstractType<any>} type
* @param {function(any,number,AbstractType):void} f A function to execute on every element of this YArray. * @param {function(any,number,AbstractType<any>):void} f A function to execute on every element of this YArray.
* @param {Snapshot} snapshot * @param {Snapshot} snapshot
*/ */
export const typeArrayForEachSnapshot = (type, f, snapshot) => { export const typeArrayForEachSnapshot = (type, f, snapshot) => {
@ -289,7 +291,7 @@ export const typeArrayForEachSnapshot = (type, f, snapshot) => {
} }
/** /**
* @param {AbstractType} type * @param {AbstractType<any>} type
* @param {number} index * @param {number} index
* @return {any} * @return {any}
*/ */
@ -306,7 +308,7 @@ export const typeArrayGet = (type, index) => {
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractItem?} referenceItem * @param {AbstractItem?} referenceItem
* @param {Array<Object<string,any>|Array<any>|number|string|ArrayBuffer>} content * @param {Array<Object<string,any>|Array<any>|number|string|ArrayBuffer>} content
*/ */
@ -349,7 +351,7 @@ export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem,
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {number} index * @param {number} index
* @param {Array<Object<string,any>|Array<any>|number|string|ArrayBuffer>} content * @param {Array<Object<string,any>|Array<any>|number|string|ArrayBuffer>} content
*/ */
@ -373,7 +375,7 @@ export const typeArrayInsertGenerics = (transaction, parent, index, content) =>
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {number} index * @param {number} index
* @param {number} length * @param {number} length
*/ */
@ -404,7 +406,7 @@ export const typeArrayDelete = (transaction, parent, index, length) => {
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string} key * @param {string} key
*/ */
export const typeMapDelete = (transaction, parent, key) => { export const typeMapDelete = (transaction, parent, key) => {
@ -416,9 +418,9 @@ export const typeMapDelete = (transaction, parent, key) => {
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string} key * @param {string} key
* @param {Object|number|Array<any>|string|ArrayBuffer|AbstractType} value * @param {Object|number|Array<any>|string|ArrayBuffer|AbstractType<any>} value
*/ */
export const typeMapSet = (transaction, parent, key, value) => { export const typeMapSet = (transaction, parent, key, value) => {
const right = parent._map.get(key) || null const right = parent._map.get(key) || null
@ -442,9 +444,9 @@ export const typeMapSet = (transaction, parent, key, value) => {
} }
/** /**
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string} key * @param {string} key
* @return {Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType|undefined} * @return {Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType<any>|undefined}
*/ */
export const typeMapGet = (parent, key) => { export const typeMapGet = (parent, key) => {
const val = parent._map.get(key) const val = parent._map.get(key)
@ -452,8 +454,8 @@ export const typeMapGet = (parent, key) => {
} }
/** /**
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @return {Object<string,Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType|undefined>} * @return {Object<string,Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType<any>|undefined>}
*/ */
export const typeMapGetAll = (parent) => { export const typeMapGetAll = (parent) => {
/** /**
@ -469,7 +471,7 @@ export const typeMapGetAll = (parent) => {
} }
/** /**
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string} key * @param {string} key
* @return {boolean} * @return {boolean}
*/ */
@ -479,10 +481,10 @@ export const typeMapHas = (parent, key) => {
} }
/** /**
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {string} key * @param {string} key
* @param {Snapshot} snapshot * @param {Snapshot} snapshot
* @return {Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType|undefined} * @return {Object<string,any>|number|Array<any>|string|ArrayBuffer|AbstractType<any>|undefined}
*/ */
export const typeMapGetSnapshot = (parent, key, snapshot) => { export const typeMapGetSnapshot = (parent, key, snapshot) => {
let v = parent._map.get(key) || null let v = parent._map.get(key) || null

View File

@ -11,10 +11,11 @@ import * as decoding from 'lib0/decoding.js' // eslint-disable-line
/** /**
* Event that describes the changes on a YArray * Event that describes the changes on a YArray
* @template T
*/ */
export class YArrayEvent extends YEvent { export class YArrayEvent extends YEvent {
/** /**
* @param {AbstractType} yarray The changed type * @param {YArray<T>} yarray The changed type
* @param {Transaction} transaction The transaction object * @param {Transaction} transaction The transaction object
*/ */
constructor (yarray, transaction) { constructor (yarray, transaction) {
@ -26,6 +27,7 @@ export class YArrayEvent extends YEvent {
/** /**
* A shared Array implementation. * A shared Array implementation.
* @template T * @template T
* @extends AbstractType<YArrayEvent<T>>
*/ */
export class YArray extends AbstractType { export class YArray extends AbstractType {
constructor () { constructor () {
@ -149,10 +151,10 @@ export class YArray extends AbstractType {
* // Insert character 'a' at position 0 * // Insert character 'a' at position 0
* yarray.insert(0, ['a']) * yarray.insert(0, ['a'])
* // Insert numbers 1, 2 at position 1 * // Insert numbers 1, 2 at position 1
* yarray.insert(2, [1, 2]) * yarray.insert(1, [1, 2])
* *
* @param {number} index The index to insert content at. * @param {number} index The index to insert content at.
* @param {Array<number|string|ArrayBuffer|AbstractType>} content The array of content * @param {Array<T>} content The array of content
*/ */
insert (index, content) { insert (index, content) {
if (this._y !== null) { if (this._y !== null) {
@ -168,7 +170,7 @@ export class YArray extends AbstractType {
/** /**
* Appends content to this YArray. * Appends content to this YArray.
* *
* @param {Array<number|string|ArrayBuffer|AbstractType>} content Array of content to append. * @param {Array<T>} content Array of content to append.
*/ */
push (content) { push (content) {
this.insert(this.length, content) this.insert(this.length, content)

View File

@ -28,6 +28,8 @@ export class YMapEvent extends YEvent {
/** /**
* @template T number|string|Object|Array|ArrayBuffer * @template T number|string|Object|Array|ArrayBuffer
* A shared Map implementation. * A shared Map implementation.
*
* @extends AbstractType<YMapEvent<T>>
*/ */
export class YMap extends AbstractType { export class YMap extends AbstractType {
constructor () { constructor () {

View File

@ -5,7 +5,7 @@
import { ItemEmbed } from '../structs/ItemEmbed.js' import { ItemEmbed } from '../structs/ItemEmbed.js'
import { ItemString } from '../structs/ItemString.js' import { ItemString } from '../structs/ItemString.js'
import { ItemFormat } from '../structs/ItemFormat.js' import { ItemFormat } from '../structs/ItemFormat.js'
import { YArrayEvent } from './YArray.js' import { YEvent } from '../utils/YEvent.js'
import { ItemType } from '../structs/ItemType.js' // eslint-disable-line import { ItemType } from '../structs/ItemType.js' // eslint-disable-line
import { AbstractType } from './AbstractType.js' import { AbstractType } from './AbstractType.js'
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
@ -57,7 +57,7 @@ const findNextPosition = (transaction, store, currentAttributes, left, right, co
* @private * @private
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {StructStore} store * @param {StructStore} store
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {number} index * @param {number} index
* @return {{left:AbstractItem|null,right:AbstractItem|null,currentAttributes:Map<string,any>}} * @return {{left:AbstractItem|null,right:AbstractItem|null,currentAttributes:Map<string,any>}}
*/ */
@ -73,7 +73,7 @@ const findPosition = (transaction, store, parent, index) => {
* *
* @private * @private
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractItem|null} left * @param {AbstractItem|null} left
* @param {AbstractItem|null} right * @param {AbstractItem|null} right
* @param {Map<string,any>} negatedAttributes * @param {Map<string,any>} negatedAttributes
@ -151,7 +151,7 @@ const minimizeAttributeChanges = (left, right, currentAttributes, attributes) =>
/** /**
* @private * @private
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractItem|null} left * @param {AbstractItem|null} left
* @param {AbstractItem|null} right * @param {AbstractItem|null} right
* @param {Map<string,any>} currentAttributes * @param {Map<string,any>} currentAttributes
@ -177,7 +177,7 @@ const insertAttributes = (transaction, parent, left, right, currentAttributes, a
/** /**
* @private * @private
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractItem|null} left * @param {AbstractItem|null} left
* @param {AbstractItem|null} right * @param {AbstractItem|null} right
* @param {Map<string,any>} currentAttributes * @param {Map<string,any>} currentAttributes
@ -206,7 +206,7 @@ const insertText = (transaction, parent, left, right, currentAttributes, text, a
/** /**
* @private * @private
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractItem|null} left * @param {AbstractItem|null} left
* @param {AbstractItem|null} right * @param {AbstractItem|null} right
* @param {Map<string,any>} currentAttributes * @param {Map<string,any>} currentAttributes
@ -260,7 +260,7 @@ const formatText = (transaction, parent, left, right, currentAttributes, length,
/** /**
* @private * @private
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractItem|null} left * @param {AbstractItem|null} left
* @param {AbstractItem|null} right * @param {AbstractItem|null} right
* @param {Map<string,any>} currentAttributes * @param {Map<string,any>} currentAttributes
@ -324,9 +324,9 @@ const deleteText = (transaction, parent, left, right, currentAttributes, length)
* *
* @private * @private
*/ */
class YTextEvent extends YArrayEvent { class YTextEvent extends YEvent {
/** /**
* @param {AbstractType} ytext * @param {YText} ytext
* @param {Transaction} transaction * @param {Transaction} transaction
*/ */
constructor (ytext, transaction) { constructor (ytext, transaction) {
@ -544,6 +544,8 @@ class YTextEvent extends YArrayEvent {
* This type replaces y-richtext as this implementation is able to handle * This type replaces y-richtext as this implementation is able to handle
* block formats (format information on a paragraph), embeds (complex elements * block formats (format information on a paragraph), embeds (complex elements
* like pictures and videos), and text formats (**bold**, *italic*). * like pictures and videos), and text formats (**bold**, *italic*).
*
* @extends AbstractType<YTextEvent>
*/ */
export class YText extends AbstractType { export class YText extends AbstractType {
/** /**

View File

@ -11,7 +11,7 @@ import { YXmlEvent } from './YXmlEvent.js'
import { ItemType } from '../structs/ItemType.js' // eslint-disable-line import { ItemType } from '../structs/ItemType.js' // eslint-disable-line
import { YXmlText } from './YXmlText.js' // eslint-disable-line import { YXmlText } from './YXmlText.js' // eslint-disable-line
import { YXmlHook } from './YXmlHook.js' // eslint-disable-line import { YXmlHook } from './YXmlHook.js' // eslint-disable-line
import { AbstractType, typeArrayMap, typeArrayForEach, typeMapGet, typeMapGetAll } from './AbstractType.js' import { AbstractType, typeArrayMap, typeArrayForEach, typeMapGet, typeMapGetAll, typeArrayInsertGenerics } from './AbstractType.js'
import { Snapshot } from '../utils/Snapshot.js' // eslint-disable-line import { Snapshot } from '../utils/Snapshot.js' // eslint-disable-line
/** /**
@ -46,7 +46,7 @@ import { Snapshot } from '../utils/Snapshot.js' // eslint-disable-line
export class YXmlTreeWalker { export class YXmlTreeWalker {
/** /**
* @param {YXmlFragment | YXmlElement} root * @param {YXmlFragment | YXmlElement} root
* @param {function(AbstractType):boolean} f * @param {function(AbstractType<any>):boolean} f
*/ */
constructor (root, f) { constructor (root, f) {
this._filter = f || (() => true) this._filter = f || (() => true)
@ -107,6 +107,7 @@ export class YXmlTreeWalker {
* element - in this case the attributes and the nodeName are not shared. * element - in this case the attributes and the nodeName are not shared.
* *
* @public * @public
* @extends AbstractType<YXmlEvent>
*/ */
export class YXmlFragment extends AbstractType { export class YXmlFragment extends AbstractType {
/** /**
@ -119,7 +120,7 @@ export class YXmlFragment extends AbstractType {
* nop(node) * nop(node)
* } * }
* *
* @param {function(AbstractType):boolean} filter Function that is called on each child element and * @param {function(AbstractType<any>):boolean} filter Function that is called on each child element and
* returns a Boolean indicating whether the child * returns a Boolean indicating whether the child
* is to be included in the subtree. * is to be included in the subtree.
* @return {YXmlTreeWalker} A subtree and a position within it. * @return {YXmlTreeWalker} A subtree and a position within it.
@ -340,6 +341,28 @@ export class YXmlElement extends YXmlFragment {
getAttributes (snapshot) { getAttributes (snapshot) {
return typeMapGetAll(this) return typeMapGetAll(this)
} }
/**
* Inserts new content at an index.
*
* @example
* // Insert character 'a' at position 0
* xml.insert(0, [new Y.XmlText('text')])
*
* @param {number} index The index to insert content at
* @param {Array<YXmlElement|YXmlText>} content The array of content
*/
insert (index, content) {
if (this._y !== null) {
this._y.transact(transaction => {
typeArrayInsertGenerics(transaction, this, index, content)
})
} else {
// @ts-ignore _prelimContent is defined because this is not yet integrated
this._prelimContent.splice(index, 0, ...content)
}
}
// TODO: outsource the binding property. // TODO: outsource the binding property.
/** /**
* Creates a Dom Element that mirrors this YXmlElement. * Creates a Dom Element that mirrors this YXmlElement.

View File

@ -6,6 +6,7 @@ import { YEvent } from '../utils/YEvent.js'
import { AbstractType } from './AbstractType.js' // eslint-disable-line import { AbstractType } from './AbstractType.js' // eslint-disable-line
import { Transaction } from '../utils/Transaction.js' // eslint-disable-line import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
import { YXmlElement, YXmlFragment } from './YXmlElement.js' // eslint-disable-line
/** /**
* An Event that describes changes on a YXml Element or Yxml Fragment * An Event that describes changes on a YXml Element or Yxml Fragment
@ -14,14 +15,14 @@ import { Transaction } from '../utils/Transaction.js' // eslint-disable-line
*/ */
export class YXmlEvent extends YEvent { export class YXmlEvent extends YEvent {
/** /**
* @param {AbstractType} target The target on which the event is created. * @param {YXmlElement|YXmlFragment} target The target on which the event is created.
* @param {Set<string|null>} subs The set of changed attributes. `null` is included if the * @param {Set<string|null>} subs The set of changed attributes. `null` is included if the
* child list changed. * child list changed.
* @param {Transaction} transaction The transaction instance with wich the * @param {Transaction} transaction The transaction instance with wich the
* change was created. * change was created.
*/ */
constructor (target, subs, transaction) { constructor (target, subs, transaction) {
super(target) super(target, transaction)
/** /**
* The transaction instance for the computed change. * The transaction instance for the computed change.
* @type {Transaction} * @type {Transaction}

View File

@ -86,7 +86,7 @@ export const readID = decoder =>
* `type` does not store any information about the `keyname`. * `type` does not store any information about the `keyname`.
* This function finds the correct `keyname` for `type` and throws otherwise. * This function finds the correct `keyname` for `type` and throws otherwise.
* *
* @param {AbstractType} type * @param {AbstractType<any>} type
* @return {string} * @return {string}
*/ */
export const findRootTypeKey = type => { export const findRootTypeKey = type => {

View File

@ -61,13 +61,13 @@ export class Transaction {
* All types that were directly modified (property added or child * All types that were directly modified (property added or child
* inserted/deleted). New types are not included in this Set. * inserted/deleted). New types are not included in this Set.
* Maps from type to parentSubs (`item._parentSub = null` for YArray) * Maps from type to parentSubs (`item._parentSub = null` for YArray)
* @type {Map<AbstractType,Set<String|null>>} * @type {Map<AbstractType<YEvent>,Set<String|null>>}
*/ */
this.changed = new Map() this.changed = new Map()
/** /**
* Stores the events for the types that observe also child elements. * Stores the events for the types that observe also child elements.
* It is mainly used by `observeDeep`. * It is mainly used by `observeDeep`.
* @type {Map<AbstractType,Array<YEvent>>} * @type {Map<AbstractType<YEvent>,Array<YEvent>>}
*/ */
this.changedParentTypes = new Map() this.changedParentTypes = new Map()
/** /**

View File

@ -14,6 +14,7 @@ import { YArray } from '../types/YArray.js'
import { YText } from '../types/YText.js' import { YText } from '../types/YText.js'
import { YMap } from '../types/YMap.js' import { YMap } from '../types/YMap.js'
import { YXmlFragment } from '../types/YXmlElement.js' import { YXmlFragment } from '../types/YXmlElement.js'
import { YEvent } from './YEvent.js' // eslint-disable-line
/** /**
* A Yjs instance handles the state of shared data. * A Yjs instance handles the state of shared data.
@ -28,7 +29,7 @@ export class Y extends Observable {
this.gcEnabled = conf.gc || false this.gcEnabled = conf.gc || false
this.clientID = random.uint32() this.clientID = random.uint32()
/** /**
* @type {Map<string, AbstractType>} * @type {Map<string, AbstractType<YEvent>>}
*/ */
this.share = new Map() this.share = new Map()
this.store = new StructStore() this.store = new StructStore()
@ -203,7 +204,7 @@ export class Y extends Observable {
* *
* @param {string} name * @param {string} name
* @param {Function} TypeConstructor The constructor of the type definition * @param {Function} TypeConstructor The constructor of the type definition
* @return {AbstractType} The created type. Constructed with TypeConstructor * @return {AbstractType<any>} The created type. Constructed with TypeConstructor
*/ */
get (name, TypeConstructor = AbstractType) { get (name, TypeConstructor = AbstractType) {
// @ts-ignore // @ts-ignore
@ -242,7 +243,7 @@ export class Y extends Observable {
} }
/** /**
* @param {string} name * @param {string} name
* @return {YMap} * @return {YMap<any>}
*/ */
getMap (name) { getMap (name) {
// @ts-ignore // @ts-ignore

View File

@ -13,18 +13,18 @@ import { isDeleted } from './DeleteSet.js'
*/ */
export class YEvent { export class YEvent {
/** /**
* @param {AbstractType} target The changed type. * @param {AbstractType<any> target The changed type.
* @param {Transaction} transaction * @param {Transaction} transaction
*/ */
constructor (target, transaction) { constructor (target, transaction) {
/** /**
* The type on which this event was created on. * The type on which this event was created on.
* @type {AbstractType} * @type {AbstractType<any>
*/ */
this.target = target this.target = target
/** /**
* The current target on which the observe callback is called. * The current target on which the observe callback is called.
* @type {AbstractType} * @type {AbstractType<any>
*/ */
this.currentTarget = target this.currentTarget = target
/** /**
@ -81,7 +81,7 @@ export class YEvent {
* console.log(path) // might look like => [2, 'key1'] * console.log(path) // might look like => [2, 'key1']
* child === type.get(path[0]).get(path[1]) * child === type.get(path[0]).get(path[1])
* *
* @param {AbstractType} parent * @param {AbstractType<any> parent
* @param {AbstractItem} child target * @param {AbstractItem} child target
* @return {Array<string|number>} Path to the target * @return {Array<string|number>} Path to the target
*/ */

View File

@ -8,8 +8,8 @@ import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
/** /**
* Check if `parent` is a parent of `child`. * Check if `parent` is a parent of `child`.
* *
* @param {AbstractType} parent * @param {AbstractType<any>} parent
* @param {AbstractType} child * @param {AbstractType<any>} child
* @return {Boolean} Whether `parent` is a parent of `child`. * @return {Boolean} Whether `parent` is a parent of `child`.
* *
* @public * @public

View File

@ -60,12 +60,12 @@ export class RelativePosition {
export class AbsolutePosition { export class AbsolutePosition {
/** /**
* @param {AbstractType} type * @param {AbstractType<any>} type
* @param {number} offset * @param {number} offset
*/ */
constructor (type, offset) { constructor (type, offset) {
/** /**
* @type {AbstractType} * @type {AbstractType<any>}
*/ */
this.type = type this.type = type
/** /**
@ -76,13 +76,13 @@ export class AbsolutePosition {
} }
/** /**
* @param {AbstractType} type * @param {AbstractType<any> type
* @param {number} offset * @param {number} offset
*/ */
export const createAbsolutePosition = (type, offset) => new AbsolutePosition(type, offset) export const createAbsolutePosition = (type, offset) => new AbsolutePosition(type, offset)
/** /**
* @param {AbstractType} type * @param {AbstractType<any> type
* @param {ID.ID|null} item * @param {ID.ID|null} item
*/ */
export const createRelativePosition = (type, item) => { export const createRelativePosition = (type, item) => {
@ -99,7 +99,7 @@ export const createRelativePosition = (type, item) => {
/** /**
* Create a relativePosition based on a absolute position. * Create a relativePosition based on a absolute position.
* *
* @param {AbstractType} type The base type (e.g. YText or YArray). * @param {AbstractType<any> type The base type (e.g. YText or YArray).
* @param {number} offset The absolute position. * @param {number} offset The absolute position.
* @return {RelativePosition} * @return {RelativePosition}
*/ */

View File

@ -224,7 +224,7 @@ export class TestConnector {
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
* @param {{users?:number}} conf * @param {{users?:number}} conf
* @return {{testConnector:TestConnector,users:Array<TestYInstance>,array0:Y.Array<any>,array1:Y.Array<any>,array2:Y.Array<any>,map0:Y.Map,map1:Y.Map,map2:Y.Map,text0:Y.Text,text1:Y.Text,text2:Y.Text,xml0:Y.XmlFragment,xml1:Y.XmlFragment,xml2:Y.XmlFragment}} * @return {{testConnector:TestConnector,users:Array<TestYInstance>,array0:Y.Array<any>,array1:Y.Array<any>,array2:Y.Array<any>,map0:Y.Map<any>,map1:Y.Map<any>,map2:Y.Map<any>,map3:Y.Map<any>,text0:Y.Text,text1:Y.Text,text2:Y.Text,xml0:Y.XmlElement,xml1:Y.XmlElement,xml2:Y.XmlElement}}
*/ */
export const init = (tc, { users = 5 } = {}) => { export const init = (tc, { users = 5 } = {}) => {
/** /**

View File

@ -1,4 +1,4 @@
import { init, compare, applyRandomTests } from './testHelper.js' import { init, compare, applyRandomTests, TestYInstance } from './testHelper.js' // eslint-disable-line
import * as Y from '../src/index.js' import * as Y from '../src/index.js'
import * as t from 'lib0/testing.js' import * as t from 'lib0/testing.js'
import * as prng from 'lib0/prng.js' import * as prng from 'lib0/prng.js'
@ -54,7 +54,7 @@ export const testGetAndSetOfMapProperty = tc => {
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
const u = user.define('map', Y.Map) const u = user.getMap('map')
t.compare(u.get('stuff'), 'stuffy') t.compare(u.get('stuff'), 'stuffy')
t.assert(u.get('undefined') === undefined, 'undefined') t.assert(u.get('undefined') === undefined, 'undefined')
t.compare(u.get('null'), null, 'null') t.compare(u.get('null'), null, 'null')
@ -82,6 +82,7 @@ export const testYmapSetsYarray = tc => {
const array = map0.set('Array', new Y.Array()) const array = map0.set('Array', new Y.Array())
t.assert(array === map0.get('Array')) t.assert(array === map0.get('Array'))
array.insert(0, [1, 2, 3]) array.insert(0, [1, 2, 3])
// @ts-ignore
t.compare(map0.toJSON(), { Array: [1, 2, 3] }) t.compare(map0.toJSON(), { Array: [1, 2, 3] })
compare(users) compare(users)
} }
@ -95,7 +96,7 @@ export const testGetAndSetOfMapPropertySyncs = tc => {
t.compare(map0.get('stuff'), 'stuffy') t.compare(map0.get('stuff'), 'stuffy')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.getMap('map')
t.compare(u.get('stuff'), 'stuffy') t.compare(u.get('stuff'), 'stuffy')
} }
compare(users) compare(users)
@ -110,7 +111,7 @@ export const testGetAndSetOfMapPropertyWithConflict = tc => {
map1.set('stuff', 'c1') map1.set('stuff', 'c1')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.getMap('map')
t.compare(u.get('stuff'), 'c0') t.compare(u.get('stuff'), 'c0')
} }
compare(users) compare(users)
@ -126,7 +127,7 @@ export const testGetAndSetAndDeleteOfMapProperty = tc => {
map1.set('stuff', 'c1') map1.set('stuff', 'c1')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.getMap('map')
t.assert(u.get('stuff') === undefined) t.assert(u.get('stuff') === undefined)
} }
compare(users) compare(users)
@ -143,7 +144,7 @@ export const testGetAndSetOfMapPropertyWithThreeConflicts = tc => {
map2.set('stuff', 'c3') map2.set('stuff', 'c3')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.getMap('map')
t.compare(u.get('stuff'), 'c0') t.compare(u.get('stuff'), 'c0')
} }
compare(users) compare(users)
@ -166,7 +167,7 @@ export const testGetAndSetAndDeleteOfMapPropertyWithThreeConflicts = tc => {
map3.set('stuff', 'c3') map3.set('stuff', 'c3')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.getMap('map')
t.assert(u.get('stuff') === undefined) t.assert(u.get('stuff') === undefined)
} }
compare(users) compare(users)
@ -183,9 +184,11 @@ export const testObserveDeepProperties = tc => {
map1.observeDeep(events => { map1.observeDeep(events => {
events.forEach(event => { events.forEach(event => {
calls++ calls++
// @ts-ignore
t.assert(event.keysChanged.has('deepmap')) t.assert(event.keysChanged.has('deepmap'))
t.assert(event.path.length === 1) t.assert(event.path.length === 1)
t.assert(event.path[0] === 'map') t.assert(event.path[0] === 'map')
// @ts-ignore
dmapid = event.target.get('deepmap')._id dmapid = event.target.get('deepmap')._id
}) })
}) })
@ -211,6 +214,9 @@ export const testObserveDeepProperties = tc => {
*/ */
export const testObserversUsingObservedeep = tc => { export const testObserversUsingObservedeep = tc => {
const { users, map0 } = init(tc, { users: 2 }) const { users, map0 } = init(tc, { users: 2 })
/**
* @type {Array<Array<string>>}
*/
const pathes = [] const pathes = []
let calls = 0 let calls = 0
map0.observeDeep(events => { map0.observeDeep(events => {
@ -228,7 +234,11 @@ export const testObserversUsingObservedeep = tc => {
} }
// TODO: Test events in Y.Map // TODO: Test events in Y.Map
const compareEvent = (t, is, should) => { /**
* @param {Object<string,any>} is
* @param {Object<string,any>} should
*/
const compareEvent = (is, should) => {
for (var key in should) { for (var key in should) {
t.compare(should[key], is[key]) t.compare(should[key], is[key])
} }
@ -239,18 +249,21 @@ const compareEvent = (t, is, should) => {
*/ */
export const testThrowsAddAndUpdateAndDeleteEvents = tc => { export const testThrowsAddAndUpdateAndDeleteEvents = tc => {
const { users, map0 } = init(tc, { users: 2 }) const { users, map0 } = init(tc, { users: 2 })
let event /**
* @type {Object<string,any>}
*/
let event = {}
map0.observe(e => { map0.observe(e => {
event = e // just put it on event, should be thrown synchronously anyway event = e // just put it on event, should be thrown synchronously anyway
}) })
map0.set('stuff', 4) map0.set('stuff', 4)
compareEvent(t, event, { compareEvent(event, {
target: map0, target: map0,
keysChanged: new Set(['stuff']) keysChanged: new Set(['stuff'])
}) })
// update, oldValue is in contents // update, oldValue is in contents
map0.set('stuff', new Y.Array()) map0.set('stuff', new Y.Array())
compareEvent(t, event, { compareEvent(event, {
target: map0, target: map0,
keysChanged: new Set(['stuff']) keysChanged: new Set(['stuff'])
}) })
@ -258,7 +271,7 @@ export const testThrowsAddAndUpdateAndDeleteEvents = tc => {
map0.set('stuff', 5) map0.set('stuff', 5)
// delete // delete
map0.delete('stuff') map0.delete('stuff')
compareEvent(t, event, { compareEvent(event, {
keysChanged: new Set(['stuff']), keysChanged: new Set(['stuff']),
target: map0 target: map0
}) })
@ -270,7 +283,10 @@ export const testThrowsAddAndUpdateAndDeleteEvents = tc => {
*/ */
export const testYmapEventHasCorrectValueWhenSettingAPrimitive = tc => { export const testYmapEventHasCorrectValueWhenSettingAPrimitive = tc => {
const { users, map0 } = init(tc, { users: 3 }) const { users, map0 } = init(tc, { users: 3 })
let event /**
* @type {Object<string,any>}
*/
let event = {}
map0.observe(e => { map0.observe(e => {
event = e event = e
}) })
@ -284,7 +300,10 @@ export const testYmapEventHasCorrectValueWhenSettingAPrimitive = tc => {
*/ */
export const testYmapEventHasCorrectValueWhenSettingAPrimitiveFromOtherUser = tc => { export const testYmapEventHasCorrectValueWhenSettingAPrimitiveFromOtherUser = tc => {
const { users, map0, map1, testConnector } = init(tc, { users: 3 }) const { users, map0, map1, testConnector } = init(tc, { users: 3 })
let event /**
* @type {Object<string,any>}
*/
let event = {}
map0.observe(e => { map0.observe(e => {
event = e event = e
}) })
@ -294,25 +313,28 @@ export const testYmapEventHasCorrectValueWhenSettingAPrimitiveFromOtherUser = tc
compare(users) compare(users)
} }
/**
* @type {Array<function(TestYInstance,prng.PRNG):void>}
*/
const mapTransactions = [ const mapTransactions = [
function set (tc, user, gen) { function set (user, gen) {
let key = prng.oneOf(gen, ['one', 'two']) let key = prng.oneOf(gen, ['one', 'two'])
var value = prng.utf16String(gen) var value = prng.utf16String(gen)
user.define('map', Y.Map).set(key, value) user.getMap('map').set(key, value)
}, },
function setType (tc, user, gen) { function setType (user, gen) {
let key = prng.oneOf(gen, ['one', 'two']) let key = prng.oneOf(gen, ['one', 'two'])
var type = prng.oneOf(gen, [new Y.Array(), new Y.Map()]) var type = prng.oneOf(gen, [new Y.Array(), new Y.Map()])
user.define('map', Y.Map).set(key, type) user.getMap('map').set(key, type)
if (type instanceof Y.Array) { if (type instanceof Y.Array) {
type.insert(0, [1, 2, 3, 4]) type.insert(0, [1, 2, 3, 4])
} else { } else {
type.set('deepkey', 'deepvalue') type.set('deepkey', 'deepvalue')
} }
}, },
function _delete (tc, user, gen) { function _delete (user, gen) {
let key = prng.oneOf(gen, ['one', 'two']) let key = prng.oneOf(gen, ['one', 'two'])
user.define('map', Y.Map).delete(key) user.getMap('map').delete(key)
} }
] ]

View File

@ -54,7 +54,8 @@ export const testBasicFormat = tc => {
t.assert(text0.toString() === 'zb') t.assert(text0.toString() === 'zb')
t.compare(text0.toDelta(), [{ insert: 'zb', attributes: { bold: true } }]) t.compare(text0.toDelta(), [{ insert: 'zb', attributes: { bold: true } }])
t.compare(delta, [{ insert: 'z', attributes: { bold: true } }]) t.compare(delta, [{ insert: 'z', attributes: { bold: true } }])
t.assert(text0._start._right._right._right._content === 'b', 'Does not insert duplicate attribute marker') // @ts-ignore
t.assert(text0._start.right.right.right.string === 'b', 'Does not insert duplicate attribute marker')
text0.insert(0, 'y') text0.insert(0, 'y')
t.assert(text0.toString() === 'yzb') t.assert(text0.toString() === 'yzb')
t.compare(text0.toDelta(), [{ insert: 'y' }, { insert: 'zb', attributes: { bold: true } }]) t.compare(text0.toDelta(), [{ insert: 'y' }, { insert: 'zb', attributes: { bold: true } }])