Skip to content

Commit

Permalink
fix: cleanup main observor effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Mar 3, 2024
1 parent d253114 commit 91f051e
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/lib/InfiniteLoader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
</script>

<script lang="ts">
import { onDestroy } from "svelte"
type InfiniteLoaderProps = {
triggerLoad: () => Promise<void>
loopTimeout?: number
Expand Down Expand Up @@ -95,26 +97,24 @@
}
}
// TODO: Observor not starting observation without
// some sort of action after 'observer.observe()'.
// 'return' unnecessary/wrong here in $effect
// @ts-expect-error
$effect(() => {
if (!observer) {
if (intersectionTarget) {
observer = new IntersectionObserver(
(entries) => {
if (entries[0]?.isIntersecting) {
attemptLoad()
}
},
{ rootMargin: "100px 0px 0px 0px" }
)
observer.observe(intersectionTarget)
return observer
}
if (observer || !intersectionTarget) return
observer = new IntersectionObserver(
(entries) => {
if (entries[0]?.isIntersecting) {
attemptLoad()
}
},
{ rootMargin: "100px 0px 0px 0px" }
)
observer.observe(intersectionTarget)
})
onDestroy(() => {
if (observer) {
observer.disconnect()
}
return () => observer?.disconnect()
})
</script>

Expand Down Expand Up @@ -156,7 +156,6 @@
display: grid;
width: 100%;
place-items: center;
margin-block: 2rem;
.loading {
margin-top: 1rem;
Expand Down Expand Up @@ -198,7 +197,7 @@
}
.target {
height: 4rem;
height: 1rem;
}
}
</style>

0 comments on commit 91f051e

Please sign in to comment.