grafana/public/app/features/scenes/services/UrlSyncManager.ts
Torkel Ödegaard 8d92417a16
Scenes: Improve typing of scene state to avoid type guards and casting (#52422)
* Trying to get rid of type guard but failing

* Improve typing of scene object state

* Fixed wrongly renamed event

* Tweaks
2022-07-19 17:46:49 +02:00

36 lines
1.1 KiB
TypeScript

import { Location } from 'history';
import { Unsubscribable } from 'rxjs';
import { locationService } from '@grafana/runtime';
import { SceneObjectStateChangedEvent } from '../core/events';
import { SceneObject } from '../core/types';
export class UrlSyncManager {
private locationListenerUnsub: () => void;
private stateChangeSub: Unsubscribable;
constructor(sceneRoot: SceneObject) {
this.stateChangeSub = sceneRoot.events.subscribe(SceneObjectStateChangedEvent, this.onStateChanged);
this.locationListenerUnsub = locationService.getHistory().listen(this.onLocationUpdate);
}
onLocationUpdate = (location: Location) => {
// TODO: find any scene object whose state we need to update
};
onStateChanged = ({ payload }: SceneObjectStateChangedEvent) => {
const changedObject = payload.changedObject;
if ('getUrlState' in changedObject) {
const urlUpdate = changedObject.getUrlState();
locationService.partial(urlUpdate, true);
}
};
cleanUp() {
this.stateChangeSub.unsubscribe();
this.locationListenerUnsub();
}
}