Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow detached autosize DOM elements to be garbage collected #410

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
39 changes: 36 additions & 3 deletions src/autosize.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
const assignedElements = new Map();
const modernEnv = typeof ResizeObserver === "function" && typeof WeakMap === "function";

const assignedElements = modernEnv ? new WeakMap() : new Map();

const resizeObserver = createResizeObserver();

function createResizeObserver() {
if (modernEnv) {
return new ResizeObserver((entries) => entries.forEach(e => onResize(e.target)));
}
// If not a modern environment, we use a Map instead of a WeakMap, which is iterable.
const resizeCallback = () => assignedElements.forEach((_, el) => onResize(el));
window.addEventListener('resize', resizeCallback);
return { observe: () => {}, unobserve: () => {}, disconnect: () => window.removeEventListener('resize', resizeCallback)};
}

function getRelevantStyles(c) {
return `${c.width}-${c.height}-${c.padding}-${c.borderWidth}-${c.overflow}-${c.boxSizing}-${c.textAlign}`;
}

function onResize(el) {
const instance = assignedElements.get(el);
if (instance !== undefined && el.scrollHeight > 0) {
const styles = getRelevantStyles(instance.computed);
if (styles !== instance.previousStyles) {
instance.update();
}
}
}

function assign(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || assignedElements.has(ta)) return;

let previousHeight = null;
let previousStyles = null;

function cacheScrollTops(el) {
const arr = [];
Expand Down Expand Up @@ -99,6 +128,8 @@ function assign(ta) {
testForHeightReduction: true,
});
}

previousStyles = getRelevantStyles(computed);
}

function fullSetHeight() {
Expand Down Expand Up @@ -127,9 +158,9 @@ function assign(ta) {
ta.removeEventListener('autosize:destroy', destroy);
ta.removeEventListener('autosize:update', fullSetHeight);
ta.removeEventListener('input', handleInput);
window.removeEventListener('resize', fullSetHeight); // future todo: consider replacing with ResizeObserver
Object.keys(style).forEach(key => ta.style[key] = style[key]);
assignedElements.delete(ta);
resizeObserver.unobserve(ta);
}).bind(ta, {
height: ta.style.height,
resize: ta.style.resize,
Expand All @@ -142,13 +173,15 @@ function assign(ta) {
ta.addEventListener('autosize:destroy', destroy);
ta.addEventListener('autosize:update', fullSetHeight);
ta.addEventListener('input', handleInput);
window.addEventListener('resize', fullSetHeight); // future todo: consider replacing with ResizeObserver
resizeObserver.observe(ta);
ta.style.overflowX = 'hidden';
ta.style.wordWrap = 'break-word';

assignedElements.set(ta, {
destroy,
update: fullSetHeight,
get previousStyles() { return previousStyles; },
computed,
});

fullSetHeight();
Expand Down