Types: Adds type safety to appEvents (#19418)

* Types: Add type safety to appEvents
This commit is contained in:
kay delaney
2019-10-14 09:27:47 +01:00
committed by GitHub
parent e7c37cc316
commit 99411bf37a
138 changed files with 991 additions and 508 deletions

View File

@@ -0,0 +1,4 @@
export interface AppEvent<T> {
readonly name: string;
payload?: T;
}

View 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');

View File

@@ -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 };

View File

@@ -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 };
}