| 8 | } |
| 9 | |
| 10 | export class ElementObserver { |
| 11 | element: Element |
| 12 | started: boolean |
| 13 | private delegate: ElementObserverDelegate |
| 14 | |
| 15 | private elements: Set<Element> |
| 16 | private mutationObserver: MutationObserver |
| 17 | private mutationObserverInit: MutationObserverInit = { attributes: true, childList: true, subtree: true } |
| 18 | |
| 19 | constructor(element: Element, delegate: ElementObserverDelegate) { |
| 20 | this.element = element |
| 21 | this.started = false |
| 22 | this.delegate = delegate |
| 23 | |
| 24 | this.elements = new Set() |
| 25 | this.mutationObserver = new MutationObserver((mutations) => this.processMutations(mutations)) |
| 26 | } |
| 27 | |
| 28 | start() { |
| 29 | if (!this.started) { |
| 30 | this.started = true |
| 31 | this.mutationObserver.observe(this.element, this.mutationObserverInit) |
| 32 | this.refresh() |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pause(callback: () => void) { |
| 37 | if (this.started) { |
| 38 | this.mutationObserver.disconnect() |
| 39 | this.started = false |
| 40 | } |
| 41 | |
| 42 | callback() |
| 43 | |
| 44 | if (!this.started) { |
| 45 | this.mutationObserver.observe(this.element, this.mutationObserverInit) |
| 46 | this.started = true |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | stop() { |
| 51 | if (this.started) { |
| 52 | this.mutationObserver.takeRecords() |
| 53 | this.mutationObserver.disconnect() |
| 54 | this.started = false |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | refresh() { |
| 59 | if (this.started) { |
| 60 | const matches = new Set(this.matchElementsInTree()) |
| 61 | |
| 62 | for (const element of Array.from(this.elements)) { |
| 63 | if (!matches.has(element)) { |
| 64 | this.removeElement(element) |
| 65 | } |
| 66 | } |
| 67 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…