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

feat: destroy, unmount #198

Open
wants to merge 1 commit 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
30 changes: 23 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,10 @@ function updateAllPos() {
* random offset.
* @param el - Element
*/
let intervalIds: NodeJS.Timeout[] = [];
function poll(el: Element) {
setTimeout(() => {
intervals.set(
el,
setInterval(() => lowPriority(updatePos.bind(null, el)), 2000)
)
intervalIds.push(setInterval(() => lowPriority(updatePos.bind(null, el)), 2000));
Copy link
Member

Choose a reason for hiding this comment

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

These intervals are not scoped to the current element. This means if any unmount is called all the intervals for any animations on the page will be cleared. We should continue to use the intervals WeakSet.

}, Math.round(2000 * Math.random()))
}

Expand Down Expand Up @@ -745,7 +743,7 @@ function deletePosition(
offsetParent &&
(getComputedStyle(offsetParent).position === "static" ||
offsetParent instanceof HTMLBodyElement)
) {
) {
offsetParent = offsetParent.parentElement
}
if (!offsetParent) offsetParent = document.body
Expand Down Expand Up @@ -839,6 +837,13 @@ export default function autoAnimate(
disable: () => {
enabled.delete(el)
},
destroy() {
parents.delete(this.parent);
mutations?.disconnect();
resize?.disconnect();
Comment on lines +841 to +843
Copy link
Member

Choose a reason for hiding this comment

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

We don’t want to disconnect the mutation observer or the resize observer. These are global instantiations (1 per load). Destroying them would destroy all other observers on the page. parents is the only non-weak set/map here so we should keep that one.

intervalIds.forEach(id => clearInterval(id));
intervalIds = [];
Comment on lines +844 to +845
Copy link
Member

Choose a reason for hiding this comment

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

These need to be scoped to the current parent using the intervals weakset.

},
isEnabled: () => enabled.has(el),
})
}
Expand All @@ -850,10 +855,21 @@ export const vAutoAnimate = {
mounted: (
el: HTMLElement,
binding: {
value: Partial<AutoAnimateOptions> | AutoAnimationPlugin | undefined
value: Partial<AutoAnimateOptions> | AutoAnimationPlugin | undefined,
instance: {
animate: AnimationController,
}
}
) => {
autoAnimate(el, binding.value || {})
binding.instance.animate = autoAnimate(el, binding.value || {})
},
unmounted(el: HTMLElement, binding: {
value: Partial<AutoAnimateOptions> | AutoAnimationPlugin | undefined,
instance: {
animate: AnimationController,
}
}) {
binding.instance.animate.destroy!()
},
// ignore ssr see #96:
getSSRProps: () => ({}),
Expand Down