Skip to content

Enhancement: Change getChildren from recursive to iterative #277

@teeboteebo

Description

@teeboteebo

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch gantt-task-react@0.3.9 for the project I'm working on.

When working with larger datasets with large projects containing large sets of tasks with many dependencies, Maximum call stack is reached. In order to avoid this I've created a patch for the package to change getChildren from a deep recursion to an iterative solution.

Here is the diff that solved my problem:

diff --git a/node_modules/gantt-task-react/dist/index.js b/node_modules/gantt-task-react/dist/index.js
index 626bb39..2c0b64b 100644
--- a/node_modules/gantt-task-react/dist/index.js
+++ b/node_modules/gantt-task-react/dist/index.js
@@ -1478,24 +1478,47 @@ function removeHiddenTasks(tasks) {
 }
 
 function getChildren(taskList, task) {
-  var tasks = [];
+  var result = [];
+  var stack = [];
 
+  // Initial children
   if (task.type !== "project") {
-    tasks = taskList.filter(function (t) {
+    stack = taskList.filter(function (t) {
       return t.dependencies && t.dependencies.indexOf(task.id) !== -1;
     });
   } else {
-    tasks = taskList.filter(function (t) {
+    stack = taskList.filter(function (t) {
       return t.project && t.project === task.id;
     });
   }
 
-  var taskChildren = [];
-  tasks.forEach(function (t) {
-    taskChildren.push.apply(taskChildren, getChildren(taskList, t));
-  });
-  tasks = tasks.concat(tasks, taskChildren);
-  return tasks;
+  var seen = new Set();
+
+  while (stack.length > 0) {
+    var current = stack.pop();
+    if (seen.has(current.id)) continue;
+    seen.add(current.id);
+    result.push(current);
+
+    var children = [];
+    if (current.type !== "project") {
+      children = taskList.filter(function (t) {
+        return t.dependencies && t.dependencies.indexOf(current.id) !== -1;
+      });
+    } else {
+      children = taskList.filter(function (t) {
+        return t.project && t.project === current.id;
+      });
+    }
+
+    children.forEach(function (child) {
+      if (!seen.has(child.id)) {
+        stack.push(child);
+      }
+    });
+  }
+
+  return result;
 }
 
 var sortTasks = function sortTasks(taskA, taskB) {
diff --git a/node_modules/gantt-task-react/dist/index.modern.js b/node_modules/gantt-task-react/dist/index.modern.js
index b372269..8266dd5 100644
--- a/node_modules/gantt-task-react/dist/index.modern.js
+++ b/node_modules/gantt-task-react/dist/index.modern.js
@@ -1477,24 +1477,47 @@ function removeHiddenTasks(tasks) {
 }
 
 function getChildren(taskList, task) {
-  var tasks = [];
+  var result = [];
+  var stack = [];
 
+  // Initial children
   if (task.type !== "project") {
-    tasks = taskList.filter(function (t) {
+    stack = taskList.filter(function (t) {
       return t.dependencies && t.dependencies.indexOf(task.id) !== -1;
     });
   } else {
-    tasks = taskList.filter(function (t) {
+    stack = taskList.filter(function (t) {
       return t.project && t.project === task.id;
     });
   }
 
-  var taskChildren = [];
-  tasks.forEach(function (t) {
-    taskChildren.push.apply(taskChildren, getChildren(taskList, t));
-  });
-  tasks = tasks.concat(tasks, taskChildren);
-  return tasks;
+  var seen = new Set();
+
+  while (stack.length > 0) {
+    var current = stack.pop();
+    if (seen.has(current.id)) continue;
+    seen.add(current.id);
+    result.push(current);
+
+    var children = [];
+    if (current.type !== "project") {
+      children = taskList.filter(function (t) {
+        return t.dependencies && t.dependencies.indexOf(current.id) !== -1;
+      });
+    } else {
+      children = taskList.filter(function (t) {
+        return t.project && t.project === current.id;
+      });
+    }
+
+    children.forEach(function (child) {
+      if (!seen.has(child.id)) {
+        stack.push(child);
+      }
+    });
+  }
+
+  return result;
 }
 
 var sortTasks = function sortTasks(taskA, taskB) {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions