Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve timeout handling #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions cytoscape-cola.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
},

Expand Down Expand Up @@ -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) {
Expand All @@ -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
};

Expand Down
40 changes: 23 additions & 17 deletions src/cola.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -461,6 +467,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 )
Expand All @@ -474,14 +488,6 @@ ColaLayout.prototype.run = function(){
)
;

if( !options.infinite ){
setTimeout(function(){
if( !layout.manuallyStopped ){
adaptor.stop();
}
}, options.maxSimulationTime);
}

return this; // chaining
};

Expand Down