Home Reference Source

cables_dev/cables/src/libs/cgl/branchprofiler/index.js

class Branch
{
    constructor(name, txt)
    {
        this.name = name;
        this.txt = txt;
        this.dur = 0;
        this._startTime = 0;
        this.childs = [];
    }

    start()
    {
        this._startTime = performance.now();
    }

    end()
    {
        this.dur = performance.now() - this._startTime;
    }

    push(name, txt)
    {
        const b = new Branch(name, txt);
        this.childs.push(b);
        b.start();
        return b;
    }

    print(level)
    {
        level = level || 0;

        let str = "";
        for (let i = 0; i < level; i++) str += "  ";

        for (let i = 0; i < this.childs.length; i++)
            this.childs[i].print(level + 1);
    }
}

// //////////////////////////////////////////

class BranchStack
{
    constructor()
    {
    }

    start()
    {
        this.root = new Branch("Root");
        this.root.start();

        this.current = this.root;
    }

    push(name, txt)
    {
        if (!this.current) this.start();

        const prev = this.current;
        this.current = this.current.push(name, txt);
        this.current.prev = prev;
        this.current.start();
        return this.current;
    }

    pop()
    {
        if (!this.current) return;
        this.current.end();
        this.current = this.current.prev;
    }

    finish()
    {
        this.current.end();
        this.root.print();
        this.current = null;
    }
}

// export { BranchStack };
// export { Branch };

CABLES.BranchStack = BranchStack;
CABLES.Branch = Branch;