# `BB.Estimator.Source`
[🔗](https://github.com/beam-bots/bb/blob/main/lib/bb/estimator/source.ex#L5)

Where an estimator's inputs come from.

`BB.Estimator.Server` doesn't receive inputs itself; it asks a source for
them. The default, `BB.Estimator.Source.PubSub`, subscribes to a path and
treats every `%BB.Message{}` published there as an input. A sensor whose
readings don't travel over `BB.PubSub` names another source with
`c:BB.Sensor.estimator_source/0`, and the estimators nested inside it use
that instead.

The server owns the pace. It calls `c:request/1` once it is ready for an
input - after it has started, and again each time it has finished with what
the source last gave it - so a source that delivers by demand never hands an
estimator more than it has asked for. A source that can't be paced, such as
PubSub, makes `c:request/1` a no-op.

The server requests again only for an input, a `:spent` event or a
`{:failed, reason}` event, so a source must tell the message that answers a
request from one that merely accompanies it. Requesting on a message that
arrives alongside an input - ahead of it in the mailbox - would leave a
second request outstanding by the time the input is dispatched, and the
estimator would be handed an input it hadn't yet asked for.

# `event`

```elixir
@type event() ::
  {:input, BB.Message.t()}
  | :no_input
  | :spent
  | {:failed, reason :: term()}
  | {:ended, reason :: term()}
  | nil
```

What a message received by the estimator's process means to a source.

- `{:input, message}` - an input, handed to `c:BB.Estimator.handle_input/2`.
  The server measures `max_input_age` and latency from the envelope's
  `monotonic_time`, so a source whose messages are built some time after
  the reading was taken should stamp the time the reading was taken there.
  It refuses a message whose envelope names another `node`, because
  monotonic clocks are node-local.
- `:no_input` - the source's own message, carrying no input and leaving
  the outstanding request where it was. The server does nothing with it.
- `:spent` - the source's own message, which answered the outstanding
  request without an input - a stream pausing, say. The server requests
  again.
- `{:failed, reason}` - the source's own message, which answered the
  outstanding request with the news that whatever produces its inputs has
  failed - a camera whose capture broke, say. Unlike `:spent`, nothing is
  expected to follow soon. The server transitions the estimator to `:lost`
  with reason `:source_failed`, firing `on_lost`, and requests again, so
  inputs resuming recover it the way they would after `lost_after`.
- `{:ended, reason}` - the source will deliver nothing more; its
  subscription is dead. The server transitions the estimator to `:lost`
  with reason `:source_ended` and stops requesting, but doesn't stop
  itself. A source ends because its sensor has gone, and
  `BB.SensorEstimatorSupervisor` restarts a sensor's estimators after the
  sensor, so each subscribes afresh to whatever the new sensor provides.
- `nil` - not this source's message.

# `state`

```elixir
@type state() :: term()
```

Whatever a source needs to recognise its own messages. Opaque to the server.

# `input_event`

```elixir
@callback input_event(state(), message :: term()) :: event()
```

What `message`, received by the subscribing process, means for this source.

# `request`

```elixir
@callback request(state()) :: :ok
```

Ask for the next input.

# `subscribe`

```elixir
@callback subscribe(robot :: module(), path :: [atom()], args :: keyword()) ::
  {:ok, state()} | {:error, term()}
```

Start delivering to the calling process the inputs at `path` on `robot`.

`args` are whatever the sensor returned alongside the source module from
`c:BB.Sensor.estimator_source/0`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
