Rewrite async calls in onMount
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
import { onMount, tick } from 'svelte'
|
import { onDestroy, onMount, tick } from 'svelte'
|
||||||
import { createStore } from '../../store'
|
import { createStore } from '../../store'
|
||||||
import Dots from '../Dots/Dots.svelte'
|
import Dots from '../Dots/Dots.svelte'
|
||||||
import Arrow from '../Arrow/Arrow.svelte'
|
import Arrow from '../Arrow/Arrow.svelte'
|
||||||
@@ -92,12 +92,10 @@
|
|||||||
directionFnDescription[autoplayDirection]()
|
directionFnDescription[autoplayDirection]()
|
||||||
}, autoplayDuration)
|
}, autoplayDuration)
|
||||||
}
|
}
|
||||||
return {
|
return () => {
|
||||||
teardownAutoplay: () => {
|
|
||||||
interval && clearInterval(interval)
|
interval && clearInterval(interval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function addClones() {
|
function addClones() {
|
||||||
const first = pagesElement.children[0]
|
const first = pagesElement.children[0]
|
||||||
@@ -106,11 +104,13 @@
|
|||||||
pagesElement.append(first.cloneNode(true))
|
pagesElement.append(first.cloneNode(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(async () => {
|
let cleanupFns = []
|
||||||
|
onMount(() => {
|
||||||
|
(async () => {
|
||||||
await tick()
|
await tick()
|
||||||
const unsubscribe = store.subscribe(value => {
|
cleanupFns.push(store.subscribe(value => {
|
||||||
currentPageIndex = value.currentPageIndex
|
currentPageIndex = value.currentPageIndex
|
||||||
})
|
}))
|
||||||
if (pagesElement && pageWindowElement) {
|
if (pagesElement && pageWindowElement) {
|
||||||
// load first and last child to clone them
|
// load first and last child to clone them
|
||||||
loaded = [0, pagesElement.children.length - 1]
|
loaded = [0, pagesElement.children.length - 1]
|
||||||
@@ -118,15 +118,14 @@
|
|||||||
infinite && addClones()
|
infinite && addClones()
|
||||||
applyPageSizes()
|
applyPageSizes()
|
||||||
}
|
}
|
||||||
|
cleanupFns.push(applyAutoplay())
|
||||||
const { teardownAutoplay } = applyAutoplay()
|
|
||||||
|
|
||||||
addResizeEventListener(applyPageSizes)
|
addResizeEventListener(applyPageSizes)
|
||||||
return () => {
|
})()
|
||||||
|
})
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
removeResizeEventListener(applyPageSizes)
|
removeResizeEventListener(applyPageSizes)
|
||||||
teardownAutoplay()
|
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
|
||||||
unsubscribe()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
function handlePageChange(pageIndex) {
|
function handlePageChange(pageIndex) {
|
||||||
|
|||||||
Reference in New Issue
Block a user