Skip to content

Commit

Permalink
fix: 修复 useObserverAncestor 逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
XYShaoKang committed Mar 17, 2023
1 parent 0f077b3 commit deffc02
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/content/hooks/useObserverAncestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export const useObserverAncestor = (
})

const mount = useEvent(async () => {
let el = await onChange(state)
if (!el) return
const root = await onChange(state)
if (!root) return
let el = root
const { ancestors, ancestorSet, observers } = ancestorRef.current
const els: HTMLElement[] = []

Expand All @@ -38,21 +39,32 @@ export const useObserverAncestor = (
el = el.parentElement!
els.push(el)
}
const pop = () => {
const ancestor = ancestors.pop()!
ancestorSet.delete(ancestor)
observers.pop()!.disconnect()
return ancestor
}

while (ancestors.length) {
const ancestor = pop()
if (el === ancestor) break
}

//#endregion

// 添加新元素变化的部分祖先结点,并添加对应的 MutationObserver
// 要判断当前结点是否被删除,必须要将 MutationObserver 挂载到父元素上

while ((el = els.pop())) {
while (els.length) {
const el = els.pop()!
const observer = new MutationObserver(mutations => {
const checked = mutations.some(({ removedNodes }) =>
Array.prototype.some.call(removedNodes, node => node === el)
)
if (checked) {
while (els.length) {
const ancestor = ancestors.pop()!
ancestorSet.delete(ancestor)
observers.pop()!.disconnect()
while (ancestors.length) {
const ancestor = pop()
if (ancestor === el) break
}
mount()
Expand All @@ -62,17 +74,14 @@ export const useObserverAncestor = (
ancestorSet.add(el)
observers.push(observer)
observer.observe(el.parentElement!, { childList: true })
state.unmount.push(() => observer.disconnect())
}

state.unmount.push(() => {
while (observers.length) pop()
})
})

useEffect(() => {
mount()
return () => {
let observer
while ((observer = ancestorRef.current.observers.pop())) {
observer.disconnect()
}
}
}, [])
}

0 comments on commit deffc02

Please sign in to comment.