avoid setting queue when tracklist is the same

This commit is contained in:
Thomas Amland 2020-10-03 20:35:46 +02:00
parent f3bdbbd44d
commit 554d58f04b
2 changed files with 18 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import { Store, Module } from 'vuex' import { Store, Module } from 'vuex'
import { trackListEquals } from '@/shared/utils'
const audio = new Audio() const audio = new Audio()
const storedQueue = JSON.parse(localStorage.getItem('queue') || '[]') const storedQueue = JSON.parse(localStorage.getItem('queue') || '[]')
@ -87,8 +88,10 @@ export const playerModule: Module<State, any> = {
}, },
actions: { actions: {
async playTrackList({ commit }, { queue, index }) { async playTrackList({ commit, state }, { tracks, index }) {
commit('setQueue', [...queue]) if (!trackListEquals(state.queue, tracks)) {
commit('setQueue', [...tracks])
}
commit('setQueueIndex', index) commit('setQueueIndex', index)
commit('setPlaying') commit('setPlaying')
await audio.play() await audio.play()

View File

@ -1,4 +1,5 @@
import MD5 from 'md5-es' import MD5 from 'md5-es'
import { Track } from '@/shared/api'
export function randomString(): string { export function randomString(): string {
let arr = new Uint8Array(16) let arr = new Uint8Array(16)
@ -11,3 +12,15 @@ export function randomString(): string {
export function md5(str: string): string { export function md5(str: string): string {
return MD5.hash(str) return MD5.hash(str)
} }
export function trackListEquals(a: Track[], b: Track[]): boolean {
if (a.length !== b.length) {
return false
}
for (let i = 0; i < a.length; i++) {
if (a[i].id !== b[i].id) {
return false
}
}
return true
}