better typings for ydoc.get
This commit is contained in:
parent
16d9638bc8
commit
e1bce03ed8
@ -20,7 +20,7 @@ import {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* An YXmlElement imitates the behavior of a
|
* An YXmlElement imitates the behavior of a
|
||||||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Element|Dom Element}.
|
* https://developer.mozilla.org/en-US/docs/Web/API/Element|Dom Element
|
||||||
*
|
*
|
||||||
* * An YXmlElement has attributes (key value pairs)
|
* * An YXmlElement has attributes (key value pairs)
|
||||||
* * An YXmlElement has childElements that must inherit from YXmlElement
|
* * An YXmlElement has childElements that must inherit from YXmlElement
|
||||||
|
@ -181,6 +181,7 @@ export class Doc extends Observable {
|
|||||||
* Define all types right after the Yjs instance is created and store them in a separate object.
|
* Define all types right after the Yjs instance is created and store them in a separate object.
|
||||||
* Also use the typed methods `getText(name)`, `getArray(name)`, ..
|
* Also use the typed methods `getText(name)`, `getArray(name)`, ..
|
||||||
*
|
*
|
||||||
|
* @template {typeof AbstractType<any>} Type
|
||||||
* @example
|
* @example
|
||||||
* const y = new Y(..)
|
* const y = new Y(..)
|
||||||
* const appState = {
|
* const appState = {
|
||||||
@ -189,12 +190,12 @@ export class Doc extends Observable {
|
|||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @param {Function} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...
|
* @param {Type} TypeConstructor The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...
|
||||||
* @return {AbstractType<any>} The created type. Constructed with TypeConstructor
|
* @return {InstanceType<Type>} The created type. Constructed with TypeConstructor
|
||||||
*
|
*
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
get (name, TypeConstructor = AbstractType) {
|
get (name, TypeConstructor = /** @type {any} */ (AbstractType)) {
|
||||||
const type = map.setIfUndefined(this.share, name, () => {
|
const type = map.setIfUndefined(this.share, name, () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const t = new TypeConstructor()
|
const t = new TypeConstructor()
|
||||||
@ -220,12 +221,12 @@ export class Doc extends Observable {
|
|||||||
t._length = type._length
|
t._length = type._length
|
||||||
this.share.set(name, t)
|
this.share.set(name, t)
|
||||||
t._integrate(this, null)
|
t._integrate(this, null)
|
||||||
return t
|
return /** @type {InstanceType<Type>} */ (t)
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Type with the name ${name} has already been defined with a different constructor`)
|
throw new Error(`Type with the name ${name} has already been defined with a different constructor`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return type
|
return /** @type {InstanceType<Type>} */ (type)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -236,8 +237,7 @@ export class Doc extends Observable {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getArray (name = '') {
|
getArray (name = '') {
|
||||||
// @ts-ignore
|
return /** @type {YArray<T>} */ (this.get(name, YArray))
|
||||||
return this.get(name, YArray)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -247,7 +247,6 @@ export class Doc extends Observable {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getText (name = '') {
|
getText (name = '') {
|
||||||
// @ts-ignore
|
|
||||||
return this.get(name, YText)
|
return this.get(name, YText)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,8 +258,7 @@ export class Doc extends Observable {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getMap (name = '') {
|
getMap (name = '') {
|
||||||
// @ts-ignore
|
return /** @type {YMap<T>} */ (this.get(name, YMap))
|
||||||
return this.get(name, YMap)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -270,8 +268,7 @@ export class Doc extends Observable {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getXmlElement (name = '') {
|
getXmlElement (name = '') {
|
||||||
// @ts-ignore
|
return /** @type {YXmlElement<{[key:string]:string}>} */ (this.get(name, YXmlElement))
|
||||||
return this.get(name, YXmlElement)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -281,7 +278,6 @@ export class Doc extends Observable {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getXmlFragment (name = '') {
|
getXmlFragment (name = '') {
|
||||||
// @ts-ignore
|
|
||||||
return this.get(name, YXmlFragment)
|
return this.get(name, YXmlFragment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +189,6 @@ export const testClone = _tc => {
|
|||||||
const third = new Y.XmlElement('p')
|
const third = new Y.XmlElement('p')
|
||||||
yxml.push([first, second, third])
|
yxml.push([first, second, third])
|
||||||
t.compareArrays(yxml.toArray(), [first, second, third])
|
t.compareArrays(yxml.toArray(), [first, second, third])
|
||||||
|
|
||||||
const cloneYxml = yxml.clone()
|
const cloneYxml = yxml.clone()
|
||||||
ydoc.getArray('copyarr').insert(0, [cloneYxml])
|
ydoc.getArray('copyarr').insert(0, [cloneYxml])
|
||||||
t.assert(cloneYxml.length === 3)
|
t.assert(cloneYxml.length === 3)
|
||||||
@ -217,7 +216,6 @@ export const testFormattingBug = _tc => {
|
|||||||
export const testElement = _tc => {
|
export const testElement = _tc => {
|
||||||
const ydoc = new Y.Doc()
|
const ydoc = new Y.Doc()
|
||||||
const yxmlel = ydoc.getXmlElement()
|
const yxmlel = ydoc.getXmlElement()
|
||||||
|
|
||||||
const text1 = new Y.XmlText('text1')
|
const text1 = new Y.XmlText('text1')
|
||||||
const text2 = new Y.XmlText('text2')
|
const text2 = new Y.XmlText('text2')
|
||||||
yxmlel.insert(0, [text1, text2])
|
yxmlel.insert(0, [text1, text2])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user