| 38 | } |
| 39 | |
| 40 | startObserving(element: HTMLElement) { |
| 41 | this.stopObserving(); |
| 42 | this._observer = new MutationObserver(mutations => { |
| 43 | const hasStructuralChange = mutations.some(m => m.addedNodes.length || m.removedNodes.length); |
| 44 | |
| 45 | if (hasStructuralChange) { |
| 46 | const newItems = this._getSortedItems(); |
| 47 | const currentItems = this._sortedItems(); |
| 48 | const hasChanged = |
| 49 | newItems.length !== currentItems.length || |
| 50 | newItems.some((item, index) => currentItems[index] !== item); |
| 51 | |
| 52 | if (hasChanged) { |
| 53 | this._sortedItems.set(newItems); |
| 54 | } |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | this._observer.observe(element, {childList: true, subtree: true, attributes: false}); |
| 59 | } |
| 60 | |
| 61 | stopObserving() { |
| 62 | this._observer?.disconnect(); |