#31 : Update progress manager

This commit is contained in:
Vadim
2021-07-02 17:20:33 +03:00
parent 892550f196
commit 3f9f6c1147
2 changed files with 48 additions and 32 deletions

View File

@@ -18,8 +18,16 @@
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const directionFnDescription = { const directionFnDescription = {
[NEXT]: showNextPage, [NEXT]: () => {
[PREV]: showPrevPage progressManager.start(() => {
showNextPage()
})
},
[PREV]: () => {
progressManager.start(() => {
showPrevPage()
})
}
} }
/** /**
@@ -67,7 +75,7 @@
/** /**
* Pause autoplay on focus * Pause autoplay on focus
*/ */
export let pauseOnFocus = false export let pauseOnFocus = true
/** /**
* Current page indicator dots * Current page indicator dots
@@ -117,15 +125,12 @@
} }
}) })
let autoplayInterval = null
$: { $: {
if (pauseOnFocus) { if (pauseOnFocus) {
if (focused) { if (focused) {
progressManager.pause() progressManager.pause()
clearAutoplay()
} else { } else {
progressManager.resume() progressManager.resume()
applyAutoplay()
} }
} }
} }
@@ -146,19 +151,6 @@
offsetPage(false) offsetPage(false)
} }
function applyAutoplay() {
if (autoplay && !autoplayInterval) {
autoplayInterval = setInterval(() => {
directionFnDescription[autoplayDirection]()
}, autoplayDuration)
}
}
function clearAutoplay() {
clearInterval(autoplayInterval)
autoplayInterval = null
}
function addClones() { function addClones() {
const first = pagesElement.children[0] const first = pagesElement.children[0]
@@ -167,6 +159,12 @@
pagesElement.append(first.cloneNode(true)) pagesElement.append(first.cloneNode(true))
} }
function applyAutoplay() {
if (autoplay) {
directionFnDescription[autoplayDirection]()
}
}
let cleanupFns = [] let cleanupFns = []
onMount(() => { onMount(() => {
@@ -174,11 +172,7 @@
await tick() await tick()
cleanupFns.push(store.subscribe(value => { cleanupFns.push(store.subscribe(value => {
currentPageIndex = value.currentPageIndex currentPageIndex = value.currentPageIndex
console.log('currentPageIndex', currentPageIndex)
progressManager.reset() // clear interval out of condition in case autoplay was changed reactively
if (autoplay) {
progressManager.start()
}
})) }))
if (pagesElement && pageWindowElement) { if (pagesElement && pageWindowElement) {
// load first and last child to clone them // load first and last child to clone them
@@ -189,13 +183,14 @@
store.init(initialPageIndex + Number(infinite)) store.init(initialPageIndex + Number(infinite))
applyPageSizes() applyPageSizes()
} }
applyAutoplay() applyAutoplay()
addResizeEventListener(applyPageSizes) addResizeEventListener(applyPageSizes)
})() })()
}) })
onDestroy(() => { onDestroy(() => {
clearAutoplay()
removeResizeEventListener(applyPageSizes) removeResizeEventListener(applyPageSizes)
cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn()) cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn())
}) })
@@ -219,12 +214,14 @@
// Disable page change while animation is in progress // Disable page change while animation is in progress
let disabled = false let disabled = false
function safeChangePage(cb, options) { function safeChangePage(cb, options) {
applyAutoplay()
const animated = get(options, 'animated', true) const animated = get(options, 'animated', true)
if (disabled) return if (disabled) return
cb() cb()
disabled = true disabled = true
setTimeout(() => { setTimeout(() => {
disabled = false disabled = false
}, animated ? duration : 0) }, animated ? duration : 0)
} }
@@ -258,6 +255,7 @@
_duration = 0 _duration = 0
} }
function handleThreshold(event) { function handleThreshold(event) {
// TODO: is it correct?
directionFnDescription[event.detail.direction]() directionFnDescription[event.detail.direction]()
} }
function handleSwipeMove(event) { function handleSwipeMove(event) {
@@ -333,6 +331,7 @@
></Dots> ></Dots>
</slot> </slot>
{/if} {/if}
{progressValue}
<Progress value={progressValue} /> <Progress value={progressValue} />
</div> </div>

View File

@@ -1,9 +1,15 @@
const PROGRESS_STEPS_COUNT = 100 const PROGRESS_STEPS_COUNT = 10
const setIntervalImmediately = (fn, ms) => {
fn();
return setInterval(fn, ms);
}
export class ProgressManager { export class ProgressManager {
#autoplayDuration #autoplayDuration
#onValueChange #onValueChange
#interval #interval
#paused = false #paused = false
@@ -15,18 +21,26 @@ export class ProgressManager {
this.#onValueChange = onValueChange this.#onValueChange = onValueChange
} }
start() { start(onFinish) {
let value = 0 this.reset()
let progress = 0
const stepMs = this.#autoplayDuration / PROGRESS_STEPS_COUNT
this.#interval = setInterval(() => { const stepMs = this.#autoplayDuration / PROGRESS_STEPS_COUNT
let progress = -stepMs
this.#interval = setIntervalImmediately(() => {
if (this.#paused) { if (this.#paused) {
return return
} }
progress += stepMs progress += stepMs
value = progress / this.#autoplayDuration
const value = progress / this.#autoplayDuration
this.#onValueChange(value) this.#onValueChange(value)
if (value > 1) {
this.reset()
onFinish()
this.start(onFinish)
}
}, stepMs) }, stepMs)
} }
@@ -40,5 +54,8 @@ export class ProgressManager {
reset() { reset() {
clearInterval(this.#interval) clearInterval(this.#interval)
// this.#onValueChange(0)
} }
// TODO: add on destroy
} }