handleMutations function

Last updated: 2026-03-04T23:21:38.393Z

Metrics

LOC: 65 Complexity: 24 Params: 1

Signature

handleMutations(mutations: MutationRecord[]): : void

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'handleMutations' has cyclomatic complexity 24 (max 10)

Source Code

function handleMutations(mutations: MutationRecord[]): void {
  if (isFillingInProgress) return;

  const isRelevant = mutations.some((m) => {
    if (
      m.type === "childList" &&
      (m.addedNodes.length > 0 || m.removedNodes.length > 0)
    ) {
      // When Shadow DOM is enabled, watch newly attached shadow hosts
      if (activeConfig.shadowDOM) {
        for (const node of m.addedNodes) {
          if (node instanceof HTMLElement && node.shadowRoot) {
            observeSingleShadowRoot(node.shadowRoot);
          }
        }
      }
      return true;
    }
    if (m.type === "attributes") {
      const target = m.target as HTMLElement;
      if (target.id === "fill-all-notification") return false;
      if (
        m.attributeName === "disabled" ||
        m.attributeName === "hidden" ||
        m.attributeName === "style" ||
        m.attributeName === "class"
      ) {
        return hasFormContent(target);
      }
    }
    return false;
  });

  if (!isRelevant) return;

  if (debounceTimer) clearTimeout(debounceTimer);
  debounceTimer = setTimeout(async () => {
    const newSignature = getCurrentFieldSignature();
    if (newSignature !== lastFieldSignature) {
      const previousSignature = lastFieldSignature;
      const oldCount = previousSignature.split("|").filter(Boolean).length;
      const newCount = newSignature.split("|").filter(Boolean).length;
      const diff = newCount - oldCount;

      lastFieldSignature = newSignature;

      if (diff > 0) {
        log.info(`Detected ${diff} new form field(s)`);

        if (onNewFieldsCallback) {
          onNewFieldsCallback(diff);
        }

        if (activeConfig.autoRefill) {
          await refillNewFields(previousSignature);
        }
      } else if (diff !== 0) {
        log.info(`Form structure changed (${diff} fields)`);
        if (onNewFieldsCallback) {
          onNewFieldsCallback(diff);
        }
      }
    }
  }, activeConfig.debounceMs);
}

Dependencies (Outgoing)

graph LR handleMutations["handleMutations"] observeSingleShadowRoot["observeSingleShadowRoot"] hasFormContent["hasFormContent"] getCurrentFieldSignature["getCurrentFieldSignature"] refillNewFields["refillNewFields"] handleMutations -->|calls| observeSingleShadowRoot handleMutations -->|calls| hasFormContent handleMutations -->|calls| getCurrentFieldSignature handleMutations -->|calls| refillNewFields style handleMutations fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handleMutations "b707db53be39a752.html" click observeSingleShadowRoot "0945931e53402ca4.html" click hasFormContent "a70353de2f735ffb.html" click getCurrentFieldSignature "e72508d8f19c287a.html" click refillNewFields "07b7f93b13ced4af.html"

No incoming dependencies.