/** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ import { State } from '../state'; import { StateTransform } from '../transform'; import { StateObject, StateObjectCell } from '../object'; export { StateTreeSpine } /** The tree spine allows access to ancestor of a node during reconciliation. */ interface StateTreeSpine { getAncestorOfType(type: T): StateObject.From | undefined; getRootOfType(type: T): StateObject.From | undefined; } namespace StateTreeSpine { export class Impl implements StateTreeSpine { private current: StateObjectCell | undefined = void 0; setSurrent(cell?: StateObjectCell) { this.current = cell; } getAncestorOfType(t: T): StateObject.From | undefined { if (!this.current) return void 0; let cell = this.current; while (true) { cell = this.cells.get(cell.transform.parent)!; if (!cell.obj) return void 0; if (cell.obj.type === t.type) return cell.obj as StateObject.From; if (cell.transform.ref === StateTransform.RootRef) return void 0; } } getRootOfType(t: T): StateObject.From | undefined { if (!this.current) return void 0; let cell = this.current; let ret: StateObjectCell | undefined = void 0; while (true) { cell = this.cells.get(cell.transform.parent)!; if (!cell.obj) return void 0; if (cell.obj.type === t.type) { ret = cell; } if (cell.transform.ref === StateTransform.RootRef) return ret ? ret.obj as StateObject.From : void 0; } } constructor(private cells: State.Cells) { } } }