From 7374445b3446412b0e72cfbb3aaabf25518b881b Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Thu, 14 Nov 2024 21:07:53 +0100 Subject: [PATCH 1/3] Register timeout before starting the algorithm This allows the layout to be stopped in a more timely manner on large networks. --- src/cola.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cola.js b/src/cola.js index 5e1b591b..3bbe22f3 100644 --- a/src/cola.js +++ b/src/cola.js @@ -461,6 +461,14 @@ ColaLayout.prototype.run = function(){ layout.trigger({ type: 'layoutstart', layout: layout }); + if( !options.infinite ){ + setTimeout(function(){ + if( !layout.manuallyStopped ){ + adaptor.stop(); + } + }, options.maxSimulationTime); + } + adaptor .avoidOverlaps( options.avoidOverlap ) .handleDisconnected( options.handleDisconnected ) @@ -474,14 +482,6 @@ ColaLayout.prototype.run = function(){ ) ; - if( !options.infinite ){ - setTimeout(function(){ - if( !layout.manuallyStopped ){ - adaptor.stop(); - } - }, options.maxSimulationTime); - } - return this; // chaining }; From 811b1d469187a015fea3e42c4adb3e878635e985 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Thu, 14 Nov 2024 21:21:27 +0100 Subject: [PATCH 2/3] Obey maxSimulationTime when not animated If not animated (requestAnimationFrame), the loop can never be interrupted by the maxSimulationTime timeout. Solve this by running the layout in a timeout as well. --- src/cola.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/cola.js b/src/cola.js index 3bbe22f3..d5fd42a6 100644 --- a/src/cola.js +++ b/src/cola.js @@ -180,19 +180,25 @@ ColaLayout.prototype.run = function(){ return ret; }; - if( options.animate ){ - let frame = function(){ - if( multitick() ){ return; } + // if not animated, run in a timeout so the + // options.maxSimulationTime timeout has a chance to interrupt + let frame = function(){ + if( multitick() ){ return; } + if (options.animate) { raf( frame ); - }; - - raf( frame ); - } else { - while( !inftick() ){ - // keep going... } + else { + setTimeout(() => frame()) + } + }; + if (options.animate) { + raf( frame ); } + else { + setTimeout(() => frame()) + } + }, on: nop, // dummy; not needed From b3f0d06b35b3b546d4e7f2d8208ca8de5e850d52 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Thu, 14 Nov 2024 21:27:35 +0100 Subject: [PATCH 3/3] regenerate --- cytoscape-cola.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cytoscape-cola.js b/cytoscape-cola.js index 6a81aeb0..758b9e85 100644 --- a/cytoscape-cola.js +++ b/cytoscape-cola.js @@ -291,20 +291,27 @@ ColaLayout.prototype.run = function () { return ret; }; - if (options.animate) { - var frame = function frame() { - if (multitick()) { - return; - } + // if not animated, run in a timeout so the + // options.maxSimulationTime timeout has a chance to interrupt + var frame = function frame() { + if (multitick()) { + return; + } + if (options.animate) { raf(frame); - }; - + } else { + setTimeout(function () { + return frame(); + }); + } + }; + if (options.animate) { raf(frame); } else { - while (!inftick()) { - // keep going... - } + setTimeout(function () { + return frame(); + }); } }, @@ -573,10 +580,6 @@ ColaLayout.prototype.run = function () { layout.trigger({ type: 'layoutstart', layout: layout }); - adaptor.avoidOverlaps(options.avoidOverlap).handleDisconnected(options.handleDisconnected).start(options.unconstrIter, options.userConstIter, options.allConstIter, undefined, // gridSnapIterations = 0 - undefined, // keepRunning = true - options.centerGraph); - if (!options.infinite) { setTimeout(function () { if (!layout.manuallyStopped) { @@ -585,6 +588,10 @@ ColaLayout.prototype.run = function () { }, options.maxSimulationTime); } + adaptor.avoidOverlaps(options.avoidOverlap).handleDisconnected(options.handleDisconnected).start(options.unconstrIter, options.userConstIter, options.allConstIter, undefined, // gridSnapIterations = 0 + undefined, // keepRunning = true + options.centerGraph); + return this; // chaining };