MCPcopy Create free account
hub / github.com/angular/angular / observeSelectMutations

Function observeSelectMutations

packages/forms/signals/src/directive/select.ts:18–44  ·  view source on GitHub ↗
(
  select: HTMLSelectElement,
  onMutation: () => void,
  destroyRef: DestroyRef,
)

Source from the content-addressed store, hash-verified

16 * @param destroyRef `DestroyRef` whose destruction disconnects the observer.
17 */
18export 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>`.

Callers 1

nativeControlCreateFunction · 0.90

Calls 5

isRelevantSelectMutationFunction · 0.85
someMethod · 0.80
onDestroyMethod · 0.65
disconnectMethod · 0.65
observeMethod · 0.45

Tested by

no test coverage detected