import { ApiResponseChecker, ApiResponseHandler } from "../state"; import { CoTypedFactory } from "../../coroutines/builder"; import { AsyncState } from "../../../main"; export const HandleApiResponse = ( asyncAccessor: (_: State & Context) => AsyncState, handlers: ApiResponseHandler, ) => { const CheckerCo = CoTypedFactory(); return CheckerCo.GetState().then((_) => { const asyncState = asyncAccessor(_); return AsyncState.Operations.isLoading(asyncState) ? CheckerCo.Do(() => {}) : CheckerCo.Do(() => { return AsyncState.Operations.status(asyncState) === "error" ? handlers.handleError?.( asyncState.kind === "error" ? asyncState.error : undefined, ) : handlers.handleSuccess?.(_); }); }); };