From 14200519cc04a062511d37ad0ff85ca0c3ba4703 Mon Sep 17 00:00:00 2001 From: Travis Shivers Date: Tue, 30 Jun 2020 16:47:12 -0500 Subject: [PATCH] Cleaning slplayer --- config/defaults.js | 3 + src/store/modules/slplayer/actions.js | 81 +++++++++++-------------- src/store/modules/slplayer/getters.js | 9 +++ src/store/modules/slplayer/mutations.js | 4 ++ src/store/modules/slplayer/state.js | 1 + src/store/modules/synclounge/actions.js | 1 + 6 files changed, 53 insertions(+), 46 deletions(-) diff --git a/config/defaults.js b/config/defaults.js index 364cfb92..20256740 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -45,6 +45,9 @@ const defaults = { default_client_poll_interval: 1000, default_sync_flexability: 3000, default_sync_mode: 'cleanseek', + + slplayer_plex_timeline_update_interval: 10000, + slplayer_controls_visible_checker_interval: 500, }; module.exports = defaults; diff --git a/src/store/modules/slplayer/actions.js b/src/store/modules/slplayer/actions.js index 2784f080..e43f87af 100644 --- a/src/store/modules/slplayer/actions.js +++ b/src/store/modules/slplayer/actions.js @@ -3,6 +3,7 @@ import axios from 'axios'; import generateGuid from '@/utils/guid'; import timeoutPromise from '@/utils/timeoutpromise'; import delay from '@/utils/delay'; +import cancelablePeriodicTask from '@/utils/cancelableperiodictask'; // These functions are a bit special since they use currentTime and duration, which can't // be tracked by vuex, so the cache isn't updated correctly @@ -38,25 +39,20 @@ const isTimeInBufferedRange = (getters, timeMs) => { return false; }; -const isPlayerPaused = (getters) => getters.GET_PLAYER_MEDIA_ELEMENT.paused - && !getters.GET_PLAYER_UI.getControls().isSeeking(); +export default { + FETCH_ARE_PLAYER_CONTROLS_SHOWN: ({ getters }) => { + // This can't be a normal getter becasuse shaka isn't reactive + // eslint-disable-next-line no-underscore-dangle + if (!getters.GET_PLAYER_UI.getControls().enabled_) { + return false; + } -const isPlayerPlaying = (getters) => !getters.GET_PLAYER_MEDIA_ELEMENT.paused - && !getters.GET_PLAYER.isBuffering(); - -const arePlayerControlsShown = (getters) => { - // eslint-disable-next-line no-underscore-dangle - if (!getters.GET_PLAYER_UI.getControls().enabled_) { - return false; - } - - // eslint-disable-next-line no-underscore-dangle - return getters.GET_PLAYER_UI.getControls().getControlsContainer().getAttribute('shown') != null + // eslint-disable-next-line no-underscore-dangle + return getters.GET_PLAYER_UI.getControls().getControlsContainer().getAttribute('shown') != null // eslint-disable-next-line no-underscore-dangle || getters.GET_PLAYER_UI.getControls().getControlsContainer().getAttribute('casting') != null; -}; + }, -export default { SEND_PLEX_DECISION_REQUEST: async ({ getters, commit }) => { const { data } = await axios.get(getters.GET_DECISION_URL, { params: getters.GET_DECISION_AND_START_PARAMS, @@ -134,13 +130,13 @@ export default { }), HANDLE_PLAYER_PLAYING: ({ dispatch, getters }) => { - if (isPlayerPlaying(getters)) { + if (getters.IS_PLAYER_PLAYING()) { dispatch('CHANGE_PLAYER_STATE', 'playing'); } }, HANDLE_PLAYER_PAUSE: ({ dispatch, getters }) => { - if (isPlayerPaused(getters)) { + if (getters.IS_PLAYER_PAUSED()) { if (!getters.GET_PLAYER.isBuffering()) { dispatch('CHANGE_PLAYER_STATE', 'paused'); } @@ -246,34 +242,24 @@ export default { } }, - START_PERIODIC_PLEX_TIMELINE_UPDATE: async ({ getters, dispatch }) => { - // Send out a timeline update to plex periodically. - - // eslint-disable-next-line no-constant-condition - while (true) { - if (!getters.GET_PLAYER_MEDIA_ELEMENT) { - return true; - } - - const delayPromise = delay(10000); - // eslint-disable-next-line no-await-in-loop - await dispatch('SEND_PLEX_TIMELINE_UPDATE').catch((e) => e); - // eslint-disable-next-line no-await-in-loop - await delayPromise; - } + START_PERIODIC_PLEX_TIMELINE_UPDATE: async ({ commit, dispatch, rootGetters }) => { + commit('SET_PLEX_TIMELINE_UPDATER_CANCELER', cancelablePeriodicTask( + () => dispatch('SEND_PLEX_TIMELINE_UPDATE').catch(console.log), + () => rootGetters.GET_CONFIG.slplayer_plex_timeline_update_interval, + )); }, - START_UPDATE_PLAYER_CONTROLS_SHOWN_INTERVAL: ({ commit, getters }) => { - commit('SET_PLAYER_CONTROLS_SHOWN_INTERVAL', setInterval(() => { - commit('UPDATE_PLAYER_CONTROLS_SHOWN', arePlayerControlsShown(getters)); - }, 500)); + START_UPDATE_PLAYER_CONTROLS_SHOWN_INTERVAL: ({ commit, dispatch, rootGetters }) => { + commit('SET_PLAYER_CONTROLS_SHOWN_INTERVAL', setInterval(async () => { + commit('UPDATE_PLAYER_CONTROLS_SHOWN', await dispatch('FETCH_ARE_PLAYER_CONTROLS_SHOWN')); + }, rootGetters.slplayer_controls_visible_checker_interval)); }, CHANGE_PLAYER_STATE: async ({ commit, dispatch }, state) => { commit('SET_PLAYER_STATE', state); - const result = dispatch('SEND_PLEX_TIMELINE_UPDATE'); + const plexTimelineUpdatePromise = dispatch('SEND_PLEX_TIMELINE_UPDATE'); await dispatch('synclounge/POLL', null, { root: true }); - return result; + return plexTimelineUpdatePromise; }, LOAD_PLAYER_SRC: async ({ getters, commit }) => { @@ -303,8 +289,8 @@ export default { commit('SET_PLAYER_VOLUME', rootGetters['settings/GET_SLPLAYERVOLUME']); - dispatch('START_PERIODIC_PLEX_TIMELINE_UPDATE'); - dispatch('START_UPDATE_PLAYER_CONTROLS_SHOWN_INTERVAL'); + await dispatch('START_PERIODIC_PLEX_TIMELINE_UPDATE'); + await dispatch('START_UPDATE_PLAYER_CONTROLS_SHOWN_INTERVAL'); return result; }, @@ -312,6 +298,9 @@ export default { commit('STOP_UPDATE_PLAYER_CONTROLS_SHOWN_INTERVAL'); await dispatch('UNREGISTER_PLAYER_EVENTS'); + getters.GET_PLEX_TIMELINE_UPDATER_CANCELER(); + commit('SET_PLEX_TIMELINE_UPDATER_CANCELER', null); + commit('plexclients/SET_ACTIVE_MEDIA_METADATA', null, { root: true }); commit('plexclients/SET_ACTIVE_SERVER_ID', null, { root: true }); // Leaving play queue around for possible upnext @@ -329,7 +318,7 @@ export default { commit('REMOVE_BUFFERING_EVENT_LISTENER'); }, - PLAY_PAUSE_VIDEO: ({ getters, commit }) => { + PLAY_PAUSE_VIDEO: async ({ getters, dispatch }) => { if (!getPlayerDurationMs(getters)) { // Can't play yet. Ignore. return; @@ -337,16 +326,16 @@ export default { getters.GET_PLAYER.cancelTrickPlay(); - if (isPlayerPaused(getters)) { - commit('PLAY'); + if (getters.IS_PLAYER_PAUSED()) { + await dispatch('PRESS_PLAY'); } else { - commit('PAUSE'); + await dispatch('PRESS_PAUSE'); } }, - SEND_PARTY_PLAY_PAUSE: ({ dispatch, getters }) => { + SEND_PARTY_PLAY_PAUSE: async ({ dispatch, getters }) => { // If the player was actually paused (and not just paused for seeking) - dispatch('synclounge/sendPartyPause', isPlayerPaused(getters), { root: true }); + await dispatch('synclounge/sendPartyPause', getters.IS_PLAYER_PAUSED(), { root: true }); }, PLAY_NEXT: ({ dispatch, commit }) => { diff --git a/src/store/modules/slplayer/getters.js b/src/store/modules/slplayer/getters.js index 7fa2daab..88e6b4d3 100644 --- a/src/store/modules/slplayer/getters.js +++ b/src/store/modules/slplayer/getters.js @@ -156,4 +156,13 @@ export default { : null), GET_X_PLEX_SESSION_ID: (state) => state.xplexsessionId, + + GET_PLEX_TIMELINE_UPDATER_CANCELER: (state) => state.plexTimelineUpdaterCanceler, + + IS_PLAYER_PAUSED: (state, getters) => () => getters.GET_PLAYER_MEDIA_ELEMENT.paused + && !getters.GET_PLAYER_UI.getControls().isSeeking(), + + IS_PLAYER_PLAYING: (state, getters) => () => !getters.GET_PLAYER_MEDIA_ELEMENT.paused + && !getters.GET_PLAYER.isBuffering(), + }; diff --git a/src/store/modules/slplayer/mutations.js b/src/store/modules/slplayer/mutations.js index 6925bd9a..6264d6a6 100644 --- a/src/store/modules/slplayer/mutations.js +++ b/src/store/modules/slplayer/mutations.js @@ -74,4 +74,8 @@ export default { state.player.removeEventListener('buffering', state.bufferingEventListener); state.bufferingEventListener = null; }, + + SET_PLEX_TIMELINE_UPDATER_CANCELER: (state, canceler) => { + state.plexTimelineUpdaterCanceler = canceler; + }, }; diff --git a/src/store/modules/slplayer/state.js b/src/store/modules/slplayer/state.js index fdfa44da..8f2b7554 100644 --- a/src/store/modules/slplayer/state.js +++ b/src/store/modules/slplayer/state.js @@ -15,6 +15,7 @@ const state = () => ({ playerControlsShown: true, playerControlsShownInterval: null, bufferingEventListener: null, + plexTimelineUpdaterCanceler: null, }); export default state; diff --git a/src/store/modules/synclounge/actions.js b/src/store/modules/synclounge/actions.js index 54157d6e..9fdadfb6 100644 --- a/src/store/modules/synclounge/actions.js +++ b/src/store/modules/synclounge/actions.js @@ -96,6 +96,7 @@ export default { // Cancel poller getters.GET_CLIENT_POLLER_CANCELER(); + commit('SET_CLIENT_POLLER_CANCELER', null); getters.GET_SOCKET.disconnect(); commit('SET_IS_IN_ROOM', false);