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()); font.set(token.name, stack.pop());
break; break;
default: default:
if (token.operand && token.operand.length) { if (token.operand?.length) {
var array = []; var array = [];
for (var j = 0; j < token.operand.length; j++) { for (var j = 0; j < token.operand.length; j++) {
array.push(stack.pop()); array.push(stack.pop());

View File

@ -87,7 +87,7 @@ class MessageHandler {
return; return;
} }
if (data.stream) { if (data.stream) {
this._processStreamMessage(data); this.#processStreamMessage(data);
return; return;
} }
if (data.callback) { if (data.callback) {
@ -140,7 +140,7 @@ class MessageHandler {
return; return;
} }
if (data.streamId) { if (data.streamId) {
this._createStreamSink(data); this.#createStreamSink(data);
return; return;
} }
action(data.data); action(data.data);
@ -286,10 +286,7 @@ class MessageHandler {
); );
} }
/** #createStreamSink(data) {
* @private
*/
_createStreamSink(data) {
const streamId = data.streamId, const streamId = data.streamId,
sourceName = this.sourceName, sourceName = this.sourceName,
targetName = data.sourceName, targetName = data.sourceName,
@ -388,10 +385,7 @@ class MessageHandler {
); );
} }
/** #processStreamMessage(data) {
* @private
*/
_processStreamMessage(data) {
const streamId = data.streamId, const streamId = data.streamId,
sourceName = this.sourceName, sourceName = this.sourceName,
targetName = data.sourceName, targetName = data.sourceName,
@ -435,7 +429,7 @@ class MessageHandler {
streamSink.desiredSize = data.desiredSize; streamSink.desiredSize = data.desiredSize;
new Promise(function (resolve) { new Promise(function (resolve) {
resolve(streamSink.onPull && streamSink.onPull()); resolve(streamSink.onPull?.());
}).then( }).then(
function () { function () {
comObj.postMessage({ comObj.postMessage({
@ -471,12 +465,12 @@ class MessageHandler {
} }
streamController.isClosed = true; streamController.isClosed = true;
streamController.controller.close(); streamController.controller.close();
this._deleteStreamController(streamController, streamId); this.#deleteStreamController(streamController, streamId);
break; break;
case StreamKind.ERROR: case StreamKind.ERROR:
assert(streamController, "error should have stream controller"); assert(streamController, "error should have stream controller");
streamController.controller.error(wrapReason(data.reason)); streamController.controller.error(wrapReason(data.reason));
this._deleteStreamController(streamController, streamId); this.#deleteStreamController(streamController, streamId);
break; break;
case StreamKind.CANCEL_COMPLETE: case StreamKind.CANCEL_COMPLETE:
if (data.success) { if (data.success) {
@ -484,7 +478,7 @@ class MessageHandler {
} else { } else {
streamController.cancelCall.reject(wrapReason(data.reason)); streamController.cancelCall.reject(wrapReason(data.reason));
} }
this._deleteStreamController(streamController, streamId); this.#deleteStreamController(streamController, streamId);
break; break;
case StreamKind.CANCEL: case StreamKind.CANCEL:
if (!streamSink) { if (!streamSink) {
@ -492,9 +486,7 @@ class MessageHandler {
} }
new Promise(function (resolve) { new Promise(function (resolve) {
resolve( resolve(streamSink.onCancel?.(wrapReason(data.reason)));
streamSink.onCancel && streamSink.onCancel(wrapReason(data.reason))
);
}).then( }).then(
function () { function () {
comObj.postMessage({ comObj.postMessage({
@ -524,16 +516,13 @@ class MessageHandler {
} }
} }
/** async #deleteStreamController(streamController, streamId) {
* @private
*/
async _deleteStreamController(streamController, streamId) {
// Delete the `streamController` only when the start, pull, and cancel // Delete the `streamController` only when the start, pull, and cancel
// capabilities have settled, to prevent `TypeError`s. // capabilities have settled, to prevent `TypeError`s.
await Promise.allSettled([ await Promise.allSettled([
streamController.startCall && streamController.startCall.promise, streamController.startCall?.promise,
streamController.pullCall && streamController.pullCall.promise, streamController.pullCall?.promise,
streamController.cancelCall && streamController.cancelCall.promise, streamController.cancelCall?.promise,
]); ]);
delete this.streamControllers[streamId]; 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. // Checks if URLs use one of the allowed protocols, e.g. to avoid XSS.
function _isValidProtocol(url) { function _isValidProtocol(url) {
if (!url) { switch (url?.protocol) {
return false;
}
switch (url.protocol) {
case "http:": case "http:":
case "https:": case "https:":
case "ftp:": case "ftp:":
@ -427,7 +424,7 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
const dots = url.match(/\./g); const dots = url.match(/\./g);
// Avoid accidentally matching a *relative* URL pointing to a file named // Avoid accidentally matching a *relative* URL pointing to a file named
// e.g. "www.pdf" or similar. // e.g. "www.pdf" or similar.
if (dots && dots.length >= 2) { if (dots?.length >= 2) {
url = `http://${url}`; url = `http://${url}`;
} }
} }
@ -537,11 +534,7 @@ class AbortException extends BaseException {
} }
function bytesToString(bytes) { function bytesToString(bytes) {
if ( if (typeof bytes !== "object" || bytes?.length === undefined) {
typeof bytes !== "object" ||
bytes === null ||
bytes.length === undefined
) {
unreachable("Invalid argument for bytesToString"); unreachable("Invalid argument for bytesToString");
} }
const length = bytes.length; const length = bytes.length;
@ -954,7 +947,7 @@ function utf8StringToString(str) {
} }
function isArrayBuffer(v) { function isArrayBuffer(v) {
return typeof v === "object" && v !== null && v.byteLength !== undefined; return typeof v === "object" && v?.byteLength !== undefined;
} }
function isArrayEqual(arr1, arr2) { function isArrayEqual(arr1, arr2) {