Skip to content
Open
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
37 changes: 37 additions & 0 deletions svgnest.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
var best = null;
var workerTimer = null;
var progress = 0;
var activeWorkers = []; // Track active workers

this.parsesvg = function(svgstring){
// reset if in progress
Expand Down Expand Up @@ -338,6 +339,9 @@
evalPath: 'util/eval.js'
});

// Track this worker
activeWorkers.push(p);

p.require('matrix.js');
p.require('geometryutil.js');
p.require('placementworker.js');
Expand Down Expand Up @@ -541,6 +545,9 @@
evalPath: 'util/eval.js'
});

// Track this worker too
activeWorkers.push(p2);

p2.require('json.js');
p2.require('clipper.js');
p2.require('matrix.js');
Expand Down Expand Up @@ -582,11 +589,25 @@
displayCallback();
}
self.working = false;

// Remove completed workers from tracking
var index = activeWorkers.indexOf(p);
if (index > -1) activeWorkers.splice(index, 1);
index = activeWorkers.indexOf(p2);
if (index > -1) activeWorkers.splice(index, 1);
}, function (err) {
console.log(err);
// Remove failed workers from tracking
var index = activeWorkers.indexOf(p);
if (index > -1) activeWorkers.splice(index, 1);
index = activeWorkers.indexOf(p2);
if (index > -1) activeWorkers.splice(index, 1);
});
}, function (err) {
console.log(err);
// Remove failed worker from tracking
var index = activeWorkers.indexOf(p);
if (index > -1) activeWorkers.splice(index, 1);
});
}

Expand Down Expand Up @@ -813,7 +834,23 @@
this.working = false;
if(workerTimer){
clearInterval(workerTimer);
workerTimer = null;
}

// Terminate all active workers
for(var i = 0; i < activeWorkers.length; i++){
try {
if(activeWorkers[i] && typeof activeWorkers[i].kill === 'function'){
activeWorkers[i].kill();
}
} catch(e) {
// Ignore errors when terminating workers
console.log('Error terminating worker:', e);
}
}

// Clear the active workers array
activeWorkers = [];
};
}

Expand Down