Rewrite async calls in onMount

This commit is contained in:
Vadim
2021-01-31 21:23:23 +03:00
parent 71b534ef1d
commit d298f2cba2

View File

@@ -1,5 +1,5 @@
<script>
import { onMount, tick } from 'svelte'
import { onDestroy, onMount, tick } from 'svelte'
import { createStore } from '../../store'
import Dots from '../Dots/Dots.svelte'
import Arrow from '../Arrow/Arrow.svelte'
@@ -92,10 +92,8 @@
directionFnDescription[autoplayDirection]()
}, autoplayDuration)
}
return {
teardownAutoplay: () => {
interval && clearInterval(interval)
}
return () => {
interval && clearInterval(interval)
}
}
@@ -106,27 +104,28 @@
pagesElement.append(first.cloneNode(true))
}
onMount(async () => {
await tick()
const unsubscribe = store.subscribe(value => {
currentPageIndex = value.currentPageIndex
})
if (pagesElement && pageWindowElement) {
// load first and last child to clone them
loaded = [0, pagesElement.children.length - 1]
let cleanupFns = []
onMount(() => {
(async () => {
await tick()
infinite && addClones()
applyPageSizes()
}
cleanupFns.push(store.subscribe(value => {
currentPageIndex = value.currentPageIndex
}))
if (pagesElement && pageWindowElement) {
// load first and last child to clone them
loaded = [0, pagesElement.children.length - 1]
await tick()
infinite && addClones()
applyPageSizes()
}
cleanupFns.push(applyAutoplay())
addResizeEventListener(applyPageSizes)
})()
})
const { teardownAutoplay } = applyAutoplay()
addResizeEventListener(applyPageSizes)
return () => {
removeResizeEventListener(applyPageSizes)
teardownAutoplay()
unsubscribe()
}
onDestroy(() => {
removeResizeEventListener(applyPageSizes)
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
})
function handlePageChange(pageIndex) {