Use optional chaining more in the src/display/network.js file

Also changes the different `_onDone`/`_onProgress` methods to use consistent parameter names, and some other small improvements.
This commit is contained in:
Jonas Jenwald 2021-08-30 09:37:06 +02:00
parent 72c34b2964
commit bd9a92a161

View File

@ -43,11 +43,10 @@ function getArrayBuffer(xhr) {
} }
class NetworkManager { class NetworkManager {
constructor(url, args) { constructor(url, args = {}) {
this.url = url; this.url = url;
args = args || {};
this.isHttp = /^https?:/i.test(url); this.isHttp = /^https?:/i.test(url);
this.httpHeaders = (this.isHttp && args.httpHeaders) || {}; this.httpHeaders = (this.isHttp && args.httpHeaders) || Object.create(null);
this.withCredentials = args.withCredentials || false; this.withCredentials = args.withCredentials || false;
this.getXhr = this.getXhr =
args.getXhr || args.getXhr ||
@ -77,9 +76,7 @@ class NetworkManager {
request(args) { request(args) {
const xhr = this.getXhr(); const xhr = this.getXhr();
const xhrId = this.currXhrId++; const xhrId = this.currXhrId++;
const pendingRequest = (this.pendingRequests[xhrId] = { const pendingRequest = (this.pendingRequests[xhrId] = { xhr });
xhr,
});
xhr.open("GET", this.url); xhr.open("GET", this.url);
xhr.withCredentials = this.withCredentials; xhr.withCredentials = this.withCredentials;
@ -119,20 +116,15 @@ class NetworkManager {
onProgress(xhrId, evt) { onProgress(xhrId, evt) {
const pendingRequest = this.pendingRequests[xhrId]; const pendingRequest = this.pendingRequests[xhrId];
if (!pendingRequest) { if (!pendingRequest) {
// Maybe abortRequest was called... return; // Maybe abortRequest was called...
return;
}
if (pendingRequest.onProgress) {
pendingRequest.onProgress(evt);
} }
pendingRequest.onProgress?.(evt);
} }
onStateChange(xhrId, evt) { onStateChange(xhrId, evt) {
const pendingRequest = this.pendingRequests[xhrId]; const pendingRequest = this.pendingRequests[xhrId];
if (!pendingRequest) { if (!pendingRequest) {
// Maybe abortRequest was called... return; // Maybe abortRequest was called...
return;
} }
const xhr = pendingRequest.xhr; const xhr = pendingRequest.xhr;
@ -155,9 +147,7 @@ class NetworkManager {
// Success status == 0 can be on ftp, file and other protocols. // Success status == 0 can be on ftp, file and other protocols.
if (xhr.status === 0 && this.isHttp) { if (xhr.status === 0 && this.isHttp) {
if (pendingRequest.onError) { pendingRequest.onError?.(xhr.status);
pendingRequest.onError(xhr.status);
}
return; return;
} }
const xhrStatus = xhr.status || OK_RESPONSE; const xhrStatus = xhr.status || OK_RESPONSE;
@ -173,9 +163,7 @@ class NetworkManager {
!ok_response_on_range_request && !ok_response_on_range_request &&
xhrStatus !== pendingRequest.expectedStatus xhrStatus !== pendingRequest.expectedStatus
) { ) {
if (pendingRequest.onError) { pendingRequest.onError?.(xhr.status);
pendingRequest.onError(xhr.status);
}
return; return;
} }
@ -192,8 +180,8 @@ class NetworkManager {
begin: 0, begin: 0,
chunk, chunk,
}); });
} else if (pendingRequest.onError) { } else {
pendingRequest.onError(xhr.status); pendingRequest.onError?.(xhr.status);
} }
} }
@ -256,9 +244,8 @@ class PDFNetworkStream {
} }
cancelAllRequests(reason) { cancelAllRequests(reason) {
if (this._fullRequestReader) { this._fullRequestReader?.cancel(reason);
this._fullRequestReader.cancel(reason);
}
for (const reader of this._rangeRequestReaders.slice(0)) { for (const reader of this._rangeRequestReaders.slice(0)) {
reader.cancel(reason); reader.cancel(reason);
} }
@ -332,13 +319,13 @@ class PDFNetworkStreamFullRequestReader {
this._headersReceivedCapability.resolve(); this._headersReceivedCapability.resolve();
} }
_onDone(args) { _onDone(data) {
if (args) { if (data) {
if (this._requests.length > 0) { if (this._requests.length > 0) {
const requestCapability = this._requests.shift(); const requestCapability = this._requests.shift();
requestCapability.resolve({ value: args.chunk, done: false }); requestCapability.resolve({ value: data.chunk, done: false });
} else { } else {
this._cachedChunks.push(args.chunk); this._cachedChunks.push(data.chunk);
} }
} }
this._done = true; this._done = true;
@ -352,25 +339,21 @@ class PDFNetworkStreamFullRequestReader {
} }
_onError(status) { _onError(status) {
const url = this._url; this._storedError = createResponseStatusError(status, this._url);
const exception = createResponseStatusError(status, url); this._headersReceivedCapability.reject(this._storedError);
this._storedError = exception;
this._headersReceivedCapability.reject(exception);
for (const requestCapability of this._requests) { for (const requestCapability of this._requests) {
requestCapability.reject(exception); requestCapability.reject(this._storedError);
} }
this._requests.length = 0; this._requests.length = 0;
this._cachedChunks.length = 0; this._cachedChunks.length = 0;
} }
_onProgress(data) { _onProgress(evt) {
if (this.onProgress) { this.onProgress?.({
this.onProgress({ loaded: evt.loaded,
loaded: data.loaded, total: evt.lengthComputable ? evt.total : this._contentLength,
total: data.lengthComputable ? data.total : this._contentLength,
}); });
} }
}
get filename() { get filename() {
return this._filename; return this._filename;
@ -440,9 +423,7 @@ class PDFNetworkStreamRangeRequestReader {
} }
_close() { _close() {
if (this.onClosed) { this.onClosed?.(this);
this.onClosed(this);
}
} }
_onDone(data) { _onDone(data) {
@ -462,10 +443,8 @@ class PDFNetworkStreamRangeRequestReader {
} }
_onProgress(evt) { _onProgress(evt) {
if (!this.isStreamingSupported && this.onProgress) { if (!this.isStreamingSupported) {
this.onProgress({ this.onProgress?.({ loaded: evt.loaded });
loaded: evt.loaded,
});
} }
} }