All files / src/render/utils flat-tree.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2538x 38x   38x 37x   37x   38x 371x 371x     38x 331x 331x     38x 182x 182x 182x   38x  
import { addUniqueItem, removeItem } from "../../utils/array"
import { compareByDepth, WithDepth } from "./compare-by-depth"
 
export class FlatTree {
    private children: WithDepth[] = []
 
    private isDirty: boolean = false
 
    add(child: WithDepth) {
        addUniqueItem(this.children, child)
        this.isDirty = true
    }
 
    remove(child: WithDepth) {
        removeItem(this.children, child)
        this.isDirty = true
    }
 
    forEach(callback: (child: WithDepth) => void) {
        this.isDirty && this.children.sort(compareByDepth)
        this.isDirty = false
        this.children.forEach(callback)
    }
}