diff --git a/lib/feed-v1/tangle.js b/lib/feed-v1/tangle.js index d59ef87..8cd88d0 100644 --- a/lib/feed-v1/tangle.js +++ b/lib/feed-v1/tangle.js @@ -85,13 +85,16 @@ class Tangle { } add(msgHash, msg) { - const tangles = msg.metadata.tangles - if (msgHash === this.#rootHash) { + if (msgHash === this.#rootHash && !this.#rootMsg) { this.#tips.add(msgHash) this.#perDepth.set(0, [msgHash]) this.#depth.set(msgHash, 0) this.#rootMsg = msg - } else if (tangles[this.#rootHash]) { + return + } + + const tangles = msg.metadata.tangles + if (msgHash !== this.#rootHash && tangles[this.#rootHash]) { this.#tips.add(msgHash) const prev = tangles[this.#rootHash].prev for (const p of prev) { @@ -105,6 +108,7 @@ class Tangle { atDepth.push(msgHash) atDepth.sort(compareMsgHashes) this.#perDepth.set(depth, atDepth) + return } } @@ -120,6 +124,10 @@ class Tangle { * @returns {Array} */ topoSort() { + if (!this.#rootMsg) { + console.warn('Tangle is missing root message') + return [] + } const sorted = [] const max = this.#maxDepth for (let i = 0; i <= max; i++) { @@ -136,6 +144,10 @@ class Tangle { * @returns {Set} */ getTips() { + if (!this.#rootMsg) { + console.warn('Tangle is missing root message') + return new Set() + } return this.#tips } @@ -144,6 +156,10 @@ class Tangle { * @returns {Set} */ getLipmaaSet(depth) { + if (!this.#rootMsg) { + console.warn('Tangle is missing root message') + return new Set() + } const lipmaaDepth = lipmaa(depth + 1) - 1 return new Set(this.#getAllAtDepth(lipmaaDepth)) } @@ -165,6 +181,10 @@ class Tangle { } isFeed() { + if (!this.#rootMsg) { + console.warn('Tangle is missing root message') + return false + } if (this.#rootMsg.content) return false const metadata = this.#rootMsg.metadata return metadata.size === 0 && metadata.hash === null @@ -177,6 +197,10 @@ class Tangle { } shortestPathToRoot(msgHash) { + if (!this.#rootMsg) { + console.warn('Tangle is missing root message') + return [] + } const path = [] let current = msgHash while (true) {