@@ -54,21 +54,16 @@ function addChildren(node, nodesToVisit) {
5454 * entire PDF document object graph to be traversed.
5555 */
5656class 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