From a59b8891e8fbfdfd0a6f12fe2112709414793a2a Mon Sep 17 00:00:00 2001 From: brightman9 <31172605+brightman9@users.noreply.github.com> Date: Sat, 30 Nov 2019 19:51:56 +0800 Subject: [PATCH] getFlatGraph: rewrite recursion to while loop To avoid call stack overflow when the graph has a large nesting depth --- js/anchor.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/js/anchor.js b/js/anchor.js index 0959d34..b96f219 100644 --- a/js/anchor.js +++ b/js/anchor.js @@ -139,9 +139,17 @@ Anchor.prototype.updateFlatGraph = function() { }; // return Array of self & all child graph items -Anchor.prototype.getFlatGraph = function() { - var flatGraph = [ this ]; - return this.addChildFlatGraph( flatGraph ); +Anchor.prototype.getFlatGraph = function () { + var flatGraph = [this]; + var index = 0; + while (index < flatGraph.length) { + var currentGraph = flatGraph[index]; + currentGraph.children.forEach(function (child) { + flatGraph.push(child); + }); + index++; + } + return flatGraph; }; Anchor.prototype.addChildFlatGraph = function( flatGraph ) {