Skip to content

Commit 779c753

Browse files
committed
transfer add logic for module, block, parent, chunk to prototype
bringing the "adder" logic to the prototype might be slighly better performance wise reuse of logic for add module and block, though they now have one superfluous check (should be negligable though)
1 parent 071b504 commit 779c753

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

lib/Chunk.js

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class Chunk {
2929
this.origins = [];
3030
this.files = [];
3131
this.rendered = false;
32-
this.addChunk = this.createAdder("chunks");
33-
this.addParent = this.createAdder("parents");
3432
if(module) {
3533
this.origins.push({
3634
module,
@@ -40,20 +38,6 @@ class Chunk {
4038
}
4139
}
4240

43-
createAdder(collection) {
44-
const createAdderCallback = (chunk) => {
45-
if(chunk === this) {
46-
return false;
47-
}
48-
if(this[collection].indexOf(chunk) >= 0) {
49-
return false;
50-
}
51-
this[collection].push(chunk);
52-
return true;
53-
};
54-
return createAdderCallback;
55-
}
56-
5741
get entry() {
5842
throw new Error("Chunk.entry was removed. Use hasRuntime()");
5943
}
@@ -83,14 +67,35 @@ class Chunk {
8367
return !!this.entryModule;
8468
}
8569

86-
addModule(module) {
87-
if(this.modules.indexOf(module) >= 0) {
70+
addToCollection(collection, item) {
71+
if(item === this) {
72+
return false;
73+
}
74+
75+
if(collection.indexOf(item) > -1) {
8876
return false;
8977
}
90-
this.modules.push(module);
78+
79+
collection.push(item);
9180
return true;
9281
}
9382

83+
addChunk(chunk) {
84+
return this.addToCollection(this.chunks, chunk);
85+
}
86+
87+
addParent(parent) {
88+
return this.addToCollection(this.parents, parent);
89+
}
90+
91+
addModule(module) {
92+
return this.addToCollection(this.modules, module);
93+
}
94+
95+
addBlock(block) {
96+
return this.addToCollection(this.blocks, block);
97+
}
98+
9499
removeModule(module) {
95100
removeAndDo(this.modules, module, "removeChunk", this);
96101
}
@@ -103,14 +108,6 @@ class Chunk {
103108
removeAndDo(this.parents, chunk, "removeChunk", this);
104109
}
105110

106-
addBlock(block) {
107-
if(this.blocks.indexOf(block) >= 0) {
108-
return false;
109-
}
110-
this.blocks.push(block);
111-
return true;
112-
}
113-
114111
addOrigin(module, loc) {
115112
this.origins.push({
116113
module,

0 commit comments

Comments
 (0)