Introduce some optional chaining in the src/shared/ folder

This commit is contained in:
Jonas Jenwald 2021-09-17 13:46:47 +02:00
parent 94c2d08975
commit f9c2a8d437
3 changed files with 18 additions and 36 deletions

View File

@ -293,7 +293,7 @@ var Type2Parser = function type2Parser(aFilePath) {
font.set(token.name, stack.pop());
break;
default:
if (token.operand && token.operand.length) {
if (token.operand?.length) {
var array = [];
for (var j = 0; j < token.operand.length; j++) {
array.push(stack.pop());

View File

@ -87,7 +87,7 @@ class MessageHandler {
return;
}
if (data.stream) {
this._processStreamMessage(data);
this.#processStreamMessage(data);
return;
}
if (data.callback) {
@ -140,7 +140,7 @@ class MessageHandler {
return;
}
if (data.streamId) {
this._createStreamSink(data);
this.#createStreamSink(data);
return;
}
action(data.data);
@ -286,10 +286,7 @@ class MessageHandler {
);
}
/**
* @private
*/
_createStreamSink(data) {
#createStreamSink(data) {
const streamId = data.streamId,
sourceName = this.sourceName,
targetName = data.sourceName,
@ -388,10 +385,7 @@ class MessageHandler {
);
}
/**
* @private
*/
_processStreamMessage(data) {
#processStreamMessage(data) {
const streamId = data.streamId,
sourceName = this.sourceName,
targetName = data.sourceName,
@ -435,7 +429,7 @@ class MessageHandler {
streamSink.desiredSize = data.desiredSize;
new Promise(function (resolve) {
resolve(streamSink.onPull && streamSink.onPull());
resolve(streamSink.onPull?.());
}).then(
function () {
comObj.postMessage({
@ -471,12 +465,12 @@ class MessageHandler {
}
streamController.isClosed = true;
streamController.controller.close();
this._deleteStreamController(streamController, streamId);
this.#deleteStreamController(streamController, streamId);
break;
case StreamKind.ERROR:
assert(streamController, "error should have stream controller");
streamController.controller.error(wrapReason(data.reason));
this._deleteStreamController(streamController, streamId);
this.#deleteStreamController(streamController, streamId);
break;
case StreamKind.CANCEL_COMPLETE:
if (data.success) {
@ -484,7 +478,7 @@ class MessageHandler {
} else {
streamController.cancelCall.reject(wrapReason(data.reason));
}
this._deleteStreamController(streamController, streamId);
this.#deleteStreamController(streamController, streamId);
break;
case StreamKind.CANCEL:
if (!streamSink) {
@ -492,9 +486,7 @@ class MessageHandler {
}
new Promise(function (resolve) {
resolve(
streamSink.onCancel && streamSink.onCancel(wrapReason(data.reason))
);
resolve(streamSink.onCancel?.(wrapReason(data.reason)));
}).then(
function () {
comObj.postMessage({
@ -524,16 +516,13 @@ class MessageHandler {
}
}
/**
* @private
*/
async _deleteStreamController(streamController, streamId) {
async #deleteStreamController(streamController, streamId) {
// Delete the `streamController` only when the start, pull, and cancel
// capabilities have settled, to prevent `TypeError`s.
await Promise.allSettled([
streamController.startCall && streamController.startCall.promise,
streamController.pullCall && streamController.pullCall.promise,
streamController.cancelCall && streamController.cancelCall.promise,
streamController.startCall?.promise,
streamController.pullCall?.promise,
streamController.cancelCall?.promise,
]);
delete this.streamControllers[streamId];
}

View File

@ -393,10 +393,7 @@ function assert(cond, msg) {
// Checks if URLs use one of the allowed protocols, e.g. to avoid XSS.
function _isValidProtocol(url) {
if (!url) {
return false;
}
switch (url.protocol) {
switch (url?.protocol) {
case "http:":
case "https:":
case "ftp:":
@ -427,7 +424,7 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
const dots = url.match(/\./g);
// Avoid accidentally matching a *relative* URL pointing to a file named
// e.g. "www.pdf" or similar.
if (dots && dots.length >= 2) {
if (dots?.length >= 2) {
url = `http://${url}`;
}
}
@ -537,11 +534,7 @@ class AbortException extends BaseException {
}
function bytesToString(bytes) {
if (
typeof bytes !== "object" ||
bytes === null ||
bytes.length === undefined
) {
if (typeof bytes !== "object" || bytes?.length === undefined) {
unreachable("Invalid argument for bytesToString");
}
const length = bytes.length;
@ -954,7 +947,7 @@ function utf8StringToString(str) {
}
function isArrayBuffer(v) {
return typeof v === "object" && v !== null && v.byteLength !== undefined;
return typeof v === "object" && v?.byteLength !== undefined;
}
function isArrayEqual(arr1, arr2) {