-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
}, Math.round(2000 * Math.random())) | ||
} | ||
|
||
|
@@ -745,7 +743,7 @@ function deletePosition( | |
offsetParent && | ||
(getComputedStyle(offsetParent).position === "static" || | ||
offsetParent instanceof HTMLBodyElement) | ||
) { | ||
) { | ||
offsetParent = offsetParent.parentElement | ||
} | ||
if (!offsetParent) offsetParent = document.body | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
intervalIds.forEach(id => clearInterval(id)); | ||
intervalIds = []; | ||
Comment on lines
+844
to
+845
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
}) | ||
} | ||
|
@@ -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: () => ({}), | ||
|
There was a problem hiding this comment.
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.