funkwhale/front/src/store/player.ts

194 wiersze
5.0 KiB
TypeScript
Czysty Zwykły widok Historia

2022-07-04 22:52:53 +00:00
import type { Module } from 'vuex'
import type { RootState } from '~/store/index'
2018-01-11 20:35:51 +00:00
import axios from 'axios'
2022-04-23 07:37:43 +00:00
import time from '~/utils/time'
import useLogger from '~/composables/useLogger'
export interface State {
maxConsecutiveErrors: number
errorCount: number
playing: boolean
isLoadingAudio: boolean
volume: number
tempVolume: number
duration: number
currentTime: number
errored: boolean
bufferProgress: number
2022-07-20 22:44:19 +00:00
looping: 0 | 1 | 2 // 0 -> no, 1 -> on track, 2 -> on queue
}
const logger = useLogger()
const store: Module<State, RootState> = {
namespaced: true,
state: {
maxConsecutiveErrors: 5,
errorCount: 0,
playing: false,
isLoadingAudio: false,
volume: 1,
tempVolume: 0.5,
duration: 0,
currentTime: 0,
errored: false,
bufferProgress: 0,
2022-07-20 22:44:19 +00:00
looping: 0
},
mutations: {
reset (state) {
state.errorCount = 0
state.playing = false
},
volume (state, value) {
value = parseFloat(value)
value = Math.min(value, 1)
value = Math.max(value, 0)
state.volume = value
},
tempVolume (state, value) {
value = parseFloat(value)
value = Math.min(value, 1)
value = Math.max(value, 0)
state.tempVolume = value
},
incrementVolume (state, value) {
value = parseFloat(state.volume + value)
value = Math.min(value, 1)
value = Math.max(value, 0)
state.volume = value
},
incrementErrorCount (state) {
state.errorCount += 1
},
resetErrorCount (state) {
state.errorCount = 0
},
duration (state, value) {
state.duration = value
},
errored (state, value) {
state.errored = value
},
currentTime (state, value) {
state.currentTime = value
},
looping (state, value) {
state.looping = value
},
playing (state, value) {
state.playing = value
},
bufferProgress (state, value) {
state.bufferProgress = value
},
toggleLooping (state) {
if (state.looping > 1) {
state.looping = 0
} else {
state.looping += 1
}
},
isLoadingAudio (state, value) {
state.isLoadingAudio = value
}
},
getters: {
durationFormatted: state => {
return time.parse(Math.round(state.duration))
},
currentTimeFormatted: state => {
return time.parse(Math.round(state.currentTime))
},
progress: state => {
2021-10-21 08:22:30 +00:00
return Math.round((state.currentTime / state.duration * 100) * 10) / 10
}
},
actions: {
2021-09-25 19:44:02 +00:00
incrementVolume ({ commit, state }, value) {
commit('volume', state.volume + value)
},
2021-09-25 19:44:02 +00:00
stop ({ commit }) {
commit('errored', false)
commit('resetErrorCount')
},
2021-09-25 19:44:02 +00:00
togglePlayback ({ commit, state, dispatch }) {
commit('playing', !state.playing)
if (state.errored && state.errorCount < state.maxConsecutiveErrors) {
setTimeout(() => {
if (state.playing) {
2021-09-25 19:44:02 +00:00
dispatch('queue/next', null, { root: true })
}
}, 3000)
}
},
2022-07-20 22:44:19 +00:00
async resumePlayback ({ commit, state, dispatch }) {
commit('playing', true)
if (state.errored && state.errorCount < state.maxConsecutiveErrors) {
2022-07-20 22:44:19 +00:00
await new Promise(resolve => setTimeout(resolve, 3000))
if (state.playing) {
return dispatch('queue/next', null, { root: true })
}
}
},
2021-09-25 19:44:02 +00:00
pausePlayback ({ commit }) {
commit('playing', false)
},
2021-09-25 19:44:02 +00:00
toggleMute ({ commit, state }) {
2019-06-25 15:48:01 +00:00
if (state.volume > 0) {
commit('tempVolume', state.volume)
commit('volume', 0)
2021-09-25 19:44:02 +00:00
} else {
2019-06-25 15:48:01 +00:00
commit('volume', state.tempVolume)
}
},
trackListened ({ rootState }, track) {
if (!rootState.auth.authenticated) {
return
}
return axios.post('history/listenings/', { track: track.id }).catch((error) => {
logger.error('Could not record track in history', error)
})
},
trackEnded ({ commit, dispatch, rootState }) {
2021-09-25 19:44:02 +00:00
const queueState = rootState.queue
if (queueState.currentIndex === queueState.tracks.length - 1) {
// we've reached last track of queue, trigger a reload
// from radio if any
2021-09-25 19:44:02 +00:00
dispatch('radios/populateQueue', null, { root: true })
}
2021-09-25 19:44:02 +00:00
dispatch('queue/next', null, { root: true })
if (queueState.ended) {
// Reset playback
commit('playing', false)
dispatch('updateProgress', 0)
}
},
2021-09-25 19:44:02 +00:00
trackErrored ({ commit, dispatch, state }) {
commit('errored', true)
commit('incrementErrorCount')
if (state.errorCount < state.maxConsecutiveErrors) {
setTimeout(() => {
if (state.playing) {
2021-09-25 19:44:02 +00:00
dispatch('queue/next', null, { root: true })
}
}, 3000)
}
},
2021-09-25 19:44:02 +00:00
updateProgress ({ commit }, t) {
commit('currentTime', t)
},
2021-09-25 19:44:02 +00:00
mute ({ commit, state }) {
commit('tempVolume', state.volume)
commit('volume', 0)
},
2021-09-25 19:44:02 +00:00
unmute ({ commit, state }) {
commit('volume', state.tempVolume)
}
}
}
export default store