Skip to content

Commit 87c1231

Browse files
Merge pull request #20896 from Snuffleupagus/CachedCanvases-Map
Re-factor the `CachedCanvases` class to use a `Map` internally
2 parents d38cddf + 2cc5327 commit 87c1231

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

src/display/canvas.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,32 @@ function mirrorContextOperations(ctx, destCtx) {
205205
}
206206

207207
class CachedCanvases {
208+
#cache = new Map();
209+
208210
constructor(canvasFactory) {
209211
this.canvasFactory = canvasFactory;
210-
this.cache = Object.create(null);
211212
}
212213

213214
getCanvas(id, width, height) {
214-
let canvasEntry;
215-
if (this.cache[id] !== undefined) {
216-
canvasEntry = this.cache[id];
215+
let canvasEntry = this.#cache.get(id);
216+
if (canvasEntry) {
217217
this.canvasFactory.reset(canvasEntry, width, height);
218218
} else {
219219
canvasEntry = this.canvasFactory.create(width, height);
220-
this.cache[id] = canvasEntry;
220+
this.#cache.set(id, canvasEntry);
221221
}
222222
return canvasEntry;
223223
}
224224

225225
delete(id) {
226-
delete this.cache[id];
226+
this.#cache.delete(id);
227227
}
228228

229229
clear() {
230-
for (const id in this.cache) {
231-
const canvasEntry = this.cache[id];
230+
for (const canvasEntry of this.#cache.values()) {
232231
this.canvasFactory.destroy(canvasEntry);
233-
delete this.cache[id];
234232
}
233+
this.#cache.clear();
235234
}
236235
}
237236

0 commit comments

Comments
 (0)