Skip to content

Commit 6f05231

Browse files
Merge pull request #19895 from Snuffleupagus/ObjectLoader-improve
Unify method return values in the `ObjectLoader` class, and simplify how the `ObjectLoader` is used
2 parents 04400c5 + 62009ff commit 6f05231

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

eslint.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ export default [
303303
selector: "NewExpression[callee.name='Name']",
304304
message: "Use `Name.get()` rather than `new Name()`.",
305305
},
306+
{
307+
selector: "NewExpression[callee.name='ObjectLoader']",
308+
message:
309+
"Use `ObjectLoader.load()` rather than `new ObjectLoader()`.",
310+
},
306311
{
307312
selector: "NewExpression[callee.name='Ref']",
308313
message: "Use `Ref.get()` rather than `new Ref()`.",

src/core/annotation.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,12 @@ class Annotation {
11581158
}
11591159
}
11601160

1161-
loadResources(keys, appearance) {
1162-
return appearance.dict.getAsync("Resources").then(resources => {
1163-
if (!resources) {
1164-
return undefined;
1165-
}
1166-
1167-
const objectLoader = new ObjectLoader(resources, keys, resources.xref);
1168-
return objectLoader.load().then(() => resources);
1169-
});
1161+
async loadResources(keys, appearance) {
1162+
const resources = await appearance.dict.getAsync("Resources");
1163+
if (resources) {
1164+
await ObjectLoader.load(resources, keys, resources.xref);
1165+
}
1166+
return resources;
11701167
}
11711168

11721169
async getOperatorList(evaluator, task, intent, annotationStorage) {

src/core/document.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ class Page {
417417
// TODO: add async `_getInheritableProperty` and remove this.
418418
await (this.resourcesPromise ??= this.pdfManager.ensure(this, "resources"));
419419

420-
const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
421-
await objectLoader.load();
420+
await ObjectLoader.load(this.resources, keys, this.xref);
422421
}
423422

424423
async #getMergedResources(streamDict, keys) {
@@ -430,8 +429,7 @@ class Page {
430429
if (!(localResources instanceof Dict && localResources.size)) {
431430
return this.resources;
432431
}
433-
const objectLoader = new ObjectLoader(localResources, keys, this.xref);
434-
await objectLoader.load();
432+
await ObjectLoader.load(localResources, keys, this.xref);
435433

436434
return Dict.merge({
437435
xref: this.xref,
@@ -1254,8 +1252,7 @@ class PDFDocument {
12541252
if (!(resources instanceof Dict)) {
12551253
return;
12561254
}
1257-
const objectLoader = new ObjectLoader(resources, ["Font"], this.xref);
1258-
await objectLoader.load();
1255+
await ObjectLoader.load(resources, ["Font"], this.xref);
12591256

12601257
const fontRes = resources.get("Font");
12611258
if (!(fontRes instanceof Dict)) {

src/core/object_loader.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,16 @@ function addChildren(node, nodesToVisit) {
5454
* entire PDF document object graph to be traversed.
5555
*/
5656
class ObjectLoader {
57+
refSet = new RefSet();
58+
5759
constructor(dict, keys, xref) {
5860
this.dict = dict;
5961
this.keys = keys;
6062
this.xref = xref;
61-
this.refSet = null;
6263
}
6364

6465
async load() {
65-
// Don't walk the graph if all the data is already loaded.
66-
if (this.xref.stream.isDataLoaded) {
67-
return undefined;
68-
}
69-
7066
const { keys, dict } = this;
71-
this.refSet = new RefSet();
7267
// Setup the initial nodes to visit.
7368
const nodesToVisit = [];
7469
for (const key of keys) {
@@ -78,10 +73,12 @@ class ObjectLoader {
7873
nodesToVisit.push(rawValue);
7974
}
8075
}
81-
return this._walk(nodesToVisit);
76+
await this.#walk(nodesToVisit);
77+
78+
this.refSet = null; // Everything is loaded, clear the cache.
8279
}
8380

84-
async _walk(nodesToVisit) {
81+
async #walk(nodesToVisit) {
8582
const nodesToRevisit = [];
8683
const pendingRequests = [];
8784
// DFS walk of the object graph.
@@ -99,11 +96,10 @@ class ObjectLoader {
9996
currentNode = this.xref.fetch(currentNode);
10097
} catch (ex) {
10198
if (!(ex instanceof MissingDataException)) {
102-
warn(`ObjectLoader._walk - requesting all data: "${ex}".`);
103-
this.refSet = null;
99+
warn(`ObjectLoader.#walk - requesting all data: "${ex}".`);
104100

105-
const { manager } = this.xref.stream;
106-
return manager.requestAllChunks();
101+
await this.xref.stream.manager.requestAllChunks();
102+
return;
107103
}
108104
nodesToRevisit.push(currentNode);
109105
pendingRequests.push({ begin: ex.begin, end: ex.end });
@@ -139,11 +135,18 @@ class ObjectLoader {
139135
this.refSet.remove(node);
140136
}
141137
}
142-
return this._walk(nodesToRevisit);
138+
await this.#walk(nodesToRevisit);
139+
}
140+
}
141+
142+
static async load(obj, keys, xref) {
143+
// Don't walk the graph if all the data is already loaded.
144+
if (xref.stream.isDataLoaded) {
145+
return;
143146
}
144-
// Everything is loaded.
145-
this.refSet = null;
146-
return undefined;
147+
// eslint-disable-next-line no-restricted-syntax
148+
const objLoader = new ObjectLoader(obj, keys, xref);
149+
await objLoader.load();
147150
}
148151
}
149152

0 commit comments

Comments
 (0)