mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Types: Adds type safety to appEvents (#19418)
* Types: Add type safety to appEvents
This commit is contained in:
4
packages/grafana-data/src/types/appEvent.ts
Normal file
4
packages/grafana-data/src/types/appEvent.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface AppEvent<T> {
|
||||
readonly name: string;
|
||||
payload?: T;
|
||||
}
|
||||
7
packages/grafana-data/src/types/events.ts
Normal file
7
packages/grafana-data/src/types/events.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { eventFactory } from './utils';
|
||||
|
||||
export type AlertPayload = [string, string?];
|
||||
|
||||
export const alertSuccess = eventFactory<AlertPayload>('alert-success');
|
||||
export const alertWarning = eventFactory<AlertPayload>('alert-warning');
|
||||
export const alertError = eventFactory<AlertPayload>('alert-error');
|
||||
@@ -13,3 +13,7 @@ export * from './graph';
|
||||
export * from './ScopedVars';
|
||||
export * from './transformations';
|
||||
export * from './vector';
|
||||
export * from './appEvent';
|
||||
|
||||
import * as AppEvents from './events';
|
||||
export { AppEvents };
|
||||
|
||||
@@ -1,2 +1,14 @@
|
||||
import { AppEvent } from './appEvent';
|
||||
|
||||
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
|
||||
export type Subtract<T, K> = Omit<T, keyof K>;
|
||||
|
||||
const typeList: Set<string> = new Set();
|
||||
export function eventFactory<T = undefined>(name: string): AppEvent<T> {
|
||||
if (typeList.has(name)) {
|
||||
throw new Error(`There is already an event defined with type '${name}'`);
|
||||
}
|
||||
|
||||
typeList.add(name);
|
||||
return { name };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user