diff --git a/time-tree.js b/time-tree.js index 43da1e7..a275417 100644 --- a/time-tree.js +++ b/time-tree.js @@ -1,106 +1,115 @@ 'use strict' + const archy = require('archy') -function TimeTree () { - this.root = null - this.tableId = new Map() - this.tableLabel = new Map() -} +const kUntrackNode = Symbol('avvio.TimeTree.untrackNode') +const kTrackNode = Symbol('avvio.TimeTree.trackNode') +const kGetParent = Symbol('avvio.TimeTree.getParent') +const kGetNode = Symbol('avvio.TimeTree.getNode') +const kAddNode = Symbol('avvio.TimeTree.addNode') -TimeTree.prototype.trackNode = function (node) { - this.tableId.set(node.id, node) - if (this.tableLabel.has(node.label)) { - this.tableLabel.get(node.label).push(node) - } else { - this.tableLabel.set(node.label, [node]) +class TimeTree { + constructor () { + this.root = null + this.tableId = new Map() + this.tableLabel = new Map() } -} -TimeTree.prototype.untrackNode = function (node) { - this.tableId.delete(node.id) - - const labelNode = this.tableLabel.get(node.label) - if (labelNode.id) { - this.tableLabel.delete(node.label) - return + [kTrackNode] (node) { + this.tableId.set(node.id, node) + if (this.tableLabel.has(node.label)) { + this.tableLabel.get(node.label).push(node) + } else { + this.tableLabel.set(node.label, [node]) + } } - labelNode.pop() - if (labelNode.length === 0) { - this.tableLabel.delete(node.label) + [kUntrackNode] (node) { + this.tableId.delete(node.id) + + const labelNode = this.tableLabel.get(node.label) + if (labelNode.id) { + this.tableLabel.delete(node.label) + return + } + labelNode.pop() + + if (labelNode.length === 0) { + this.tableLabel.delete(node.label) + } } -} -TimeTree.prototype.getParent = function (parent) { - if (parent === null) { - return this.root + [kGetParent] (parent) { + if (parent === null) { + return this.root + } + + const parentNode = this.tableLabel.get(parent) + if (parentNode.id) { + return parentNode + } + return parentNode[parentNode.length - 1] } - const parentNode = this.tableLabel.get(parent) - if (parentNode.id) { - return parentNode + [kGetNode] (nodeId) { + return this.tableId.get(nodeId) } - return parentNode[parentNode.length - 1] -} -TimeTree.prototype.getNode = function (nodeId) { - return this.tableId.get(nodeId) -} + [kAddNode] (parent, childName, start) { + const isRoot = parent === null + if (isRoot) { + this.root = { + id: 'root', + label: childName, + start, + nodes: [] + } + this[kTrackNode](this.root) + return this.root.id + } -TimeTree.prototype.add = function (parent, child, start) { - const isRoot = parent === null - if (isRoot) { - this.root = { - id: 'root', - label: child, + const parentNode = this[kGetParent](parent) + const nodeId = `${childName}-${Math.random()}` + const childNode = { + id: nodeId, + parent, start, + label: childName, nodes: [] } - this.trackNode(this.root) - return this.root.id + parentNode.nodes.push(childNode) + this[kTrackNode](childNode) + return nodeId } - const parentNode = this.getParent(parent) - const nodeId = `${child}-${Math.random()}` - const childNode = { - id: nodeId, - parent, - start, - label: child, - nodes: [] + start (parent, childName, start = Date.now()) { + return this[kAddNode](parent, childName, start) } - parentNode.nodes.push(childNode) - this.trackNode(childNode) - return nodeId -} -TimeTree.prototype.start = function (parent, child, start) { - return this.add(parent, child, start || Date.now()) -} - -TimeTree.prototype.stop = function (nodeId, stop) { - const node = this.getNode(nodeId) - if (node) { - node.stop = stop || Date.now() - node.diff = (node.stop - node.start) || 0 - this.untrackNode(node) + stop (nodeId, stop = Date.now()) { + const node = this[kGetNode](nodeId) + if (node) { + node.stop = stop + node.diff = (node.stop - node.start) || 0 + this[kUntrackNode](node) + } } -} -TimeTree.prototype.toJSON = function () { - return Object.assign({}, this.root) -} + toJSON () { + return Object.assign({}, this.root) + } -TimeTree.prototype.prittyPrint = function () { - const decorateText = (node) => { - node.label = `${node.label} ${node.diff} ms` - if (node.nodes.length > 0) { - node.nodes = node.nodes.map(_ => decorateText(_)) + prittyPrint () { + const decorateText = (node) => { + node.label = `${node.label} ${node.diff} ms` + if (node.nodes.length > 0) { + node.nodes = node.nodes.map(_ => decorateText(_)) + } + return node } - return node + const out = decorateText(this.toJSON()) + return archy(out) } - const out = decorateText(this.toJSON()) - return archy(out) } module.exports = TimeTree