prelim refactor commit

This commit is contained in:
Kevin Jahns
2019-03-26 01:14:15 +01:00
parent 293527e62b
commit d9ab593b07
44 changed files with 2263 additions and 1914 deletions

View File

@@ -1,3 +1,6 @@
import { AbstractItem } from '../structs/AbstractItem.js' // eslint-disable-line
import { AbstractType } from '../types/AbstractType.js' // eslint-disable-line
/**
* @module utils
*/
@@ -7,17 +10,17 @@
*/
export class YEvent {
/**
* @param {Type} target The changed type.
* @param {AbstractType} target The changed type.
*/
constructor (target) {
/**
* The type on which this event was created on.
* @type {Type}
* @type {AbstractType}
*/
this.target = target
/**
* The current target on which the observe callback is called.
* @type {Type}
* @type {AbstractType}
*/
this.currentTarget = target
}
@@ -34,6 +37,48 @@ export class YEvent {
* type === event.target // => true
*/
get path () {
return this.currentTarget.getPathTo(this.target)
// @ts-ignore _item is defined because target is integrated
return getPathTo(this.currentTarget, this.target._item)
}
}
/**
* Compute the path from this type to the specified target.
*
* @example
* // `child` should be accessible via `type.get(path[0]).get(path[1])..`
* const path = type.getPathTo(child)
* // assuming `type instanceof YArray`
* console.log(path) // might look like => [2, 'key1']
* child === type.get(path[0]).get(path[1])
*
* @param {AbstractType} parent
* @param {AbstractItem} child target
* @return {Array<string|number>} Path to the target
*/
const getPathTo = (parent, child) => {
const path = []
while (true) {
const cparent = child.parent
if (child.parentSub !== null) {
// parent is map-ish
path.unshift(child.parentSub)
} else {
// parent is array-ish
let i = 0
let c = cparent._start
while (c !== child && c !== null) {
if (!c.deleted) {
i++
}
c = c.right
}
path.unshift(i)
}
if (parent === cparent) {
return path
}
// @ts-ignore parent._item cannot be null, because it is integrated
child = parent._item
}
}