Convert transport_stream.js
to use ES6 classes
This commit is contained in:
parent
c8c937c257
commit
737705264b
@ -12,12 +12,13 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
/* eslint no-var: error */
|
||||||
|
|
||||||
import { assert, createPromiseCapability } from '../shared/util';
|
import { assert, createPromiseCapability } from '../shared/util';
|
||||||
|
|
||||||
/** @implements {IPDFStream} */
|
/** @implements {IPDFStream} */
|
||||||
var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
class PDFDataTransportStream {
|
||||||
function PDFDataTransportStream(params, pdfDataRangeTransport) {
|
constructor(params, pdfDataRangeTransport) {
|
||||||
assert(pdfDataRangeTransport);
|
assert(pdfDataRangeTransport);
|
||||||
|
|
||||||
this._queuedChunks = [];
|
this._queuedChunks = [];
|
||||||
@ -25,7 +26,7 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|||||||
|
|
||||||
const initialData = params.initialData;
|
const initialData = params.initialData;
|
||||||
if (initialData && initialData.length > 0) {
|
if (initialData && initialData.length > 0) {
|
||||||
let buffer = new Uint8Array(initialData).buffer;
|
const buffer = new Uint8Array(initialData).buffer;
|
||||||
this._queuedChunks.push(buffer);
|
this._queuedChunks.push(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,95 +56,93 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|||||||
|
|
||||||
this._pdfDataRangeTransport.transportReady();
|
this._pdfDataRangeTransport.transportReady();
|
||||||
}
|
}
|
||||||
PDFDataTransportStream.prototype = {
|
|
||||||
_onReceiveData: function PDFDataTransportStream_onReceiveData(args) {
|
|
||||||
let buffer = new Uint8Array(args.chunk).buffer;
|
|
||||||
if (args.begin === undefined) {
|
|
||||||
if (this._fullRequestReader) {
|
|
||||||
this._fullRequestReader._enqueue(buffer);
|
|
||||||
} else {
|
|
||||||
this._queuedChunks.push(buffer);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var found = this._rangeReaders.some(function (rangeReader) {
|
|
||||||
if (rangeReader._begin !== args.begin) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
rangeReader._enqueue(buffer);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
assert(found);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
get _progressiveDataLength() {
|
_onReceiveData(args) {
|
||||||
return (this._fullRequestReader ? this._fullRequestReader._loaded : 0);
|
const buffer = new Uint8Array(args.chunk).buffer;
|
||||||
},
|
if (args.begin === undefined) {
|
||||||
|
if (this._fullRequestReader) {
|
||||||
_onProgress: function PDFDataTransportStream_onDataProgress(evt) {
|
this._fullRequestReader._enqueue(buffer);
|
||||||
if (evt.total === undefined) {
|
|
||||||
// Reporting to first range reader, if it exists.
|
|
||||||
let firstReader = this._rangeReaders[0];
|
|
||||||
if (firstReader && firstReader.onProgress) {
|
|
||||||
firstReader.onProgress({ loaded: evt.loaded, });
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let fullReader = this._fullRequestReader;
|
this._queuedChunks.push(buffer);
|
||||||
if (fullReader && fullReader.onProgress) {
|
}
|
||||||
fullReader.onProgress({ loaded: evt.loaded, total: evt.total, });
|
} else {
|
||||||
|
const found = this._rangeReaders.some(function(rangeReader) {
|
||||||
|
if (rangeReader._begin !== args.begin) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
rangeReader._enqueue(buffer);
|
||||||
},
|
return true;
|
||||||
|
|
||||||
_onProgressiveDone() {
|
|
||||||
if (this._fullRequestReader) {
|
|
||||||
this._fullRequestReader.progressiveDone();
|
|
||||||
}
|
|
||||||
this._progressiveDone = true;
|
|
||||||
},
|
|
||||||
|
|
||||||
_removeRangeReader:
|
|
||||||
function PDFDataTransportStream_removeRangeReader(reader) {
|
|
||||||
var i = this._rangeReaders.indexOf(reader);
|
|
||||||
if (i >= 0) {
|
|
||||||
this._rangeReaders.splice(i, 1);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getFullReader: function PDFDataTransportStream_getFullReader() {
|
|
||||||
assert(!this._fullRequestReader);
|
|
||||||
var queuedChunks = this._queuedChunks;
|
|
||||||
this._queuedChunks = null;
|
|
||||||
return new PDFDataTransportStreamReader(this, queuedChunks,
|
|
||||||
this._progressiveDone);
|
|
||||||
},
|
|
||||||
|
|
||||||
getRangeReader: function PDFDataTransportStream_getRangeReader(begin, end) {
|
|
||||||
if (end <= this._progressiveDataLength) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
var reader = new PDFDataTransportStreamRangeReader(this, begin, end);
|
|
||||||
this._pdfDataRangeTransport.requestDataRange(begin, end);
|
|
||||||
this._rangeReaders.push(reader);
|
|
||||||
return reader;
|
|
||||||
},
|
|
||||||
|
|
||||||
cancelAllRequests:
|
|
||||||
function PDFDataTransportStream_cancelAllRequests(reason) {
|
|
||||||
if (this._fullRequestReader) {
|
|
||||||
this._fullRequestReader.cancel(reason);
|
|
||||||
}
|
|
||||||
var readers = this._rangeReaders.slice(0);
|
|
||||||
readers.forEach(function (rangeReader) {
|
|
||||||
rangeReader.cancel(reason);
|
|
||||||
});
|
});
|
||||||
this._pdfDataRangeTransport.abort();
|
assert(found);
|
||||||
},
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/** @implements {IPDFStreamReader} */
|
get _progressiveDataLength() {
|
||||||
function PDFDataTransportStreamReader(stream, queuedChunks,
|
return (this._fullRequestReader ? this._fullRequestReader._loaded : 0);
|
||||||
progressiveDone = false) {
|
}
|
||||||
|
|
||||||
|
_onProgress(evt) {
|
||||||
|
if (evt.total === undefined) {
|
||||||
|
// Reporting to first range reader, if it exists.
|
||||||
|
const firstReader = this._rangeReaders[0];
|
||||||
|
if (firstReader && firstReader.onProgress) {
|
||||||
|
firstReader.onProgress({ loaded: evt.loaded, });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const fullReader = this._fullRequestReader;
|
||||||
|
if (fullReader && fullReader.onProgress) {
|
||||||
|
fullReader.onProgress({ loaded: evt.loaded, total: evt.total, });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_onProgressiveDone() {
|
||||||
|
if (this._fullRequestReader) {
|
||||||
|
this._fullRequestReader.progressiveDone();
|
||||||
|
}
|
||||||
|
this._progressiveDone = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
_removeRangeReader(reader) {
|
||||||
|
const i = this._rangeReaders.indexOf(reader);
|
||||||
|
if (i >= 0) {
|
||||||
|
this._rangeReaders.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getFullReader() {
|
||||||
|
assert(!this._fullRequestReader);
|
||||||
|
const queuedChunks = this._queuedChunks;
|
||||||
|
this._queuedChunks = null;
|
||||||
|
return new PDFDataTransportStreamReader(this, queuedChunks,
|
||||||
|
this._progressiveDone);
|
||||||
|
}
|
||||||
|
|
||||||
|
getRangeReader(begin, end) {
|
||||||
|
if (end <= this._progressiveDataLength) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const reader = new PDFDataTransportStreamRangeReader(this, begin, end);
|
||||||
|
this._pdfDataRangeTransport.requestDataRange(begin, end);
|
||||||
|
this._rangeReaders.push(reader);
|
||||||
|
return reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelAllRequests(reason) {
|
||||||
|
if (this._fullRequestReader) {
|
||||||
|
this._fullRequestReader.cancel(reason);
|
||||||
|
}
|
||||||
|
const readers = this._rangeReaders.slice(0);
|
||||||
|
readers.forEach(function(rangeReader) {
|
||||||
|
rangeReader.cancel(reason);
|
||||||
|
});
|
||||||
|
this._pdfDataRangeTransport.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @implements {IPDFStreamReader} */
|
||||||
|
class PDFDataTransportStreamReader {
|
||||||
|
constructor(stream, queuedChunks, progressiveDone = false) {
|
||||||
this._stream = stream;
|
this._stream = stream;
|
||||||
this._done = progressiveDone || false;
|
this._done = progressiveDone || false;
|
||||||
this._filename = null;
|
this._filename = null;
|
||||||
@ -156,73 +155,74 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|||||||
this._headersReady = Promise.resolve();
|
this._headersReady = Promise.resolve();
|
||||||
stream._fullRequestReader = this;
|
stream._fullRequestReader = this;
|
||||||
|
|
||||||
this.onProgress = null; // not used
|
this.onProgress = null;
|
||||||
}
|
}
|
||||||
PDFDataTransportStreamReader.prototype = {
|
|
||||||
_enqueue: function PDFDataTransportStreamReader_enqueue(chunk) {
|
|
||||||
if (this._done) {
|
|
||||||
return; // ignore new data
|
|
||||||
}
|
|
||||||
if (this._requests.length > 0) {
|
|
||||||
var requestCapability = this._requests.shift();
|
|
||||||
requestCapability.resolve({ value: chunk, done: false, });
|
|
||||||
} else {
|
|
||||||
this._queuedChunks.push(chunk);
|
|
||||||
}
|
|
||||||
this._loaded += chunk.byteLength;
|
|
||||||
},
|
|
||||||
|
|
||||||
get headersReady() {
|
_enqueue(chunk) {
|
||||||
return this._headersReady;
|
if (this._done) {
|
||||||
},
|
return; // Ignore new data.
|
||||||
|
}
|
||||||
|
if (this._requests.length > 0) {
|
||||||
|
const requestCapability = this._requests.shift();
|
||||||
|
requestCapability.resolve({ value: chunk, done: false, });
|
||||||
|
} else {
|
||||||
|
this._queuedChunks.push(chunk);
|
||||||
|
}
|
||||||
|
this._loaded += chunk.byteLength;
|
||||||
|
}
|
||||||
|
|
||||||
get filename() {
|
get headersReady() {
|
||||||
return this._filename;
|
return this._headersReady;
|
||||||
},
|
}
|
||||||
|
|
||||||
get isRangeSupported() {
|
get filename() {
|
||||||
return this._stream._isRangeSupported;
|
return this._filename;
|
||||||
},
|
}
|
||||||
|
|
||||||
get isStreamingSupported() {
|
get isRangeSupported() {
|
||||||
return this._stream._isStreamingSupported;
|
return this._stream._isRangeSupported;
|
||||||
},
|
}
|
||||||
|
|
||||||
get contentLength() {
|
get isStreamingSupported() {
|
||||||
return this._stream._contentLength;
|
return this._stream._isStreamingSupported;
|
||||||
},
|
}
|
||||||
|
|
||||||
async read() {
|
get contentLength() {
|
||||||
if (this._queuedChunks.length > 0) {
|
return this._stream._contentLength;
|
||||||
var chunk = this._queuedChunks.shift();
|
}
|
||||||
return { value: chunk, done: false, };
|
|
||||||
}
|
|
||||||
if (this._done) {
|
|
||||||
return { value: undefined, done: true, };
|
|
||||||
}
|
|
||||||
var requestCapability = createPromiseCapability();
|
|
||||||
this._requests.push(requestCapability);
|
|
||||||
return requestCapability.promise;
|
|
||||||
},
|
|
||||||
|
|
||||||
cancel: function PDFDataTransportStreamReader_cancel(reason) {
|
async read() {
|
||||||
this._done = true;
|
if (this._queuedChunks.length > 0) {
|
||||||
this._requests.forEach(function (requestCapability) {
|
const chunk = this._queuedChunks.shift();
|
||||||
requestCapability.resolve({ value: undefined, done: true, });
|
return { value: chunk, done: false, };
|
||||||
});
|
}
|
||||||
this._requests = [];
|
if (this._done) {
|
||||||
},
|
return { value: undefined, done: true, };
|
||||||
|
}
|
||||||
|
const requestCapability = createPromiseCapability();
|
||||||
|
this._requests.push(requestCapability);
|
||||||
|
return requestCapability.promise;
|
||||||
|
}
|
||||||
|
|
||||||
progressiveDone() {
|
cancel(reason) {
|
||||||
if (this._done) {
|
this._done = true;
|
||||||
return;
|
this._requests.forEach(function(requestCapability) {
|
||||||
}
|
requestCapability.resolve({ value: undefined, done: true, });
|
||||||
this._done = true;
|
});
|
||||||
},
|
this._requests = [];
|
||||||
};
|
}
|
||||||
|
|
||||||
/** @implements {IPDFStreamRangeReader} */
|
progressiveDone() {
|
||||||
function PDFDataTransportStreamRangeReader(stream, begin, end) {
|
if (this._done) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @implements {IPDFStreamRangeReader} */
|
||||||
|
class PDFDataTransportStreamRangeReader {
|
||||||
|
constructor(stream, begin, end) {
|
||||||
this._stream = stream;
|
this._stream = stream;
|
||||||
this._begin = begin;
|
this._begin = begin;
|
||||||
this._end = end;
|
this._end = end;
|
||||||
@ -232,55 +232,52 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|||||||
|
|
||||||
this.onProgress = null;
|
this.onProgress = null;
|
||||||
}
|
}
|
||||||
PDFDataTransportStreamRangeReader.prototype = {
|
|
||||||
_enqueue: function PDFDataTransportStreamRangeReader_enqueue(chunk) {
|
|
||||||
if (this._done) {
|
|
||||||
return; // ignore new data
|
|
||||||
}
|
|
||||||
if (this._requests.length === 0) {
|
|
||||||
this._queuedChunk = chunk;
|
|
||||||
} else {
|
|
||||||
var requestsCapability = this._requests.shift();
|
|
||||||
requestsCapability.resolve({ value: chunk, done: false, });
|
|
||||||
this._requests.forEach(function (requestCapability) {
|
|
||||||
requestCapability.resolve({ value: undefined, done: true, });
|
|
||||||
});
|
|
||||||
this._requests = [];
|
|
||||||
}
|
|
||||||
this._done = true;
|
|
||||||
this._stream._removeRangeReader(this);
|
|
||||||
},
|
|
||||||
|
|
||||||
get isStreamingSupported() {
|
_enqueue(chunk) {
|
||||||
return false;
|
if (this._done) {
|
||||||
},
|
return; // ignore new data
|
||||||
|
}
|
||||||
async read() {
|
if (this._requests.length === 0) {
|
||||||
if (this._queuedChunk) {
|
this._queuedChunk = chunk;
|
||||||
let chunk = this._queuedChunk;
|
} else {
|
||||||
this._queuedChunk = null;
|
const requestsCapability = this._requests.shift();
|
||||||
return { value: chunk, done: false, };
|
requestsCapability.resolve({ value: chunk, done: false, });
|
||||||
}
|
this._requests.forEach(function(requestCapability) {
|
||||||
if (this._done) {
|
|
||||||
return { value: undefined, done: true, };
|
|
||||||
}
|
|
||||||
var requestCapability = createPromiseCapability();
|
|
||||||
this._requests.push(requestCapability);
|
|
||||||
return requestCapability.promise;
|
|
||||||
},
|
|
||||||
|
|
||||||
cancel: function PDFDataTransportStreamRangeReader_cancel(reason) {
|
|
||||||
this._done = true;
|
|
||||||
this._requests.forEach(function (requestCapability) {
|
|
||||||
requestCapability.resolve({ value: undefined, done: true, });
|
requestCapability.resolve({ value: undefined, done: true, });
|
||||||
});
|
});
|
||||||
this._requests = [];
|
this._requests = [];
|
||||||
this._stream._removeRangeReader(this);
|
}
|
||||||
},
|
this._done = true;
|
||||||
};
|
this._stream._removeRangeReader(this);
|
||||||
|
}
|
||||||
|
|
||||||
return PDFDataTransportStream;
|
get isStreamingSupported() {
|
||||||
})();
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async read() {
|
||||||
|
if (this._queuedChunk) {
|
||||||
|
const chunk = this._queuedChunk;
|
||||||
|
this._queuedChunk = null;
|
||||||
|
return { value: chunk, done: false, };
|
||||||
|
}
|
||||||
|
if (this._done) {
|
||||||
|
return { value: undefined, done: true, };
|
||||||
|
}
|
||||||
|
const requestCapability = createPromiseCapability();
|
||||||
|
this._requests.push(requestCapability);
|
||||||
|
return requestCapability.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel(reason) {
|
||||||
|
this._done = true;
|
||||||
|
this._requests.forEach(function(requestCapability) {
|
||||||
|
requestCapability.resolve({ value: undefined, done: true, });
|
||||||
|
});
|
||||||
|
this._requests = [];
|
||||||
|
this._stream._removeRangeReader(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
PDFDataTransportStream,
|
PDFDataTransportStream,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user