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