* Inserts a chunk directly before another chunk that already belongs to the * group, preserving the rest of the ordering. * @param {Chunk} chunk Chunk being inserted * @param {Chunk} before Placeholder/target chunk marking new chunk insertion point * @returns {boolean} return true if inserti
(chunk, before)
| 221 | * @returns {boolean} return true if insertion was successful |
| 222 | */ |
| 223 | insertChunk(chunk, before) { |
| 224 | const oldIdx = this.chunks.indexOf(chunk); |
| 225 | const idx = this.chunks.indexOf(before); |
| 226 | if (idx < 0) { |
| 227 | throw new Error("before chunk not found"); |
| 228 | } |
| 229 | if (oldIdx >= 0 && oldIdx > idx) { |
| 230 | this.chunks.splice(oldIdx, 1); |
| 231 | this.chunks.splice(idx, 0, chunk); |
| 232 | } else if (oldIdx < 0) { |
| 233 | this.chunks.splice(idx, 0, chunk); |
| 234 | return true; |
| 235 | } |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Appends a chunk to the group when it is not already a member. |