2022-07-07 01:53:02 -05:00
|
|
|
import { Location } from 'history';
|
|
|
|
import { Unsubscribable } from 'rxjs';
|
|
|
|
|
|
|
|
import { locationService } from '@grafana/runtime';
|
|
|
|
|
|
|
|
import { SceneObjectStateChangedEvent } from '../core/events';
|
2022-07-19 10:46:49 -05:00
|
|
|
import { SceneObject } from '../core/types';
|
2022-07-07 01:53:02 -05:00
|
|
|
|
|
|
|
export class UrlSyncManager {
|
|
|
|
private locationListenerUnsub: () => void;
|
|
|
|
private stateChangeSub: Unsubscribable;
|
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
public constructor(sceneRoot: SceneObject) {
|
2022-10-31 09:16:16 -05:00
|
|
|
this.stateChangeSub = sceneRoot.subscribeToEvent(SceneObjectStateChangedEvent, this.onStateChanged);
|
2022-07-07 01:53:02 -05:00
|
|
|
this.locationListenerUnsub = locationService.getHistory().listen(this.onLocationUpdate);
|
|
|
|
}
|
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
private onLocationUpdate = (location: Location) => {
|
2022-07-07 01:53:02 -05:00
|
|
|
// TODO: find any scene object whose state we need to update
|
|
|
|
};
|
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
private onStateChanged = ({ payload }: SceneObjectStateChangedEvent) => {
|
2022-07-07 01:53:02 -05:00
|
|
|
const changedObject = payload.changedObject;
|
|
|
|
|
2022-07-19 10:46:49 -05:00
|
|
|
if ('getUrlState' in changedObject) {
|
|
|
|
const urlUpdate = changedObject.getUrlState();
|
|
|
|
locationService.partial(urlUpdate, true);
|
|
|
|
}
|
2022-07-07 01:53:02 -05:00
|
|
|
};
|
|
|
|
|
2022-11-07 08:32:02 -06:00
|
|
|
public cleanUp() {
|
2022-07-07 01:53:02 -05:00
|
|
|
this.stateChangeSub.unsubscribe();
|
|
|
|
this.locationListenerUnsub();
|
|
|
|
}
|
|
|
|
}
|