aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Utility
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-02-01 07:55:46 -0800
committerFuwn <[email protected]>2024-02-01 07:55:46 -0800
commit541d2905ed7b86c7e6ec1fd77c19132e5d800fe1 (patch)
treea0741db07d144ff8109b12b0cf93b8427c4843e4 /src/lib/Utility
parentfeat(badges): use anime rate limit card (diff)
downloaddue.moe-541d2905ed7b86c7e6ec1fd77c19132e5d800fe1.tar.xz
due.moe-541d2905ed7b86c7e6ec1fd77c19132e5d800fe1.zip
fix(html): height observer memory leak
Diffstat (limited to 'src/lib/Utility')
-rw-r--r--src/lib/Utility/html.ts59
1 files changed, 33 insertions, 26 deletions
diff --git a/src/lib/Utility/html.ts b/src/lib/Utility/html.ts
index 8a0b7888..9dc16634 100644
--- a/src/lib/Utility/html.ts
+++ b/src/lib/Utility/html.ts
@@ -4,32 +4,39 @@ import { get } from 'svelte/store';
export const nbsp = (str: string) => str.replace(/ /g, '&nbsp;');
export const createHeightObserver = (details = true) => {
- const heightObserver = new ResizeObserver(() => {
- if (get(settings).displayLimitListHeight) {
- document.querySelectorAll('.list').forEach((list) => {
- const element = list as HTMLElement;
-
- element.style.height = 'auto';
-
- const elementBound = element.getBoundingClientRect();
- const height = window.innerHeight - elementBound.top - 2.5 * 16;
-
- if (elementBound.height > height) element.style.height = `${height}px`;
- });
+ document.querySelectorAll('.list').forEach((element) => {
+ if (
+ !(
+ element as unknown as {
+ dataset: { observed: string };
+ }
+ ).dataset.observed
+ ) {
+ new ResizeObserver((entries) => {
+ entries.forEach((entry) => {
+ const element = entry.target as HTMLElement;
+
+ if (get(settings).displayLimitListHeight) {
+ element.style.height = 'auto';
+
+ const elementBound = element.getBoundingClientRect();
+ const height = window.innerHeight - elementBound.top - 2.5 * 16;
+
+ if (elementBound.height > height) element.style.height = `${height}px`;
+ }
+ });
+ }).observe(element);
+
+ if (details)
+ new MutationObserver((mutations) => {
+ mutations.forEach((mutation) => {
+ const element = mutation.target as HTMLDetailsElement;
+
+ if (element.tagName === 'DETAILS' && !element.open) element.style.height = 'auto';
+ });
+ }).observe(element, { attributes: true });
+
+ element.setAttribute('data-observed', 'true');
}
});
- const collapseObserver = new MutationObserver(() => {
- document.querySelectorAll('.list').forEach((list) => {
- const element = list as HTMLDetailsElement;
-
- if (element.tagName === 'DETAILS' && !element.open) element.style.height = 'auto';
- });
- });
-
- document.querySelectorAll('.list').forEach((element) => heightObserver.observe(element));
-
- if (details)
- document
- .querySelectorAll('.list')
- .forEach((element) => collapseObserver.observe(element, { attributes: true }));
};