[]previous and next page

Resource

Reactive handle to an asynchronous value.

Reactive handle to an asynchronous value.

Import#

ts
import type { Resource } from "@shadow-js/core";

Signature#

ts
interface Resource<T> extends Accessor<T | undefined> {
  /** The current {@link ResourceState}. Tracked when read in an effect or memo. */
  readonly state: ResourceState;
  /** True while a fetch is in flight (`"pending"` or `"refreshing"`). Tracked. */
  readonly loading: boolean;
  /** The error of the last fetch, or `undefined` when it did not fail. Tracked; never throws. */
  readonly error: unknown;
  /** The last successfully loaded value; unlike the call form it never throws or suspends. */
  readonly latest: T | undefined;
  /**
   * Fetches again (keeping the previous value visible). The promise resolves with the new value,
   * or with `undefined` when the fetch failed (see `error`) or a newer one replaced it; it never
   * rejects, so a call you do not await is safe.
   */
  refetch(): Promise<T | undefined>;
  /** Replaces the value without fetching, cancels a fetch in flight, and returns the new value. */
  mutate(value: T | ((previous: T | undefined) => T)): T;
}

Members#

Name Type Description
T T
state readonly state: ResourceState The current ResourceState. Tracked when read in an effect or memo.
loading readonly loading: boolean True while a fetch is in flight ("pending" or "refreshing"). Tracked.
error readonly error: unknown The error of the last fetch, or undefined when it did not fail. Tracked; never throws.
latest readonly latest: T | undefined The last successfully loaded value; unlike the call form it never throws or suspends.
refetch refetch(): Promise<T | undefined> Fetches again (keeping the previous value visible). The promise resolves with the new value, or with undefined when the fetch failed (see error) or a newer one replaced it; it never rejects, so a call you do not await is safe.
mutate mutate(value: T | ((previous: T | undefined) => T)): T Replaces the value without fetching, cancels a fetch in flight, and returns the new value.