-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
always wait for active read operation to finish before resetting data…
… processors
- Loading branch information
1 parent
fa04bd1
commit 95d2d6f
Showing
9 changed files
with
112 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import CRC32 from '../Util/CRC32.js'; | ||
|
||
export default class DataProcessorChunkReader { | ||
/** @type {DataReader} */ reader; | ||
/** @type {boolean} */ eof = false; | ||
/** @type {boolean} */ closed = false; | ||
/** @type {boolean} */ reading = false; | ||
/** @type {Array} */ closePromises = []; | ||
/** @type {?CRC32} */ crc = null; | ||
|
||
/** | ||
* @param {DataReader} reader | ||
* @param {boolean} createCrc | ||
*/ | ||
constructor(reader, createCrc = false) { | ||
this.reader = reader; | ||
this.crc = createCrc ? new CRC32() : null; | ||
} | ||
|
||
/** | ||
* @param {number} length | ||
* @return {Promise<Uint8Array>} | ||
*/ | ||
async getChunk(length) { | ||
if (this.closed) { | ||
return new Uint8Array(0); | ||
} | ||
if (this.reading) { | ||
throw new Error('Simultaneous read not supported'); | ||
} | ||
this.reading = true; | ||
if (this.reader.offset + length > this.reader.byteLength) { | ||
this.eof = true; | ||
length = this.reader.byteLength - this.reader.offset; | ||
} | ||
let chunk = await this.reader.read(length); | ||
|
||
if(this.crc) { | ||
this.crc.add(chunk); | ||
} | ||
|
||
this.reading = false; | ||
|
||
if (this.closePromises.length > 0) { | ||
this.closePromises.forEach(resolve => resolve()); | ||
this.closePromises = []; | ||
} | ||
|
||
return chunk; | ||
} | ||
|
||
/** | ||
* @return {boolean} | ||
*/ | ||
isEof() { | ||
return this.eof; | ||
} | ||
|
||
/** | ||
* @return {?CRC32} | ||
*/ | ||
getCrc() { | ||
return this.crc; | ||
} | ||
|
||
/** | ||
* @return {Promise<void>} | ||
*/ | ||
close() { | ||
this.closed = true; | ||
return new Promise(resolve => { | ||
if (this.reading) { | ||
this.closePromises.push(resolve); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
import AbstractDataProcessor from './AbstractDataProcessor.js'; | ||
|
||
export default class PassThroughDataProcessor extends AbstractDataProcessor { | ||
/** @type {boolean} */ eof = false; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
async reset() { | ||
super.reset(); | ||
await super.reset(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
async generate(length) { | ||
if(this.eof) { | ||
if(this.chunkReader.isEof()) { | ||
return null; | ||
} | ||
|
||
return await this.getChunkFromReader(length); | ||
return await this.chunkReader.getChunk(length); | ||
} | ||
} | ||
|