Add minor optimization in media-session service

This commit is contained in:
Stefano Brilli 2021-09-21 00:17:10 +02:00
parent e63ca90c38
commit c647826f58
1 changed files with 13 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import { createEmptyWave } from '../create-empty-wave';
import { control } from '../redux/actions';
import { AppStore, AppSubscribe, AppGetState } from '../redux/store';
import { Dispatch } from '@reduxjs/toolkit';
import { Disc } from 'netmd-js';
export interface MediaSessionService {
init(): Promise<void>;
@ -92,6 +93,17 @@ export class BrowserMediaSessionService {
});
}
// This will save cpu cycles when just playing music. Might replace in the future with some memoization library
private sortedTracks: DisplayTrack[] = [];
private sortedTracksDisc: Disc | null = null;
getSortedTracksWithCache(disc: Disc | null) {
if (disc !== this.sortedTracksDisc) {
this.sortedTracks = getSortedTracks(disc);
this.sortedTracksDisc = disc;
}
return this.sortedTracks;
}
syncState() {
if (!this.initialized) {
return;
@ -105,7 +117,7 @@ export class BrowserMediaSessionService {
const isPlaying = deviceStatus?.state === 'playing';
const currentDiscTitle = disc?.title;
const currentTrackIndex = deviceStatus?.track ?? -1;
const allTracks = getSortedTracks(disc);
const allTracks = this.getSortedTracksWithCache(disc);
const currentTrack: DisplayTrack | undefined = allTracks[currentTrackIndex];
const currentTrackTitle = currentTrack ? currentTrack.fullWidthTitle || currentTrack?.title : '';
const currentTrackDurationInSecs = Math.round(currentTrack?.durationInSecs ?? -1);