MCPcopy Index your code
hub / github.com/angular/angular / observeSelectMutations

Function observeSelectMutations

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

Source from the content-addressed store, hash-verified

17 * @return The newly created `MutationObserver`.
18 */
19export 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>.

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…