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
This commit is contained in:
Jonas Jenwald 2021-10-17 13:42:21 +02:00
parent 89785a23f3
commit 7216511fbf

View File

@ -36,16 +36,11 @@ 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;
}
static getCanvas(width, height) {
const tempCanvas = (this.#tempCanvas ||= document.createElement("canvas"));
tempCanvas.width = width;
tempCanvas.height = height;
@ -64,20 +59,19 @@ const TempImageFactory = (function TempImageFactoryClosure() {
ctx.fillRect(0, 0, width, height);
ctx.restore();
return [tempCanvas, tempCanvas.getContext("2d")];
},
}
destroyCanvas() {
const tempCanvas = tempCanvasCache;
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;
}
tempCanvasCache = null;
},
};
})();
this.#tempCanvas = null;
}
}
/**
* @implements {IRenderableView}