1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Add general iteration support in the RefSet and RefSetCache classes

This patch removes the existing `forEach` methods, in favor of making the classes properly iterable instead. Given that the classes are using a `Set` respectively a `Map` internally, implementing this is very easy/efficient and allows us to simplify some existing code.
This commit is contained in:
Jonas Jenwald 2022-03-18 14:18:03 +01:00
parent 489e9ff7d3
commit c0736647f9
5 changed files with 54 additions and 60 deletions

View file

@ -1040,42 +1040,32 @@ class Catalog {
return shadow(this, "jsActions", actions);
}
fontFallback(id, handler) {
const promises = [];
this.fontCache.forEach(function (promise) {
promises.push(promise);
});
async fontFallback(id, handler) {
const translatedFonts = await Promise.all(this.fontCache);
return Promise.all(promises).then(translatedFonts => {
for (const translatedFont of translatedFonts) {
if (translatedFont.loadedName === id) {
translatedFont.fallback(handler);
return;
}
for (const translatedFont of translatedFonts) {
if (translatedFont.loadedName === id) {
translatedFont.fallback(handler);
return;
}
});
}
}
cleanup(manuallyTriggered = false) {
async cleanup(manuallyTriggered = false) {
clearGlobalCaches();
this.globalImageCache.clear(/* onlyData = */ manuallyTriggered);
this.pageKidsCountCache.clear();
this.pageIndexCache.clear();
this.nonBlendModesSet.clear();
const promises = [];
this.fontCache.forEach(function (promise) {
promises.push(promise);
});
const translatedFonts = await Promise.all(this.fontCache);
return Promise.all(promises).then(translatedFonts => {
for (const { dict } of translatedFonts) {
delete dict.cacheKey;
}
this.fontCache.clear();
this.builtInCMapCache.clear();
this.standardFontDataCache.clear();
});
for (const { dict } of translatedFonts) {
delete dict.cacheKey;
}
this.fontCache.clear();
this.builtInCMapCache.clear();
this.standardFontDataCache.clear();
}
async getPageDict(pageIndex) {

View file

@ -352,9 +352,9 @@ class PartialEvaluator {
// When no blend modes exist, there's no need re-fetch/re-parse any of the
// processed `Ref`s again for subsequent pages. This helps reduce redundant
// `XRef.fetch` calls for some documents (e.g. issue6961.pdf).
processed.forEach(ref => {
for (const ref of processed) {
nonBlendModesSet.put(ref);
});
}
return false;
}

View file

@ -179,9 +179,9 @@ class GlobalImageCache {
get _byteSize() {
let byteSize = 0;
this._imageCache.forEach(imageData => {
for (const imageData of this._imageCache) {
byteSize += imageData.byteSize;
});
}
return byteSize;
}

View file

@ -350,10 +350,8 @@ class RefSet {
this._set.delete(ref.toString());
}
forEach(callback) {
for (const ref of this._set.values()) {
callback(ref);
}
[Symbol.iterator]() {
return this._set.values();
}
clear() {
@ -386,10 +384,8 @@ class RefSetCache {
this._map.set(ref.toString(), this.get(aliasRef));
}
forEach(callback) {
for (const value of this._map.values()) {
callback(value);
}
[Symbol.iterator]() {
return this._map.values();
}
clear() {