Skip to content

Commit

Permalink
fix(ui): click on Play button without any track still changes icon
Browse files Browse the repository at this point in the history
chore(ui): reduce CPU usage
  • Loading branch information
feugy committed Oct 1, 2020
1 parent f98684d commit 03c21ca
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions renderer/components/Player/Player.svelte
Expand Up @@ -19,6 +19,7 @@
export let isPlaylistOpen = false
let isPlaying
let player
let timeUpdateTimer
let duration = 0
let currentTime = 0
let nextSeek = null
Expand Down Expand Up @@ -64,16 +65,18 @@
if (player) {
clearTimeout(nextSeek)
nextSeek = setTimeout(function () {
currentTime = detail
player.currentTime = detail
}, 100)
}
}
function handlePlay() {
if (isPlaying) {
player.pause()
} else {
player.play()
if (src) {
if (isPlaying) {
player.pause()
} else {
player.play()
}
}
}
Expand Down Expand Up @@ -115,6 +118,17 @@
shuffle()
}
}
// Do not use svelte's `bind:currentTime` who eats too much CPU
// instead, poll the current time ourselves, up to 10 times per second
function updateTime() {
if (player) {
currentTime = player.currentTime
setTimeout(() => {
timeUpdateTimer = window.requestAnimationFrame(updateTime)
}, 100)
}
}
</script>

<style type="postcss">
Expand Down Expand Up @@ -172,15 +186,16 @@
autoplay
{src}
on:ended={handleEnded}
bind:currentTime
bind:duration
bind:volume
bind:muted
on:play={() => {
isPlaying = true
timeUpdateTimer = window.requestAnimationFrame(updateTime)
}}
on:pause={() => {
isPlaying = false
window.cancelAnimationFrame(timeUpdateTimer)
}} />

<div class="flex items-center">
Expand Down

0 comments on commit 03c21ca

Please sign in to comment.