Merge pull request #15398 from Snuffleupagus/more-optional-chaining

Use more optional chaining in the code-base
This commit is contained in:
Jonas Jenwald 2022-09-06 20:31:03 +02:00 committed by GitHub
commit af6aacfc0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 51 additions and 119 deletions

View File

@ -380,9 +380,7 @@ class AnnotationElement {
const commonActions = this._commonActions; const commonActions = this._commonActions;
for (const name of Object.keys(jsEvent.detail)) { for (const name of Object.keys(jsEvent.detail)) {
const action = actions[name] || commonActions[name]; const action = actions[name] || commonActions[name];
if (action) { action?.(jsEvent);
action(jsEvent);
}
} }
} }
@ -752,9 +750,7 @@ class LinkAnnotationElement extends AnnotationElement {
} }
link.onclick = () => { link.onclick = () => {
if (otherClickAction) { otherClickAction?.();
otherClickAction();
}
const { const {
fields: resetFormFields, fields: resetFormFields,

View File

@ -1354,9 +1354,7 @@ class PDFPageProxy {
pageColors = null, pageColors = null,
printAnnotationStorage = null, printAnnotationStorage = null,
}) { }) {
if (this._stats) { this._stats?.time("Overall");
this._stats.time("Overall");
}
const intentArgs = this._transport.getRenderingIntent( const intentArgs = this._transport.getRenderingIntent(
intent, intent,
@ -1401,9 +1399,7 @@ class PDFPageProxy {
separateAnnots: null, separateAnnots: null,
}; };
if (this._stats) { this._stats?.time("Page Request");
this._stats.time("Page Request");
}
this._pumpOperatorList(intentArgs); this._pumpOperatorList(intentArgs);
} }
@ -1427,10 +1423,9 @@ class PDFPageProxy {
} else { } else {
internalRenderTask.capability.resolve(); internalRenderTask.capability.resolve();
} }
if (this._stats) {
this._stats.timeEnd("Rendering"); this._stats?.timeEnd("Rendering");
this._stats.timeEnd("Overall"); this._stats?.timeEnd("Overall");
}
}; };
const internalRenderTask = new InternalRenderTask({ const internalRenderTask = new InternalRenderTask({
@ -1465,9 +1460,8 @@ class PDFPageProxy {
complete(); complete();
return; return;
} }
if (this._stats) { this._stats?.time("Rendering");
this._stats.time("Rendering");
}
internalRenderTask.initializeGraphics({ internalRenderTask.initializeGraphics({
transparency, transparency,
optionalContentConfig, optionalContentConfig,
@ -1523,9 +1517,7 @@ class PDFPageProxy {
separateAnnots: null, separateAnnots: null,
}; };
if (this._stats) { this._stats?.time("Page Request");
this._stats.time("Page Request");
}
this._pumpOperatorList(intentArgs); this._pumpOperatorList(intentArgs);
} }
return intentState.opListReadCapability.promise; return intentState.opListReadCapability.promise;
@ -1697,14 +1689,11 @@ class PDFPageProxy {
if (!intentState) { if (!intentState) {
return; // Rendering was cancelled. return; // Rendering was cancelled.
} }
if (this._stats) { this._stats?.timeEnd("Page Request");
this._stats.timeEnd("Page Request");
}
// TODO Refactor RenderPageRequest to separate rendering // TODO Refactor RenderPageRequest to separate rendering
// and operator list logic // and operator list logic
if (intentState.displayReadyCapability) { intentState.displayReadyCapability?.resolve(transparency);
intentState.displayReadyCapability.resolve(transparency);
}
} }
/** /**
@ -3259,17 +3248,14 @@ class InternalRenderTask {
}); });
this.operatorListIdx = 0; this.operatorListIdx = 0;
this.graphicsReady = true; this.graphicsReady = true;
if (this.graphicsReadyCallback) { this.graphicsReadyCallback?.();
this.graphicsReadyCallback();
}
} }
cancel(error = null) { cancel(error = null) {
this.running = false; this.running = false;
this.cancelled = true; this.cancelled = true;
if (this.gfx) { this.gfx?.endDrawing();
this.gfx.endDrawing();
}
if (this._canvas) { if (this._canvas) {
InternalRenderTask.#canvasInUse.delete(this._canvas); InternalRenderTask.#canvasInUse.delete(this._canvas);
} }
@ -3289,10 +3275,7 @@ class InternalRenderTask {
} }
return; return;
} }
this.stepper?.updateOperatorList(this.operatorList);
if (this.stepper) {
this.stepper.updateOperatorList(this.operatorList);
}
if (this.running) { if (this.running) {
return; return;

View File

@ -88,9 +88,8 @@ class PDFFetchStream {
} }
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);
} }
@ -202,9 +201,7 @@ class PDFFetchStreamReader {
} }
cancel(reason) { cancel(reason) {
if (this._reader) { this._reader?.cancel(reason);
this._reader.cancel(reason);
}
this._abortController.abort(); this._abortController.abort();
} }
} }
@ -256,17 +253,14 @@ class PDFFetchStreamRangeReader {
return { value, done }; return { value, done };
} }
this._loaded += value.byteLength; this._loaded += value.byteLength;
if (this.onProgress) { this.onProgress?.({ loaded: this._loaded });
this.onProgress({ loaded: this._loaded });
}
const buffer = new Uint8Array(value).buffer; const buffer = new Uint8Array(value).buffer;
return { value: buffer, done: false }; return { value: buffer, done: false };
} }
cancel(reason) { cancel(reason) {
if (this._reader) { this._reader?.cancel(reason);
this._reader.cancel(reason);
}
this._abortController.abort(); this._abortController.abort();
} }
} }

View File

@ -402,9 +402,7 @@ class FontFaceObject {
); );
} }
if (this.fontRegistry) { this.fontRegistry?.registerFont(this);
this.fontRegistry.registerFont(this);
}
return nativeFontFace; return nativeFontFace;
} }
@ -426,9 +424,7 @@ class FontFaceObject {
rule = `@font-face {font-family:"${this.cssFontInfo.fontFamily}";${css}src:${url}}`; rule = `@font-face {font-family:"${this.cssFontInfo.fontFamily}";${css}src:${url}}`;
} }
if (this.fontRegistry) { this.fontRegistry?.registerFont(this, url);
this.fontRegistry.registerFont(this, url);
}
return rule; return rule;
} }

View File

@ -95,9 +95,8 @@ class PDFNodeStream {
} }
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);
} }
@ -252,9 +251,8 @@ class BaseRangeReader {
return this.read(); return this.read();
} }
this._loaded += chunk.length; this._loaded += chunk.length;
if (this.onProgress) { this.onProgress?.({ loaded: this._loaded });
this.onProgress({ loaded: this._loaded });
}
// Ensure that `read()` method returns ArrayBuffer. // Ensure that `read()` method returns ArrayBuffer.
const buffer = new Uint8Array(chunk).buffer; const buffer = new Uint8Array(chunk).buffer;
return { value: buffer, done: false }; return { value: buffer, done: false };

View File

@ -1543,9 +1543,7 @@ if (
} }
eoFill() { eoFill() {
if (this.current.element) { this.current.element?.setAttributeNS(null, "fill-rule", "evenodd");
this.current.element.setAttributeNS(null, "fill-rule", "evenodd");
}
this.fill(); this.fill();
} }
@ -1557,9 +1555,7 @@ if (
} }
eoFillStroke() { eoFillStroke() {
if (this.current.element) { this.current.element?.setAttributeNS(null, "fill-rule", "evenodd");
this.current.element.setAttributeNS(null, "fill-rule", "evenodd");
}
this.fillStroke(); this.fillStroke();
} }

View File

@ -105,9 +105,7 @@ class PDFDataTransportStream {
} }
_onProgressiveDone() { _onProgressiveDone() {
if (this._fullRequestReader) { this._fullRequestReader?.progressiveDone();
this._fullRequestReader.progressiveDone();
}
this._progressiveDone = true; this._progressiveDone = true;
} }
@ -144,9 +142,8 @@ class PDFDataTransportStream {
} }
cancelAllRequests(reason) { cancelAllRequests(reason) {
if (this._fullRequestReader) { this._fullRequestReader?.cancel(reason);
this._fullRequestReader.cancel(reason);
}
for (const reader of this._rangeReaders.slice(0)) { for (const reader of this._rangeReaders.slice(0)) {
reader.cancel(reason); reader.cancel(reason);
} }

View File

@ -100,16 +100,12 @@ class App extends PDFObject {
const timeout = Object.create(null); const timeout = Object.create(null);
const id = { callbackId, interval }; const id = { callbackId, interval };
this._timeoutIds.set(timeout, id); this._timeoutIds.set(timeout, id);
if (this._timeoutIdsRegistry) { this._timeoutIdsRegistry?.register(timeout, id);
this._timeoutIdsRegistry.register(timeout, id);
}
return timeout; return timeout;
} }
_unregisterTimeout(timeout) { _unregisterTimeout(timeout) {
if (this._timeoutIdsRegistry) { this._timeoutIdsRegistry?.unregister(timeout);
this._timeoutIdsRegistry.unregister(timeout);
}
const data = this._timeoutIds.get(timeout); const data = this._timeoutIds.get(timeout);
if (!data) { if (!data) {

View File

@ -950,10 +950,9 @@ class Doc extends PDFObject {
} }
getPrintParams() { getPrintParams() {
if (!this._printParams) { return (this._printParams ||= new PrintParams({
this._printParams = new PrintParams({ lastPage: this._numPages - 1 }); lastPage: this._numPages - 1,
} }));
return this._printParams;
} }
getSound() { getSound() {

View File

@ -91,9 +91,7 @@ class EventDispatcher {
} else if (id === "app" && baseEvent.name === "ResetForm") { } else if (id === "app" && baseEvent.name === "ResetForm") {
for (const fieldId of baseEvent.ids) { for (const fieldId of baseEvent.ids) {
const obj = this._objects[fieldId]; const obj = this._objects[fieldId];
if (obj) { obj?.obj._reset();
obj.obj._reset();
}
} }
} }
return; return;

View File

@ -854,9 +854,7 @@ function unitTestPostHandler(req, res) {
var onCancel = null, var onCancel = null,
ttxTimeout = 10000; ttxTimeout = 10000;
var timeoutId = setTimeout(function () { var timeoutId = setTimeout(function () {
if (onCancel) { onCancel?.("TTX timeout");
onCancel("TTX timeout");
}
}, ttxTimeout); }, ttxTimeout);
translateFont( translateFont(
body, body,
@ -1002,9 +1000,7 @@ function startBrowsers(initSessionCallback, makeStartUrl = null) {
session.browserPromise = startBrowser(browserName, startUrl) session.browserPromise = startBrowser(browserName, startUrl)
.then(function (browser) { .then(function (browser) {
session.browser = browser; session.browser = browser;
if (initSessionCallback) { initSessionCallback?.(session);
initSessionCallback(session);
}
}) })
.catch(function (ex) { .catch(function (ex) {
console.log(`Error while starting ${browserName}: ${ex.message}`); console.log(`Error while starting ${browserName}: ${ex.message}`);
@ -1050,10 +1046,7 @@ async function closeSession(browser) {
const rimraf = require("rimraf"); const rimraf = require("rimraf");
rimraf.sync(tempDir); rimraf.sync(tempDir);
} }
onAllSessionsClosed?.();
if (onAllSessionsClosed) {
onAllSessionsClosed();
}
} }
} }
} }

View File

@ -637,12 +637,9 @@ class BaseViewer {
this._cancelRendering(); this._cancelRendering();
this._resetView(); this._resetView();
if (this.findController) { this.findController?.setDocument(null);
this.findController.setDocument(null); this._scriptingManager?.setDocument(null);
}
if (this._scriptingManager) {
this._scriptingManager.setDocument(null);
}
if (this.#annotationEditorUIManager) { if (this.#annotationEditorUIManager) {
this.#annotationEditorUIManager.destroy(); this.#annotationEditorUIManager.destroy();
this.#annotationEditorUIManager = null; this.#annotationEditorUIManager = null;
@ -807,12 +804,8 @@ class BaseViewer {
// starts to create the correct size canvas. Wait until one page is // starts to create the correct size canvas. Wait until one page is
// rendered so we don't tie up too many resources early on. // rendered so we don't tie up too many resources early on.
this.#onePageRenderedOrForceFetch().then(async () => { this.#onePageRenderedOrForceFetch().then(async () => {
if (this.findController) { this.findController?.setDocument(pdfDocument); // Enable searching.
this.findController.setDocument(pdfDocument); // Enable searching. this._scriptingManager?.setDocument(pdfDocument); // Enable scripting.
}
if (this._scriptingManager) {
this._scriptingManager.setDocument(pdfDocument); // Enable scripting.
}
if (this.#annotationEditorUIManager) { if (this.#annotationEditorUIManager) {
// Ensure that the Editor buttons, in the toolbar, are updated. // Ensure that the Editor buttons, in the toolbar, are updated.

View File

@ -45,9 +45,7 @@ const ChromeCom = {
}; };
if (!chrome.runtime) { if (!chrome.runtime) {
console.error("chrome.runtime is undefined."); console.error("chrome.runtime is undefined.");
if (callback) { callback?.();
callback();
}
} else if (callback) { } else if (callback) {
chrome.runtime.sendMessage(message, callback); chrome.runtime.sendMessage(message, callback);
} else { } else {

View File

@ -221,9 +221,7 @@ class PDFPageView {
destroy() { destroy() {
this.reset(); this.reset();
if (this.pdfPage) { this.pdfPage?.cleanup();
this.pdfPage.cleanup();
}
} }
/** /**

View File

@ -231,10 +231,7 @@ class PDFThumbnailViewer {
// Set the first `pdfPage` immediately, since it's already loaded, // Set the first `pdfPage` immediately, since it's already loaded,
// rather than having to repeat the `PDFDocumentProxy.getPage` call in // rather than having to repeat the `PDFDocumentProxy.getPage` call in
// the `this.#ensurePdfPageLoaded` method before rendering can start. // the `this.#ensurePdfPageLoaded` method before rendering can start.
const firstThumbnailView = this._thumbnails[0]; this._thumbnails[0]?.setPdfPage(firstPdfPage);
if (firstThumbnailView) {
firstThumbnailView.setPdfPage(firstPdfPage);
}
// Ensure that the current thumbnail is always highlighted on load. // Ensure that the current thumbnail is always highlighted on load.
const thumbnailView = this._thumbnails[this._currentPageNumber - 1]; const thumbnailView = this._thumbnails[this._currentPageNumber - 1];