Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/ext/hx-live.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@

const OBSERVE_OPTIONS = { childList: true, subtree: true, attributes: true, characterData: true };

let inputDebounceId = null;
const INPUT_DEBOUNCE_MS = htmx.config.live?.inputDebounceMs ?? 100;

function ensureActive() {
if (observer) return;
recomputeBound = () => schedule();
document.addEventListener('input', recomputeBound, true);
document.addEventListener('input', () => {
clearTimeout(inputDebounceId);
inputDebounceId = setTimeout(schedule, INPUT_DEBOUNCE_MS);
}, true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this listener be captured and removed in deactivate() like the recomputeBound listener is?

document.addEventListener('change', recomputeBound, true);
observer = new MutationObserver(recomputeBound);
observer.observe(document.documentElement, OBSERVE_OPTIONS);
}

function deactivate() {
if (!observer) return;
clearTimeout(inputDebounceId);
inputDebounceId = null;
document.removeEventListener('input', recomputeBound, true);
document.removeEventListener('change', recomputeBound, true);
observer.disconnect();
Expand Down Expand Up @@ -595,10 +603,20 @@
}

function writeAttrBinding(elt, attrName, value) {
if (attrName === 'text') { elt.textContent = value == null ? '' : String(value); return; }
if (attrName === 'html') { elt.innerHTML = value == null ? '' : String(value); return; }
if (attrName === 'text') {
let s = value == null ? '' : String(value);
if (elt.textContent !== s) elt.textContent = s;
return;
}
if (attrName === 'html') {
let s = value == null ? '' : String(value);
if (elt.innerHTML !== s) elt.innerHTML = s;
return;
}
if (attrName === 'style') { applyStyleBinding(elt, value); return; }
// Everything else (class, .class, aria-*, boolean, property-sync, regular) → applyAttr.
// Always write aria-* and property-backed attrs (getter type differs from setter).
// For everything else skip if unchanged.
if (!attrName.startsWith('aria-') && !PROPERTY_ATTRS.has(attrName) && applyAttr([elt], attrName) === value) return;
applyAttr([elt], attrName, value);
}

Expand Down
Loading