grafana/public/app/features/live/channel-config/index.ts
ArturWierzbicki e8a30f651e
Live: Extract scopes from CentrifugeSrv (#41051)
* Refactor: remove `scopes` from CentrifugeSrv, remove dependencies on window/config/user context

* Refactor: add GrafanaLiveChannelConfigService to wrap direct access to *Scope classes

* Refactor: added GrafanaLiveService acting like a proxy to GrafanaLiveSrv + LiveChannelConfigSrv

* Refactor: live module instantiation

* Refactor: import fixes

* Fix: URL construction in centrifugeSrv
2021-10-28 21:48:57 +04:00

37 lines
1.4 KiB
TypeScript

import { LiveChannelScope, LiveChannelSupport, SelectableValue } from '@grafana/data';
import {
grafanaLiveCoreFeatures,
GrafanaLiveDataSourceScope,
GrafanaLivePluginScope,
GrafanaLiveScope,
GrafanaLiveStreamScope,
} from './scope';
import { GrafanaLiveChannelConfigSrv, ExistingLiveChannelScope } from './types';
export class GrafanaLiveChannelConfigService implements GrafanaLiveChannelConfigSrv {
private readonly scopes: Record<LiveChannelScope, GrafanaLiveScope>;
constructor() {
this.scopes = Object.freeze({
[LiveChannelScope.Grafana]: grafanaLiveCoreFeatures,
[LiveChannelScope.DataSource]: new GrafanaLiveDataSourceScope(),
[LiveChannelScope.Plugin]: new GrafanaLivePluginScope(),
[LiveChannelScope.Stream]: new GrafanaLiveStreamScope(),
});
}
private getScope = (liveChannelScope: ExistingLiveChannelScope): GrafanaLiveScope =>
this.scopes[liveChannelScope as LiveChannelScope];
doesScopeExist = (liveChannelScope: LiveChannelScope): liveChannelScope is ExistingLiveChannelScope =>
Boolean(this.scopes[liveChannelScope]);
getChannelSupport = async (
liveChannelScope: ExistingLiveChannelScope,
namespace: string
): Promise<LiveChannelSupport | undefined> => this.getScope(liveChannelScope).getChannelSupport(namespace);
getNamespaces = async (liveChannelScope: ExistingLiveChannelScope): Promise<Array<SelectableValue<string>>> =>
this.getScope(liveChannelScope).listNamespaces();
}