Skip to content

Commit f4d8c5f

Browse files
committed
fix inner loop of "remove" method
as it is not guaranteed that the the chunk has parents the loop may not be run this could lead to stale "this" still being a parent of one of its childs. therefore we have to loop the chunks again.
1 parent 99d7cb8 commit f4d8c5f

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

lib/Chunk.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,23 @@ class Chunk {
157157
chunk.addParent(parentChunk);
158158
// add "sub chunk" to parent
159159
parentChunk.addChunk(chunk);
160-
161-
// remove this as parent of every "sub chunk"
162-
const idx = chunk.parents.indexOf(this);
163-
if(idx >= 0) {
164-
chunk.parents.splice(idx, 1);
165-
}
166160
});
167161
});
168162

163+
/**
164+
* we need to iterate again over the chunks
165+
* to remove this from the chunks parents.
166+
* This can not be done in the above loop
167+
* as it is not garuanteed that `this.parents` contains anything.
168+
*/
169+
this.chunks.forEach(chunk => {
170+
// remove this as parent of every "sub chunk"
171+
const idx = chunk.parents.indexOf(this);
172+
if(idx >= 0) {
173+
chunk.parents.splice(idx, 1);
174+
}
175+
});
176+
169177
// cleanup blocks
170178
this.blocks.forEach(block => {
171179
const idx = block.chunks.indexOf(this);

0 commit comments

Comments
 (0)