Skip to content

Commit

Permalink
Merge pull request #11972 from Snuffleupagus/ChunkedStream-loadedChun…
Browse files Browse the repository at this point in the history
…ks-Set

Change the `loadedChunks` property, on `ChunkedStream` instances, from an Array to a Set
  • Loading branch information
timvandermeij authored Jun 5, 2020
2 parents 891c706 + b7272a3 commit 039307f
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/core/chunked_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class ChunkedStream {
this.pos = 0;
this.end = length;
this.chunkSize = chunkSize;
this.loadedChunks = [];
this.numChunksLoaded = 0;
this._loadedChunks = new Set();
this.numChunks = Math.ceil(length / chunkSize);
this.manager = manager;
this.progressiveDataLength = 0;
Expand All @@ -42,7 +41,7 @@ class ChunkedStream {
getMissingChunks() {
const chunks = [];
for (let chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
chunks.push(chunk);
}
}
Expand All @@ -53,6 +52,10 @@ class ChunkedStream {
return [this];
}

get numChunksLoaded() {
return this._loadedChunks.size;
}

allChunksLoaded() {
return this.numChunksLoaded === this.numChunks;
}
Expand All @@ -75,10 +78,9 @@ class ChunkedStream {
const endChunk = Math.floor((end - 1) / chunkSize) + 1;

for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
if (!this.loadedChunks[curChunk]) {
this.loadedChunks[curChunk] = true;
++this.numChunksLoaded;
}
// Since a value can only occur *once* in a `Set`, there's no need to
// manually check `Set.prototype.has()` before adding the value here.
this._loadedChunks.add(curChunk);
}
}

Expand All @@ -95,10 +97,9 @@ class ChunkedStream {
: Math.floor(position / this.chunkSize);

for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
if (!this.loadedChunks[curChunk]) {
this.loadedChunks[curChunk] = true;
++this.numChunksLoaded;
}
// Since a value can only occur *once* in a `Set`, there's no need to
// manually check `Set.prototype.has()` before adding the value here.
this._loadedChunks.add(curChunk);
}
}

Expand All @@ -112,7 +113,7 @@ class ChunkedStream {
return;
}

if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
throw new MissingDataException(pos, pos + 1);
}
this.lastSuccessfulEnsureByteChunk = chunk;
Expand All @@ -130,7 +131,7 @@ class ChunkedStream {
const beginChunk = Math.floor(begin / chunkSize);
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
throw new MissingDataException(begin, end);
}
}
Expand All @@ -140,15 +141,15 @@ class ChunkedStream {
const numChunks = this.numChunks;
for (let i = 0; i < numChunks; ++i) {
const chunk = (beginChunk + i) % numChunks; // Wrap around to beginning.
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
return chunk;
}
}
return null;
}

hasChunk(chunk) {
return !!this.loadedChunks[chunk];
return this._loadedChunks.has(chunk);
}

get length() {
Expand Down Expand Up @@ -286,7 +287,7 @@ class ChunkedStream {
const endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
const missingChunks = [];
for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
missingChunks.push(chunk);
}
}
Expand Down

0 comments on commit 039307f

Please sign in to comment.