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