I have a problem that I tried to solve multiple times but I continue to fail.
I am not sure whether the problem falls into the bucket "You don't understand JavaScript fundamentals" or into the bucket "You don't understand paper.js".
Background
- The code is run in an node.js environment
- I execute multiple functions simultaneously using async.each
- Each function creates a paper.js project and then removes it at the end
- To avoid interferences, I create a list of paperscope objects before I call async.each. Then each function gets called with one of the paperscope objects.
It looks something like this:
var list_of_paperscopes = [];
for (var i = 0; i < number_of_desired_results; i++){
list_of_paperscopes.push( new paper.PaperScope() );
}
list_of_functions[1] = function (paperscope, callback) {
paperscope.setup(new paperscope.Size(500, 500));
// etc. etc.
// export paper result
paperscope.project.remove();
},
list_of_functions[2] = function (paperscope, callback) {
paperscope.setup(new paperscope.Size(300, 300));
// etc. etc.
// export paper result
paperscope.project.remove();
}
// ...
async.each(list_of_indices, function (index, asyncEachCallback) {
var paperscope = list_of_paperscopes.pop();
list_of_functions[index](paperscope, function (err, value) {
// do something
// etc etc.
The problem
The general code works. But the different paper projects interfere with each other. Elements that were meant to be in all projects sometimes accumulate in a single one and lack in all others.
Question
How do I prevent simultaneously executed paper projects from interfering with each other? Is there a way that allows me to use async.each, or do I have to switch to a slow consecutive execution?
[EDIT:] Updated and more complete code example
// This is still a toy example
// That is why some things may appear nonsensical when - in fact - they are required
var list_of_functions = [];
list_of_functions[0] = function (paperscope, callback) {
paperscope.setup(new paperscope.Size(300, 300));
// etc. etc.
// export paper result
paperscope.project.remove();
}
list_of_functions[1] = function (paperscope, callback) {
paperscope.setup(new paperscope.Size(300, 300));
// The following text will appear multiple times in the same paper project but not the others for THIS function with ID 1
var some_text = new paperscope.PointText({
point: [240, 190],
content: 'some text',
fillColor: 'black',
fontFamily: 'Courier New',
fontSize: 7,
justification: 'center'
});
// etc. etc.
// export paper result
paperscope.project.remove();
}
// This array determines how often we will call each function
var list_of_function_indices = [0, 1, 1, 1] // I want to execute function with ID 0 once, and the other function 3 times
async.each(list_of_function_indices, function (function_index, asyncEachCallback) {
// Generate a new PaperScope() object
var paperscope = new paper.PaperScope();
list_of_functions[function_index](paperscope, function (err, value) {
if (err) {
sails.log.error(err);
}
// always continue
asyncEachCallback();
});
}, function (err) {
if (err) {
sails.log.debug('A function failed...');
}
});
Is it that paper.js cannot be executed asynchronously in general? I don't fully understand this: http://paperjs.org/reference/paperscope/#project