mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Trying to get rid of type guard but failing * Improve typing of scene object state * Fixed wrongly renamed event * Tweaks
36 lines
1.1 KiB
TypeScript
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();
|
|
}
|
|
}
|