(index: number)
| 528 | } |
| 529 | |
| 530 | getElementAtIndex(index: number): Element | null { |
| 531 | if (index < 0 || index >= this.numElements) { |
| 532 | console.warn( |
| 533 | `Invalid index ${index} specified; store contains ${this.numElements} items.`, |
| 534 | ); |
| 535 | |
| 536 | return null; |
| 537 | } |
| 538 | |
| 539 | // Find which root this element is in... |
| 540 | let root; |
| 541 | let rootWeight = 0; |
| 542 | for (let i = 0; i < this._roots.length; i++) { |
| 543 | const rootID = this._roots[i]; |
| 544 | root = this._idToElement.get(rootID); |
| 545 | |
| 546 | if (root === undefined) { |
| 547 | this._throwAndEmitError( |
| 548 | Error( |
| 549 | `Couldn't find root with id "${rootID}": no matching node was found in the Store.`, |
| 550 | ), |
| 551 | ); |
| 552 | |
| 553 | return null; |
| 554 | } |
| 555 | |
| 556 | if (root.children.length === 0) { |
| 557 | continue; |
| 558 | } |
| 559 | |
| 560 | if (rootWeight + root.weight > index) { |
| 561 | break; |
| 562 | } else { |
| 563 | rootWeight += root.weight; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (root === undefined) { |
| 568 | return null; |
| 569 | } |
| 570 | |
| 571 | // Find the element in the tree using the weight of each node... |
| 572 | // Skip over the root itself, because roots aren't visible in the Elements tree. |
| 573 | let currentElement: Element = root; |
| 574 | let currentWeight = rootWeight - 1; |
| 575 | |
| 576 | while (index !== currentWeight) { |
| 577 | const numChildren = currentElement.children.length; |
| 578 | for (let i = 0; i < numChildren; i++) { |
| 579 | const childID = currentElement.children[i]; |
| 580 | const child = this._idToElement.get(childID); |
| 581 | |
| 582 | if (child === undefined) { |
| 583 | this._throwAndEmitError( |
| 584 | Error( |
| 585 | `Couldn't child element with id "${childID}": no matching node was found in the Store.`, |
| 586 | ), |
| 587 | ); |
no test coverage detected