Skip to content

Commit

Permalink
DataWorker.js from Stuk#555
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebingsoo authored May 1, 2023
1 parent 2ceb998 commit 68e1710
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/stream/DataWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ function DataWorker(dataP) {
self.data = data;
self.max = data && data.length || 0;
self.type = utils.getTypeOf(data);
if(!self.isPaused) {

if (self.type === 'blob') {
self.max = data.size;
}

if (!self.isPaused) {
self._tickAndRepeat();
}
}, function (e) {
Expand Down Expand Up @@ -85,13 +90,30 @@ DataWorker.prototype._tick = function() {
return false;
}

var self = this;
var size = DEFAULT_BLOCK_SIZE;
var data = null, nextIndex = Math.min(this.max, this.index + size);
if (this.index >= this.max) {
// EOF
return this.end();
} else {
switch(this.type) {
case "blob":
var chunk = self.data.slice(this.index, nextIndex);
var reader = new FileReader();
self.pause();
self.index = nextIndex;
reader.onload = function(e) {
self.push({
data : new Uint8Array(e.target.result),
meta : {
percent : self.max ? self.index / self.max * 100 : 0
}
});
self.resume();
};
reader.readAsArrayBuffer(chunk);
return;
case "string":
data = this.data.substring(this.index, nextIndex);
break;
Expand Down

0 comments on commit 68e1710

Please sign in to comment.