@@ -21,7 +21,7 @@ const INITIAL_DATA = Symbol("INITIAL_DATA");
2121 * a worker. This class implements some basic methods to manage these objects.
2222 */
2323class PDFObjects {
24- #objs = Object . create ( null ) ;
24+ #objs = new Map ( ) ;
2525
2626 /**
2727 * Ensures there is an object defined for `objId`.
@@ -30,10 +30,10 @@ class PDFObjects {
3030 * @returns {Object }
3131 */
3232 #ensureObj( objId ) {
33- return ( this . #objs[ objId ] ||= {
33+ return this . #objs. getOrInsertComputed ( objId , ( ) => ( {
3434 ...Promise . withResolvers ( ) ,
3535 data : INITIAL_DATA ,
36- } ) ;
36+ } ) ) ;
3737 }
3838
3939 /**
@@ -58,7 +58,7 @@ class PDFObjects {
5858 }
5959 // If there isn't a callback, the user expects to get the resolved data
6060 // directly.
61- const obj = this . #objs[ objId ] ;
61+ const obj = this . #objs. get ( objId ) ;
6262 // If there isn't an object yet or the object isn't resolved, then the
6363 // data isn't ready yet!
6464 if ( ! obj || obj . data === INITIAL_DATA ) {
@@ -72,7 +72,7 @@ class PDFObjects {
7272 * @returns {boolean }
7373 */
7474 has ( objId ) {
75- const obj = this . #objs[ objId ] ;
75+ const obj = this . #objs. get ( objId ) ;
7676 return ! ! obj && obj . data !== INITIAL_DATA ;
7777 }
7878
@@ -81,12 +81,12 @@ class PDFObjects {
8181 * @returns {boolean }
8282 */
8383 delete ( objId ) {
84- const obj = this . #objs[ objId ] ;
84+ const obj = this . #objs. get ( objId ) ;
8585 if ( ! obj || obj . data === INITIAL_DATA ) {
8686 // Only allow removing the object *after* it's been resolved.
8787 return false ;
8888 }
89- delete this . #objs[ objId ] ;
89+ this . #objs. delete ( objId ) ;
9090 return true ;
9191 }
9292
@@ -103,21 +103,17 @@ class PDFObjects {
103103 }
104104
105105 clear ( ) {
106- for ( const objId in this . #objs) {
107- const { data } = this . #objs[ objId ] ;
106+ for ( const { data } of this . #objs. values ( ) ) {
108107 data ?. bitmap ?. close ( ) ; // Release any `ImageBitmap` data.
109108 }
110- this . #objs = Object . create ( null ) ;
109+ this . #objs. clear ( ) ;
111110 }
112111
113112 * [ Symbol . iterator ] ( ) {
114- for ( const objId in this . #objs) {
115- const { data } = this . #objs[ objId ] ;
116-
117- if ( data === INITIAL_DATA ) {
118- continue ;
113+ for ( const [ objId , { data } ] of this . #objs) {
114+ if ( data !== INITIAL_DATA ) {
115+ yield [ objId , data ] ;
119116 }
120- yield [ objId , data ] ;
121117 }
122118 }
123119}
0 commit comments