Skip to content

Commit 95a74e5

Browse files
committed
feat: improve resize observer handling with validation and listener management
1 parent 3635d57 commit 95a74e5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/preview/markmap-init.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ import { debounce } from "radashi"
44

55
const resize = {
66
event: new Event('resize'),
7-
observer: new ResizeObserver((entries) =>
8-
entries.forEach((entry) => entry.target.dispatchEvent(resize.event))
9-
),
7+
observer: new ResizeObserver((entries) => {
8+
entries.forEach((entry) => {
9+
entry.target.dispatchEvent(resize.event)
10+
})
11+
}),
12+
listeners: new WeakMap<Element, (event: Event) => void>(),
1013
observe: (el: Element, func: () => void) => {
11-
resize.observer.observe(el)
12-
el.addEventListener('resize', func)
14+
if (!(el instanceof Element) || typeof func !== "function") return
15+
16+
if (!resize.listeners.has(el)) {
17+
resize.listeners.set(el, func)
18+
el.addEventListener("resize", func)
19+
resize.observer.observe(el)
20+
}
1321
},
1422
}
1523

0 commit comments

Comments
 (0)