From 7216511fbf16188bfa0a4fa13f7b217aec519656 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 17 Oct 2021 13:42:21 +0200 Subject: [PATCH] Convert `TempImageFactory` to a `class`, using static fields/methods Please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields --- web/pdf_thumbnail_view.js | 70 ++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js index 4b43d26f1..0df790e90 100644 --- a/web/pdf_thumbnail_view.js +++ b/web/pdf_thumbnail_view.js @@ -36,48 +36,42 @@ const THUMBNAIL_WIDTH = 98; // px * @property {IL10n} l10n - Localization service. */ -const TempImageFactory = (function TempImageFactoryClosure() { - let tempCanvasCache = null; +class TempImageFactory { + static #tempCanvas = null; - return { - getCanvas(width, height) { - let tempCanvas = tempCanvasCache; - if (!tempCanvas) { - tempCanvas = document.createElement("canvas"); - tempCanvasCache = tempCanvas; - } - tempCanvas.width = width; - tempCanvas.height = height; + static getCanvas(width, height) { + const tempCanvas = (this.#tempCanvas ||= document.createElement("canvas")); + tempCanvas.width = width; + tempCanvas.height = height; - // Since this is a temporary canvas, we need to fill it with a white - // background ourselves. `_getPageDrawContext` uses CSS rules for this. - if ( - typeof PDFJSDev === "undefined" || - PDFJSDev.test("MOZCENTRAL || GENERIC") - ) { - tempCanvas.mozOpaque = true; - } + // Since this is a temporary canvas, we need to fill it with a white + // background ourselves. `_getPageDrawContext` uses CSS rules for this. + if ( + typeof PDFJSDev === "undefined" || + PDFJSDev.test("MOZCENTRAL || GENERIC") + ) { + tempCanvas.mozOpaque = true; + } - const ctx = tempCanvas.getContext("2d", { alpha: false }); - ctx.save(); - ctx.fillStyle = "rgb(255, 255, 255)"; - ctx.fillRect(0, 0, width, height); - ctx.restore(); - return [tempCanvas, tempCanvas.getContext("2d")]; - }, + const ctx = tempCanvas.getContext("2d", { alpha: false }); + ctx.save(); + ctx.fillStyle = "rgb(255, 255, 255)"; + ctx.fillRect(0, 0, width, height); + ctx.restore(); + return [tempCanvas, tempCanvas.getContext("2d")]; + } - destroyCanvas() { - const tempCanvas = tempCanvasCache; - if (tempCanvas) { - // Zeroing the width and height causes Firefox to release graphics - // resources immediately, which can greatly reduce memory consumption. - tempCanvas.width = 0; - tempCanvas.height = 0; - } - tempCanvasCache = null; - }, - }; -})(); + static destroyCanvas() { + const tempCanvas = this.#tempCanvas; + if (tempCanvas) { + // Zeroing the width and height causes Firefox to release graphics + // resources immediately, which can greatly reduce memory consumption. + tempCanvas.width = 0; + tempCanvas.height = 0; + } + this.#tempCanvas = null; + } +} /** * @implements {IRenderableView}