36 lines · 1.2 KB · TypeScript
RawDownload
import { useAtomRefresh, useAtomValue } from "@effect/atom-react";import * as Cause from "effect/Cause";import * as Option from "effect/Option";import { AsyncResult, Atom } from "effect/unstable/reactivity"; const EMPTY_ASYNC_RESULT_ATOM = Atom.make(AsyncResult.initial<never, never>(false)).pipe(  Atom.withLabel("mobile-environment-query:empty"),); export interface EnvironmentQueryView<A> {  readonly data: A | null;  readonly error: string | null;  readonly isPending: boolean;  readonly refresh: () => void;} function formatError(cause: Cause.Cause<unknown>): string {  const error = Cause.squash(cause);  return error instanceof Error && error.message.trim().length > 0    ? error.message    : "The environment request failed.";} export function useEnvironmentQuery<A, E>(  atom: Atom.Atom<AsyncResult.AsyncResult<A, E>> | null,): EnvironmentQueryView<A> {  const selectedAtom = atom ?? EMPTY_ASYNC_RESULT_ATOM;  const result = useAtomValue(selectedAtom);  const refresh = useAtomRefresh(selectedAtom);  return {    data: Option.getOrNull(AsyncResult.value(result)),    error: result._tag === "Failure" ? formatError(result.cause) : null,    isPending: atom !== null && result.waiting,    refresh,  };}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t