From 462e9b422ff8830c84819e4fe2a14566d68a8414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Fern=C3=A1ndez?= Date: Fri, 24 Feb 2023 08:51:42 +0000 Subject: [PATCH] fast stop without waiting for request --- frontend/src/store/playbackManager.ts | 39 +++++++++++++++------------ 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/frontend/src/store/playbackManager.ts b/frontend/src/store/playbackManager.ts index 84d7c1e8925..38d235b0734 100644 --- a/frontend/src/store/playbackManager.ts +++ b/frontend/src/store/playbackManager.ts @@ -621,18 +621,25 @@ class PlaybackManagerStore { /** * Report playback stopped to the server. Used by the "Now playing" statistics in other clients. */ - private _reportPlaybackStopped = async (itemId: string): Promise => { + private _reportPlaybackStopped = async ( + itemId: string, + sessionId = state.playSessionId, + currentTime = this.currentTime, + updateState = true + ): Promise => { const remote = useRemote(); await remote.sdk.newUserApi(getPlaystateApi).reportPlaybackStopped({ playbackStopInfo: { ItemId: itemId, - PlaySessionId: state.playSessionId, - PositionTicks: msToTicks((this.currentTime || 0) * 1000) + PlaySessionId: sessionId, + PositionTicks: msToTicks((currentTime || 0) * 1000) } }); - state.lastProgressUpdate = Date.now(); + if (updateState) { + state.lastProgressUpdate = Date.now(); + } }; /** @@ -837,24 +844,22 @@ class PlaybackManagerStore { }; public stop = (): void => { + const sessionId = String(state.playSessionId || ''); + const time = Number(this.currentTime); + const itemId = String(this.currentItem?.Id || ''); + const volume = Number(this.currentVolume); + + Object.assign(state, defaultState); + this.currentVolume = volume; + window.setTimeout(async () => { const remote = useRemote(); try { - if ( - !isNil(this.currentItem) && - !isNil(this.currentItem.Id) && - !isNil(remote.auth.currentUser) - ) { - this._reportPlaybackStopped(this.currentItem.Id); + if (sessionId && itemId && time && remote.auth.currentUser) { + await this._reportPlaybackStopped(itemId, sessionId, time, false); } - } catch { - } finally { - const volume = this.currentVolume; - - Object.assign(state, defaultState); - this.currentVolume = volume; - } + } catch {} }); };