feat(callstatelib): add a callstate publishable lib

This commit is contained in:
thomas
2022-12-07 16:03:05 +01:00
parent 278e513538
commit 1ec45ce141
19 changed files with 1477 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
import { CustomError, ErrorHandler } from './external.model';
export type LoadingState = 'INIT' | 'LOADING' | 'LOADED';
export const UNKNOWN_ERROR_CAUSE = 'UNKNOWN_ERROR';
export const UNKNOWN_ERROR_MESSAGE = 'unknown error occured';
export class CallStateError {
name: string;
message: string;
stack?: string;
constructor(name = '', message = '') {
this.name = name;
this.message = message;
}
}
export class CallStateErrorHandler implements ErrorHandler<CallStateError> {
toError = (error: unknown): CallStateError => {
if (error instanceof CallStateError) return error;
if (error instanceof Error)
return new CallStateError(error.name, error.message);
return new CallStateError(UNKNOWN_ERROR_CAUSE, UNKNOWN_ERROR_MESSAGE);
};
getErrorMessage = (error?: CallStateError): string | undefined => {
return error?.message;
};
}
export interface EsuiteError {
code: string;
}
export interface ErrorState {
error: CustomError;
}
export type CallState = LoadingState | ErrorState;
export const getErrorCallState = (
callState: CallState
): CustomError | undefined => {
if (isErrorState(callState)) {
return callState.error;
}
return undefined;
};
export const isLoadedOrInError = (callState: CallState): boolean =>
callState === 'LOADED' || isErrorState(callState);
export const isErrorState = (callState: CallState): callState is ErrorState =>
Object.prototype.hasOwnProperty.call(callState, 'error');