Skip to content

Commit

Permalink
Fix #96
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pollithy committed Aug 10, 2017
1 parent fbaa4d1 commit 2618e66
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,18 +574,18 @@ Codec.mxgraph_to_neo4j = function(image_id, fragment_id, overwrite_xml_path) {
is_frame = true;
}
}
all_node_promises.push(database.add_node(image_id, fragment_id, label, cell.$))
all_node_promises.push(function() {return database.add_node(image_id, fragment_id, label, cell.$)})
});
return Promise.all(all_node_promises).then(function (values) {
return utils.chain_promises(all_node_promises).then(function (values) {
// create all the edges
var all_edge_promises = [];
if (edges.length > 0) {
edges.forEach(function (cell) {
all_edge_promises.push(
database.add_edge(image_id, fragment_id, cell.$.source, cell.$.target, cell.$)
function() {return database.add_edge(image_id, fragment_id, cell.$.source, cell.$.target, cell.$)}
);
});
return Promise.all(all_edge_promises);
return utils.chain_promises(all_edge_promises);
} else {
return values
}
Expand Down
1 change: 1 addition & 0 deletions src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ Database.add_node = function(image_id, fragment_id, node_label, node_attributes)
* @return {Promise}
* @param node_id
* @param frame_name
* @param groupType
*/
Database.add_frame_edge = function(node_id, frame_name, groupType) {
var session = this._get_session();
Expand Down
34 changes: 34 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,46 @@ function token_type_mapping(token_type) {
}
return null;
}
/**
* chains a list of functions (that return promises) and executes them in the right order
* [function() {return Promise.resolve();}, function() {return Promise.resolve();}]
*
* @param funcs is an array of functions returning promises
* @returns {Promise}
*/
function chain_promises(funcs) {
if (funcs.length < 1) {
return Promise.resolve();
}
var i = 0;
return chain_executor(funcs, i);
}

/**
* Recursive help method for chain_promises
* 1) executes a function that returns a promise (no params allowed)
* 2) chains itself to the success resolve of the promise
*
* @param funcs is an array of functions returning promises
* @param i is the current working index
*/
function chain_executor(funcs, i) {
var promise = funcs[i]();
return promise.then(function(){
console.log(i);
if (funcs.length > i+1) {
return chain_executor(funcs, i+1);
} else {
return Promise.resolve();
}
})
}

module.exports = {
'javascript_demo_constraint':javascript_demo_constraint,
'hash_of_file_content': hash_of_file_content,
'hash_xml_fragment': hash_xml_fragment,
'remove_image': remove_image,
'token_type_mapping': token_type_mapping,
'chain_promises': chain_promises
};
2 changes: 1 addition & 1 deletion test/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('utils', function() {
var creation_date = record['upload_date'];
console.log(creation_date);
var wrong = true;
if (Number(creation_date) === 1483244880000) {
if (Number(creation_date) === 1496000809000) {
wrong = false;
}
if (wrong) {
Expand Down

0 comments on commit 2618e66

Please sign in to comment.