Skip to content

Commit

Permalink
fixing generic return type
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jun 25, 2020
1 parent dcc264e commit 33374c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions x-pack/plugins/observability/public/data_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ interface FetchDataParams {
export type FetchData<T extends FetchDataResponse = FetchDataResponse> = (
fetchDataParams: FetchDataParams
) => Promise<T>;

export type HasData = () => Promise<boolean>;

interface DataHandler {
fetchData: FetchData;
interface DataHandler<T extends ObservabilityApp = ObservabilityApp> {
fetchData: FetchData<ObservabilityFetchDataResponse[T]>;
hasData: HasData;
}

const dataHandlers: Partial<Record<ObservabilityApp, DataHandler>> = {};

export type RegisterDataHandler<T extends ObservabilityApp = ObservabilityApp> = (params: {
appName: T;
fetchData: FetchData<ObservabilityFetchDataResponse[T]>;
hasData: HasData;
}) => void;

export const registerDataHandler: RegisterDataHandler = ({ appName, fetchData, hasData }) => {
export function registerDataHandler<T extends ObservabilityApp = ObservabilityApp>({
appName,
fetchData,
hasData,
}: { appName: T } & DataHandler<T>) {
dataHandlers[appName] = { fetchData, hasData };
};
}

export function getDataHandler(appName: ObservabilityApp): DataHandler | undefined {
return dataHandlers[appName];
export function getDataHandler<T extends ObservabilityApp>(appName: T) {
const dataHandler = dataHandlers[appName];
if (dataHandler) {
return dataHandler as DataHandler<T>;
}
}
4 changes: 2 additions & 2 deletions x-pack/plugins/observability/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
Plugin as PluginClass,
PluginInitializerContext,
} from '../../../../src/core/public';
import { RegisterDataHandler, registerDataHandler } from './data_handler';
import { registerDataHandler } from './data_handler';

export interface ObservabilityPluginSetup {
dashboard: { register: RegisterDataHandler };
dashboard: { register: typeof registerDataHandler };
}

export type ObservabilityPluginStart = void;
Expand Down

0 comments on commit 33374c3

Please sign in to comment.