# Lifecycles

> Read when state comes from outside Pinna. Start a listener when a value gains its first observer and stop it after the last, stand for outside state with an atom, and hold a value observed.

## Example

An observable is observed while something holds it: a watch, an autorun, a
mounted `useValue`, `observe`, or an observed computed value that read it.
`onObserved` runs on the first observer, and its cleanup after the last.

```ts
import { box, createRealm, onObserved, update } from "@frondruntime/pinna";

const realm = createRealm();

const $online = box(realm, true);

// Listens to the browser only while something observes $online.
onObserved($online, () => {
  const sync = () => {
    update(realm, (_read, write) => write($online, navigator.onLine));
  };
  sync(); // the value may have changed while no one listened
  window.addEventListener("online", sync);
  window.addEventListener("offline", sync);
  return () => {
    window.removeEventListener("online", sync);
    window.removeEventListener("offline", sync);
  };
});
```

An atom stands for state kept outside Pinna. `read($atom)` subscribes;
`write.changed($atom)`, inside an update, reruns what observed it.

```ts
import { atom, computed, onObserved, update } from "@frondruntime/pinna";

// The clock is state kept outside Pinna: the atom stands for it.
const $clock = atom(realm);

onObserved($clock, () => {
  const timer = setInterval(() => {
    update(realm, (_read, write) => write.changed($clock));
  }, 1000);
  return () => clearInterval(timer);
});

const $time = computed(realm, (read) => {
  read($clock);
  return new Date().toLocaleTimeString();
});
```

`observe` holds a value observed with nothing rendered.

```ts
import { observe } from "@frondruntime/pinna";

// The clock ticks, and $time stays cached, until release runs.
const release = observe($time);
```

## Rules

- A hook may write, in an update. It runs once every link of the call that
  observed is in place, before that call returns. Inside an update, hooks
  start and stop once the recipe returns, never in the middle of it.
- An observer holds what it read until it stops: a watch or an autorun until
  its stop function runs or its next run stops reading the value, `observe` until its release runs, `useValue` and
  `useValues` until the component unmounts, `watchScoped`, `observeScoped`,
  and `changes` until the scope closes or the stream stops, and an observed
  computed value until it is no longer observed or a recompute stops reading
  the value. `untracked` and an update read without observing.
- A source lost and gained within one call or one update fires nothing, so a
  watcher that swaps its sources does not restart a subscription.
- A hook's error, or its cleanup's, goes to the realm's `onError`, never to
  the caller. The caller always gets its release.
- Nothing reports what outside state did while no one observed it, so a hook
  syncs the state when it starts, as `sync()` does above.
- An atom nothing observes counts as changed on every check. A computed value
  that reads such an atom recomputes on every read; observed, it recomputes
  only after `write.changed`.

## Answers

| Call | Answers |
| --- | --- |
| `onObserved($x, () => cleanup)` | The function that removes the hook. The function passed in returns the cleanup. |
| `atom(realm)` | An atom. `read($atom)` returns `undefined`. |
| `write.changed($atom)` | Reruns what observed the atom. |
| `observe($x)` | A release. Until it runs, `$x`'s hooks stay started and a computed value stays cached. |

## See also

- [Reacting](./reacting.md): watches and autoruns.
- [Programs](../effect/programs.md): `fromStream`, a value fed by a stream while observed.
- [Live data](../guides/live-data.md): a WebSocket opened while something observes the feed.
