mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-25 08:27:19 +02:00
Add a getOrPutComputed method in the RefSetCache class
This is equivalent to the native `Map.prototype.getOrInsertComputed()` method, and it helps simplify/shorten some existing code.
This commit is contained in:
parent
c661ba1408
commit
a18b581f00
@ -455,12 +455,7 @@ class PDFEditor {
|
|||||||
// Re-entry means a (malformed) cycle back to this stream: allocate its
|
// Re-entry means a (malformed) cycle back to this stream: allocate its
|
||||||
// reference now to break the loop, like the generic path's eager alloc.
|
// reference now to break the loop, like the generic path's eager alloc.
|
||||||
if (resourceStreamPath.has(oldRef)) {
|
if (resourceStreamPath.has(oldRef)) {
|
||||||
let ref = oldRefMapping.get(oldRef);
|
return oldRefMapping.getOrPutComputed(oldRef, () => this.newRef);
|
||||||
if (!ref) {
|
|
||||||
ref = this.newRef;
|
|
||||||
oldRefMapping.put(oldRef, ref);
|
|
||||||
}
|
|
||||||
return ref;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = oldRef.toString();
|
const key = oldRef.toString();
|
||||||
|
|||||||
@ -228,11 +228,7 @@ class GlobalImageCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shouldCache(ref, pageIndex) {
|
shouldCache(ref, pageIndex) {
|
||||||
let pageIndexSet = this._refCache.get(ref);
|
const pageIndexSet = this._refCache.getOrPutComputed(ref, () => new Set());
|
||||||
if (!pageIndexSet) {
|
|
||||||
pageIndexSet = new Set();
|
|
||||||
this._refCache.put(ref, pageIndexSet);
|
|
||||||
}
|
|
||||||
pageIndexSet.add(pageIndex);
|
pageIndexSet.add(pageIndex);
|
||||||
|
|
||||||
if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
|
if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {
|
||||||
|
|||||||
@ -398,6 +398,16 @@ class RefSetCache {
|
|||||||
this._map.set(ref.toString(), this.get(aliasRef));
|
this._map.set(ref.toString(), this.get(aliasRef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOrPutComputed(ref, callback) {
|
||||||
|
const map = this._map,
|
||||||
|
refStr = ref.toString();
|
||||||
|
|
||||||
|
if (!map.has(refStr)) {
|
||||||
|
map.set(refStr, callback(ref));
|
||||||
|
}
|
||||||
|
return map.get(refStr);
|
||||||
|
}
|
||||||
|
|
||||||
[Symbol.iterator]() {
|
[Symbol.iterator]() {
|
||||||
return this._map.values();
|
return this._map.values();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -93,12 +93,7 @@ class StructTreeRoot {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.structParentIds ||= new RefSetCache();
|
this.structParentIds ||= new RefSetCache();
|
||||||
let ids = this.structParentIds.get(pageRef);
|
this.structParentIds.getOrPutComputed(pageRef, makeArr).push([id, type]);
|
||||||
if (!ids) {
|
|
||||||
ids = [];
|
|
||||||
this.structParentIds.put(pageRef, ids);
|
|
||||||
}
|
|
||||||
ids.push([id, type]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addAnnotationIdToPage(pageRef, id) {
|
addAnnotationIdToPage(pageRef, id) {
|
||||||
@ -537,11 +532,9 @@ class StructTreeRoot {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let cachedParentDict = cache.get(parentRef);
|
const cachedParentDict = cache.getOrPutComputed(parentRef, () =>
|
||||||
if (!cachedParentDict) {
|
parentDict.clone()
|
||||||
cachedParentDict = parentDict.clone();
|
);
|
||||||
cache.put(parentRef, cachedParentDict);
|
|
||||||
}
|
|
||||||
const parentKidsRaw = cachedParentDict.getRaw("K");
|
const parentKidsRaw = cachedParentDict.getRaw("K");
|
||||||
let cachedParentKids =
|
let cachedParentKids =
|
||||||
parentKidsRaw instanceof Ref ? cache.get(parentKidsRaw) : null;
|
parentKidsRaw instanceof Ref ? cache.get(parentKidsRaw) : null;
|
||||||
|
|||||||
@ -568,6 +568,12 @@ describe("primitives", function () {
|
|||||||
cache.put(ref2, obj2);
|
cache.put(ref2, obj2);
|
||||||
expect([...cache.keys()]).toEqual([ref1, ref2]);
|
expect([...cache.keys()]).toEqual([ref1, ref2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle getOrPutComputed correctly", function () {
|
||||||
|
expect(cache.getOrPutComputed(ref1, () => obj1)).toEqual(obj1);
|
||||||
|
// Trying to set it again should be ignored.
|
||||||
|
expect(cache.getOrPutComputed(ref1, () => obj2)).toEqual(obj1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("isName", function () {
|
describe("isName", function () {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user