@@ -88,7 +88,11 @@ class Chunk {
8888 }
8989
9090 addChunk ( chunk ) {
91- return this . addToCollection ( this . chunks , chunk ) ;
91+ if ( this . _chunks . has ( chunk ) ) {
92+ return false ;
93+ }
94+ this . _chunks . add ( chunk ) ;
95+ return true ;
9296 }
9397
9498 addParent ( parentChunk ) {
@@ -116,13 +120,13 @@ class Chunk {
116120 }
117121
118122 removeChunk ( chunk ) {
119- const idx = this . chunks . indexOf ( chunk ) ;
120- if ( idx >= 0 ) {
121- this . chunks . splice ( idx , 1 ) ;
122- chunk . removeParent ( this ) ;
123- return true ;
123+ if ( ! this . _chunks . has ( chunk ) ) {
124+ return false ;
124125 }
125- return false ;
126+
127+ this . _chunks . delete ( chunk ) ;
128+ chunk . removeParent ( this ) ;
129+ return true ;
126130 }
127131
128132 removeParent ( chunk ) {
@@ -208,13 +212,10 @@ class Chunk {
208212 // cleanup parents
209213 this . parents . forEach ( parentChunk => {
210214 // remove this chunk from its parents
211- const idx = parentChunk . chunks . indexOf ( this ) ;
212- if ( idx >= 0 ) {
213- parentChunk . chunks . splice ( idx , 1 ) ;
214- }
215+ parentChunk . _chunks . delete ( this ) ;
215216
216217 // cleanup "sub chunks"
217- this . chunks . forEach ( chunk => {
218+ this . _chunks . forEach ( chunk => {
218219 /**
219220 * remove this chunk as "intermediary" and connect
220221 * it "sub chunks" and parents directly
@@ -232,7 +233,7 @@ class Chunk {
232233 * This can not be done in the above loop
233234 * as it is not garuanteed that `this.parents` contains anything.
234235 */
235- this . chunks . forEach ( chunk => {
236+ this . _chunks . forEach ( chunk => {
236237 // remove this as parent of every "sub chunk"
237238 const idx = chunk . parents . indexOf ( this ) ;
238239 if ( idx >= 0 ) {
@@ -261,10 +262,7 @@ class Chunk {
261262 }
262263
263264 replaceChunk ( oldChunk , newChunk ) {
264- const idx = this . chunks . indexOf ( oldChunk ) ;
265- if ( idx >= 0 ) {
266- this . chunks . splice ( idx , 1 ) ;
267- }
265+ this . _chunks . delete ( oldChunk ) ;
268266 if ( this !== newChunk && newChunk . addParent ( this ) ) {
269267 this . addChunk ( newChunk ) ;
270268 }
@@ -293,8 +291,8 @@ class Chunk {
293291 otherChunk . parents . forEach ( parentChunk => parentChunk . replaceChunk ( otherChunk , this ) ) ;
294292 otherChunk . parents . length = 0 ;
295293
296- otherChunk . chunks . forEach ( chunk => chunk . replaceParentChunk ( otherChunk , this ) ) ;
297- otherChunk . chunks . length = 0 ;
294+ otherChunk . _chunks . forEach ( chunk => chunk . replaceParentChunk ( otherChunk , this ) ) ;
295+ otherChunk . _chunks . clear ( ) ;
298296
299297 otherChunk . blocks . forEach ( b => {
300298 b . chunks = b . chunks ? b . chunks . map ( c => {
@@ -315,9 +313,8 @@ class Chunk {
315313 origin . reasons . unshift ( reason ) ;
316314 }
317315 } ) ;
318- this . chunks = this . chunks . filter ( chunk => {
319- return chunk !== otherChunk && chunk !== this ;
320- } ) ;
316+ this . _chunks . delete ( otherChunk ) ;
317+ this . _chunks . delete ( this ) ;
321318 this . parents = this . parents . filter ( parentChunk => {
322319 return parentChunk !== otherChunk && parentChunk !== this ;
323320 } ) ;
@@ -329,12 +326,12 @@ class Chunk {
329326 newChunk . blocks . push ( block ) ;
330327 block . chunks . push ( newChunk ) ;
331328 } ) ;
332- this . chunks . forEach ( chunk => {
333- newChunk . chunks . push ( chunk ) ;
329+ this . _chunks . forEach ( chunk => {
330+ newChunk . addChunk ( chunk ) ;
334331 chunk . parents . push ( newChunk ) ;
335332 } ) ;
336333 this . parents . forEach ( parentChunk => {
337- parentChunk . chunks . push ( newChunk ) ;
334+ parentChunk . addChunk ( newChunk ) ;
338335 newChunk . parents . push ( parentChunk ) ;
339336 } ) ;
340337 this . entrypoints . forEach ( entrypoint => {
@@ -413,7 +410,7 @@ class Chunk {
413410 if ( chunk . name )
414411 chunkNameMap [ chunk . id ] = chunk . name ;
415412 }
416- chunk . chunks . forEach ( addChunk ) ;
413+ chunk . _chunks . forEach ( addChunk ) ;
417414 } ( this ) ) ;
418415 return {
419416 hash : chunkHashMap ,
@@ -435,7 +432,7 @@ class Chunk {
435432 origin . reasons . sort ( ) ;
436433 } ) ;
437434 this . parents . sort ( sortById ) ;
438- this . chunks . sort ( sortById ) ;
435+ this . _chunks = new Set ( Array . from ( this . _chunks ) . sort ( sortById ) ) ;
439436 }
440437
441438 toString ( ) {
@@ -444,16 +441,16 @@ class Chunk {
444441
445442 checkConstraints ( ) {
446443 const chunk = this ;
447- chunk . chunks . forEach ( ( child , idx ) => {
448- if ( chunk . chunks . indexOf ( child ) !== idx )
444+ chunk . _chunks . forEach ( ( child , idx ) => {
445+ if ( Array . from ( chunk . _chunks ) . indexOf ( child ) !== idx )
449446 throw new Error ( `checkConstraints: duplicate child in chunk ${ chunk . debugId } ${ child . debugId } ` ) ;
450447 if ( child . parents . indexOf ( chunk ) < 0 )
451448 throw new Error ( `checkConstraints: child missing parent ${ chunk . debugId } -> ${ child . debugId } ` ) ;
452449 } ) ;
453450 chunk . parents . forEach ( ( parentChunk , idx ) => {
454451 if ( chunk . parents . indexOf ( parentChunk ) !== idx )
455452 throw new Error ( `checkConstraints: duplicate parent in chunk ${ chunk . debugId } ${ parentChunk . debugId } ` ) ;
456- if ( parentChunk . chunks . indexOf ( chunk ) < 0 )
453+ if ( parentChunk . _chunks . indexOf ( chunk ) < 0 )
457454 throw new Error ( `checkConstraints: parent missing child ${ parentChunk . debugId } <- ${ chunk . debugId } ` ) ;
458455 } ) ;
459456 }
0 commit comments