( select: HTMLSelectElement, onMutation: () => void, destroyRef: DestroyRef, )
| 17 | * @return The newly created `MutationObserver`. |
| 18 | */ |
| 19 | export function observeSelectMutations( |
| 20 | select: HTMLSelectElement, |
| 21 | onMutation: () => void, |
| 22 | destroyRef: DestroyRef, |
| 23 | ): void { |
| 24 | if (typeof MutationObserver !== 'function') { |
| 25 | // Observing mutations is best-effort. |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | const observer = new MutationObserver((mutations) => { |
| 30 | if (mutations.some((m) => isRelevantSelectMutation(m))) { |
| 31 | onMutation(); |
| 32 | } |
| 33 | }); |
| 34 | observer.observe(select, { |
| 35 | attributes: true, |
| 36 | attributeFilter: ['value'], |
| 37 | // We watch the character data, because an `<option>` with no explicit `value` property set uses |
| 38 | // its text content as its value. |
| 39 | // (See https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/value) |
| 40 | characterData: true, |
| 41 | childList: true, |
| 42 | subtree: true, |
| 43 | }); |
| 44 | destroyRef.onDestroy(() => observer.disconnect()); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Checks if a given mutation record is relevant for resyncing a <select>. |
no test coverage detected
searching dependent graphs…