Skip to content

Commit

Permalink
rewrite BAM feature-reading loop to stuff itself into a timeout perio…
Browse files Browse the repository at this point in the history
…dically to avoid blocking UI events
  • Loading branch information
rbuels committed Oct 23, 2012
1 parent a2af4cb commit 6d9e415
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions src/JBrowse/Store/SeqFeature/BAM/File.js
Expand Up @@ -330,32 +330,50 @@ var BamFile = declare( null,

var data = BAMUtil.unbgzf(r, c.maxv.block - c.minv.block + 1);

thisB.readBamFeatures( new Uint8Array(data), c.minv.offset, features );

if( ++chunksProcessed == chunks.length )
callback( features );
thisB.readBamFeatures( new Uint8Array(data), c.minv.offset, features, function() {
if( ++chunksProcessed == chunks.length )
callback( features );
});
});
});
},

readBamFeatures: function(ba, blockStart, sink ) {
while (true) {
var blockSize = readInt(ba, blockStart);
var blockEnd = blockStart + blockSize;
if (blockEnd >= ba.length) {
return sink;
}
readBamFeatures: function(ba, blockStart, sink, callback ) {
var that = this;
var featureCount = 0;

var feature = new BAMFeature({
store: this.store,
file: this,
bytes: { byteArray: ba, start: blockStart, end: blockEnd }
});
var maxFeaturesWithoutYielding = 300;

sink.push(feature);
blockStart = blockEnd + 4;
while ( true ) {
if( blockStart >= ba.length ) {
// if we're done, call the callback and return
callback( sink );
return;
}
else if( featureCount <= maxFeaturesWithoutYielding ) {
// if we've read no more than 200 features this cycle, read another one
var blockSize = readInt(ba, blockStart);
var blockEnd = blockStart + blockSize;

var feature = new BAMFeature({
store: this.store,
file: this,
bytes: { byteArray: ba, start: blockStart, end: blockEnd }
});
sink.push(feature);
featureCount++;
blockStart = blockEnd + 4;
}
else {
// if we're not done but we've read a good chunk of
// features, put the rest of our work into a timeout to continue
// later, avoiding blocking any UI stuff that's going on
window.setTimeout( function() {
that.readBamFeatures( ba, blockStart, sink, callback );
}, 1);
return;
}
}
// Exits via top of loop.
}
});

Expand Down

0 comments on commit 6d9e415

Please sign in to comment.