Skip to content

Commit

Permalink
Using async links initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
anvaka committed Aug 5, 2015
1 parent efd0869 commit 45ed887
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"ngraph.events": "0.0.3",
"node-libs-browser": "^0.5.0",
"qs": "^4.0.0",
"rafor": "^1.0.2",
"react": "^0.13.3",
"react-bootstrap": "^0.23.4",
"react-list": "^0.6.0",
Expand Down
32 changes: 27 additions & 5 deletions src/galaxy/service/graphLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import request from './request.js';
import createGraph from './graph.js';
import appEvents from './appEvents.js';
import appConfig from '../native/appConfig.js';
import asyncFor from 'rafor';
import Promise from 'bluebird';

export default loadGraph;

Expand Down Expand Up @@ -117,13 +119,15 @@ function loadGraph(name, progress) {
var links = new Int32Array(buffer);
var lastArray = [];
outLinks[0] = lastArray;
asyncFor(links, processLink, reportBack);
var deffered = defer();

for (var i = 0; i < links.length; ++i) {
if (links[i] < 0) {
var srcIndex = -(links[i]) - 1;
function processLink(link) {
if (link < 0) {
var srcIndex = -link - 1;
lastArray = outLinks[srcIndex] = [];
} else {
var toNode = links[i] - 1;
var toNode = link - 1;
lastArray.push(toNode);
if (inLinks[toNode] === undefined) {
inLinks[toNode] = [srcIndex];
Expand All @@ -133,7 +137,12 @@ function loadGraph(name, progress) {
}
}

appEvents.linksDownloaded.fire(outLinks, inLinks);
function reportBack() {
appEvents.linksDownloaded.fire(outLinks, inLinks);
deffered.resolve();
}

return deffered.promise;
}

function loadLabels() {
Expand All @@ -158,3 +167,16 @@ function loadGraph(name, progress) {
};
}
}

function defer() {
var resolve, reject;
var promise = new Promise(function() {
resolve = arguments[0];
reject = arguments[1];
});
return {
resolve: resolve,
reject: reject,
promise: promise
};
}

0 comments on commit 45ed887

Please sign in to comment.