Playlists: Check if playlist is playing before reloading for assets (#93605)

* Check if playlist is playing before updating

* add tests
This commit is contained in:
Oscar Kilhed 2024-09-24 13:00:03 +01:00 committed by GitHub
parent f04e032cf1
commit 7f7fed8c3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 3 deletions

View File

@ -1,4 +1,7 @@
import { Location } from 'history';
import { locationService, setBackendSrv, BackendSrv } from '@grafana/runtime';
import { playlistSrv } from 'app/features/playlist/PlaylistSrv';
import { NewFrontendAssetsChecker } from './NewFrontendAssetsChecker';
@ -46,4 +49,28 @@ describe('NewFrontendAssetsChecker', () => {
expect(backendApiGet).toHaveBeenCalledTimes(2);
});
it('should skip reloading if we are playing a playlist', () => {
const checker = new NewFrontendAssetsCheckerExposedLocationUpdate();
const reloadMock = jest.fn();
checker.reloadIfUpdateDetected = reloadMock;
playlistSrv.state.isPlaying = true;
checker.doLocationUpdated({ hash: 'foo', pathname: '/d/dashboarduid', state: {}, search: '' });
expect(reloadMock).not.toHaveBeenCalled();
playlistSrv.state.isPlaying = false;
});
it('should reload if we are accessing a dashboard', () => {
const checker = new NewFrontendAssetsCheckerExposedLocationUpdate();
const reloadMock = jest.fn();
checker.reloadIfUpdateDetected = reloadMock;
checker.doLocationUpdated({ hash: 'foo', pathname: '/d/dashboarduid', state: {}, search: '' });
expect(reloadMock).toHaveBeenCalled();
});
});
class NewFrontendAssetsCheckerExposedLocationUpdate extends NewFrontendAssetsChecker {
public doLocationUpdated(location: Location) {
this.locationUpdated(location);
}
}

View File

@ -2,6 +2,7 @@ import { Location } from 'history';
import { isEqual } from 'lodash';
import { getBackendSrv, getGrafanaLiveSrv, locationService, reportInteraction } from '@grafana/runtime';
import { playlistSrv } from 'app/features/playlist/PlaylistSrv';
export class NewFrontendAssetsChecker {
private hasUpdates = false;
@ -35,7 +36,7 @@ export class NewFrontendAssetsChecker {
/**
* Tries to detect some navigation events where it's safe to trigger a reload
*/
private locationUpdated(location: Location) {
protected locationUpdated(location: Location) {
if (this.prevLocationPath === location.pathname) {
return;
}
@ -46,8 +47,8 @@ export class NewFrontendAssetsChecker {
if (newLocationSegments[1] === '/' && this.prevLocationPath !== '/') {
this.reloadIfUpdateDetected();
}
// Moving to dashboard (or changing dashboards)
else if (newLocationSegments[1] === 'd') {
// Moving to dashboard (or changing dashboards, except when we're playing a playlist)
else if (newLocationSegments[1] === 'd' && !playlistSrv.state.isPlaying) {
this.reloadIfUpdateDetected();
}
// Track potential page change