Core

Resources

A resource is a computed whose computation can return a promise. Reading it while the promise is pending throws a ResourceNotReadyError, so a caller can wait or show something else.

Creating one

TypeScript
import { atom, resource } from 'atemno';

const userId = atom('userId', 1);

const user = resource<User>('user', async $ => {
  const response = await fetch(`/api/users/${$.get(userId)}`);
  return response.json();
});

A resource is lazy and cached like a computed. It runs on the first read, and again on the next read after a dependency changes. If something observes it, it reruns during the write instead.

User loader
Larger ids answer sooner, so stepping quickly makes an old request finish after a newer one. Its result is dropped. User 4 does not exist, so its request rejects.

Reading one

Reading a resource has three outcomes.

  • While the promise is pending, it throws a ResourceNotReadyError. Its request property is the promise, so the caller can wait for it.
  • Once the promise resolves, it returns the value.
  • Once the promise rejects, it rethrows the reason on every read, until a dependency changes and it runs again.

A computation that returns a plain value instead of a promise resolves at once.

TypeScript
import { ResourceNotReadyError } from 'atemno';

try {
  render(domain.get(user));
} catch (error) {
  if (error instanceof ResourceNotReadyError) {
    showSpinner();
    error.request.then(() => render(domain.get(user)));
  } else {
    throw error;
  }
}

domain.get(toResult(user)) does the same without the try. It is described below.

Only reads before the first await are tracked

A resource records the dependencies it reads while its computation runs synchronously, up to the first await. Reads after that still return the current value, but they are not dependencies, so changing them does not rerun the resource.

TypeScript
const user = resource<User>('user', async $ => {
  const id = $.get(userId); // tracked
  const response = await fetch(`/api/users/${id}`);
  const token = $.get(session); // read, not tracked
  return parse(response, token);
});

Read everything the request depends on first. This applies to plain computeds too, which should not be async at all.

The latest run wins

Each run replaces the one before it. When an older request settles after a newer one started, its result is dropped. Use onCleanup to cancel the request as well, which runs before the next run and when the node is released.

TypeScript
const user = resource<User>('user', async $ => {
  const controller = new AbortController();
  $.onCleanup(() => controller.abort());
  const response = await fetch(`/api/users/${$.get(userId)}`, {
    signal: controller.signal,
  });
  return response.json();
});

Keeping the previous value

By default a resource that reruns is pending again, so anything reading it waits, and React shows the Suspense fallback. Pass keepPrevious to keep returning the last resolved value until the new one arrives.

TypeScript
const user = resource<User>('user', async $ => fetchUser($.get(userId)), {
  keepPrevious: true,
});
  • The first run is still pending, since there is no value to keep yet.
  • isRefreshing(user) is true while a newer request is in flight. isPending(user) stays false, because reads succeed.
  • A rejection replaces the kept value. Reads then rethrow it, as usual.
  • Computeds reading the resource keep their value too, until the new one arrives.

The third argument is either an equality check, as before, or an options object with isEqual and keepPrevious.

Refetching

domain.reset(user) runs a resource again without changing anything it reads. If something observes it, the new request starts straight away. In React, useResetSource(user) returns a callback that does the same.

TSX
function Reload() {
  const reload = useResetSource(user);
  return <button onClick={reload}>Reload</button>;
}

A resource with keepPrevious keeps returning its last value while the new request runs, and isRefreshing(user) is true until it settles.

Downstream of a resource

A computed that reads a pending resource throws the same ResourceNotReadyError, and so does anything reading that computed. When the resource settles, they rerun like after any other change.

An observer is not called while its source is pending, and pending is not an error, so onError does not hear about it. The observer is called once the resource settles. A rejection goes to onError like any other error from a read.

To show every state, loading included, observe toResult(user) instead. It changes on each step, so its observer is called for all of them. The demo above does this.

TypeScript
domain.observe(toResult(user), result => render(result));

Waiting on several

The helpers take sources and return a computed, which you read like any other: $.get, domain.get or useComputedValue.

HelperHoldsPending whileThrows
toResult(source){ type, value } or { type, request }never, it reports itnever
isPending(source)whether the source is pendingnever, it reports itnever
isRefreshing(source)whether a keepPrevious resource is rerunningnever, it reports itnever
waitForNone(sources)one result per sourcenever, it reports itnever
waitForAll(sources)every valueany is pendingan AggregateError if any failed
waitForAny(sources)the first value, in list order, that resolvednone resolved and any is pendingan AggregateError if all failed
waitForRace(sources)the first outcome, in list order, that settledall are pendingthe error, if that outcome is a failure
TypeScript
import { computed, waitForAll } from 'atemno';

const profile = computed<string>('profile', $ => {
  const [owner, prefs] = $.get(waitForAll([account, settings]));
  return `${owner.name} (${prefs.theme})`;
});

Each call with the same sources returns the same description, named after them, such as waitForAll(resource:account, resource:settings). That keeps it cheap to call during render and readable in the devtools graph.

In React

useComputedValue suspends while a resource is pending, so the nearest Suspense boundary shows its fallback, and a rejection goes to the nearest error boundary. See React hooks.