extracted blobSlicer, fixed download cancel tests

This commit is contained in:
Danny Coates 2018-07-25 12:29:19 -07:00
parent 5483dc2506
commit 5e9e63944b
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
6 changed files with 58 additions and 58 deletions

27
app/blobSlicer.js Normal file
View file

@ -0,0 +1,27 @@
export default class BlobSlicer {
constructor(blob, size) {
this.blob = blob;
this.index = 0;
this.chunkSize = size;
}
pull(controller) {
return new Promise((resolve, reject) => {
const bytesLeft = this.blob.size - this.index;
if (bytesLeft <= 0) {
controller.close();
return resolve();
}
const size = Math.min(this.chunkSize, bytesLeft);
const blob = this.blob.slice(this.index, this.index + size);
const reader = new FileReader();
reader.onload = () => {
controller.enqueue(new Uint8Array(reader.result));
resolve();
};
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
this.index += size;
});
}
}