DashboardSceneChangeTracker: Do not load the worker until is editing (#83817)

This commit is contained in:
Ivan Ortega Alba 2024-03-04 14:31:14 +01:00 committed by GitHub
parent 519f965c8e
commit 59fb26443f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View File

@ -22,12 +22,11 @@ import { DashboardChangeInfo } from './shared';
export class DashboardSceneChangeTracker {
private _changeTrackerSub: Unsubscribable | undefined;
private _changesWorker: Worker;
private _changesWorker?: Worker;
private _dashboard: DashboardScene;
constructor(dashboard: DashboardScene) {
this._dashboard = dashboard;
this._changesWorker = createWorker();
}
private onStateChanged({ payload }: SceneObjectStateChangedEvent) {
@ -95,8 +94,15 @@ export class DashboardSceneChangeTracker {
}
}
private init() {
this._changesWorker = createWorker();
}
public startTrackingChanges() {
this._changesWorker.onmessage = (e: MessageEvent<DashboardChangeInfo>) => {
if (!this._changesWorker) {
this.init();
}
this._changesWorker!.onmessage = (e: MessageEvent<DashboardChangeInfo>) => {
this.updateIsDirty(e.data);
};
@ -112,6 +118,6 @@ export class DashboardSceneChangeTracker {
public terminate() {
this.stopTrackingChanges();
this._changesWorker.terminate();
this._changesWorker?.terminate();
}
}

View File

@ -66,6 +66,23 @@ describe('DashboardScene', () => {
});
describe('Editing and discarding', () => {
describe('Given scene in view mode', () => {
it('Should set isEditing to false', () => {
const scene = buildTestScene();
scene.activate();
expect(scene.state.isEditing).toBeFalsy();
});
it('Should not start the detect changes worker', () => {
const scene = buildTestScene();
scene.activate();
// @ts-expect-error it is a private property
expect(scene._changesWorker).toBeUndefined();
});
});
describe('Given scene in edit mode', () => {
let scene: DashboardScene;
let deactivateScene: () => void;