From 58195cd4d1fa2fd5395da8e988f906a09d063a62 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Tue, 7 May 2019 11:59:41 +0200
Subject: [PATCH] Added small timeout before playback to avoid loading lots of
 tracks when skipping through the queue

---
 front/src/components/audio/Player.vue | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/front/src/components/audio/Player.vue b/front/src/components/audio/Player.vue
index 2086c0d4..81bdb91d 100644
--- a/front/src/components/audio/Player.vue
+++ b/front/src/components/audio/Player.vue
@@ -254,7 +254,8 @@ export default {
       maxPreloaded: 3,
       preloadDelay: 15,
       soundsCache: [],
-      soundId: null
+      soundId: null,
+      playTimeout: null
     }
   },
   mounted() {
@@ -577,6 +578,7 @@ export default {
           this.soundId = this.currentSound.play()
           this.$store.commit('player/errored', false)
           this.$store.commit('player/playing', true)
+          this.$store.dispatch('player/updateProgress', 0)
           this.observeProgress(true)
         }
       }
@@ -672,10 +674,18 @@ export default {
   watch: {
     currentTrack: {
       async handler (newValue, oldValue) {
-        await this.loadSound(newValue, oldValue)
-        if (!newValue || !newValue.album.cover) {
-          this.ambiantColors = this.defaultAmbiantColors
+        clearTimeout(this.playTimeout)
+        let self = this
+        if (this.currentSound) {
+          this.currentSound.pause()
         }
+        this.$store.commit("player/isLoadingAudio", true)
+        this.playTimeout = setTimeout(async () => {
+          await self.loadSound(newValue, oldValue)
+          if (!newValue || !newValue.album.cover) {
+            self.ambiantColors = self.defaultAmbiantColors
+          }
+        }, 500);
       },
       immediate: false
     },
-- 
GitLab