Alerting: Update useAsync to typeguard states (#98884)

pull/98889/head
Gilles De Mey 6 months ago committed by GitHub
parent 9ec10be1c7
commit 4aa29c79a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 58
      public/app/features/alerting/unified/hooks/useAsync.tsx

@ -8,26 +8,34 @@ import { stringifyErrorLike } from '../utils/misc';
export type AsyncStatus = 'loading' | 'success' | 'error' | 'not-executed'; export type AsyncStatus = 'loading' | 'success' | 'error' | 'not-executed';
export type AsyncState<Result> = export type AsyncState<Result> =
| { | AsyncStateUninitialized<Result>
status: 'not-executed'; | AsyncStateFulfilled<Result>
error: undefined; | AsyncStateWithError<Result>
result: Result; | AsyncStateLoading<Result>;
}
| { export type AsyncStateWithError<Result> = {
status: 'success'; status: 'error';
error: undefined; error: Error;
result: Result; result: Result;
} };
| {
status: 'error'; export type AsyncStateFulfilled<Result> = {
error: Error; status: 'success';
result: Result; error: undefined;
} result: Result;
| { };
status: AsyncStatus;
error: Error | undefined; export type AsyncStateUninitialized<Result> = {
result: Result; status: 'not-executed';
}; error: undefined;
result: Result;
};
export type AsyncStateLoading<Result> = {
status: 'loading';
error: Error | undefined;
result: Result;
};
export type UseAsyncActions<Result, Args extends unknown[] = unknown[]> = { export type UseAsyncActions<Result, Args extends unknown[] = unknown[]> = {
/** /**
@ -157,19 +165,19 @@ function useSyncedRef<T>(value: T): { readonly current: T } {
// --- utility functions to help with request state assertions --- // --- utility functions to help with request state assertions ---
export function isError<T>(state: AsyncState<T>) { export function isError<T>(state: AsyncState<unknown>): state is AsyncStateWithError<T> {
return state.status === 'error'; return state.status === 'error';
} }
export function isSuccess<T>(state: AsyncState<T>) { export function isSuccess<T>(state: AsyncState<T>): state is AsyncStateFulfilled<T> {
return state.status === 'success'; return state.status === 'success';
} }
export function isUninitialized<T>(state: AsyncState<T>) { export function isUninitialized<T>(state: AsyncState<T>): state is AsyncStateUninitialized<T> {
return state.status === 'not-executed'; return state.status === 'not-executed';
} }
export function isLoading<T>(state: AsyncState<T>) { export function isLoading<T>(state: AsyncState<T>): state is AsyncStateLoading<T> {
return state.status === 'loading'; return state.status === 'loading';
} }
@ -185,7 +193,7 @@ export function anyOfRequestState(...states: Array<AsyncState<unknown>>) {
/** /**
* This is only used for testing and serializing the async state * This is only used for testing and serializing the async state
*/ */
export function SerializeState({ state }: { state: AsyncState<unknown> }) { export function SerializeState<T>({ state }: { state: AsyncState<T> }) {
return ( return (
<> <>
{isUninitialized(state) && 'uninitialized'} {isUninitialized(state) && 'uninitialized'}

Loading…
Cancel
Save