BB.Parameter.Store behaviour (bb v0.15.2)

Copy Markdown View Source

Behaviour for parameter persistence backends.

Implementations handle loading and saving parameter values to durable storage. The store is initialized when a robot starts and closed when it stops.

Lifecycle

  1. init/2 - Called during robot startup with module name and options
  2. load/1 - Called to retrieve all persisted parameters
  3. save/3 - Called after each successful parameter change
  4. close/1 - Called during robot shutdown

Built-in Implementations

Example Implementation

defmodule MyApp.ParameterStore do
  @behaviour BB.Parameter.Store

  @impl true
  def init(robot_module, opts) do
    # Initialize storage connection
    {:ok, %{robot: robot_module, conn: connect(opts)}}
  end

  @impl true
  def load(state) do
    # Return all stored parameters
    {:ok, fetch_all(state.conn)}
  end

  @impl true
  def save(state, path, value) do
    # Persist a parameter change
    :ok = write(state.conn, path, value)
    :ok
  end

  @impl true
  def close(state) do
    # Clean up resources
    disconnect(state.conn)
    :ok
  end
end

Summary

Callbacks

Close the parameter store.

Initialize the parameter store.

Load all persisted parameters.

Save a parameter value.

Types

path()

@type path() :: [atom()]

state()

@type state() :: term()

value()

@type value() :: term()

Callbacks

close(state)

@callback close(state()) :: :ok

Close the parameter store.

Called during robot shutdown. Implementations should release any resources (file handles, connections, etc.).

init(robot_module, opts)

@callback init(robot_module :: module(), opts :: keyword()) ::
  {:ok, state()} | {:error, term()}

Initialize the parameter store.

Called during robot startup. The robot_module identifies which robot this store is for (useful for multi-robot setups). Options come from the DSL configuration.

Returns {:ok, state} where state is passed to subsequent callbacks, or {:error, reason} if initialization fails.

load(state)

@callback load(state()) :: {:ok, [{path(), value()}]} | {:error, term()}

Load all persisted parameters.

Called after initialization to retrieve previously saved parameter values. Returns a list of {path, value} tuples.

These values are applied after DSL defaults, so persisted values take precedence over defaults.

save(state, path, value)

@callback save(state(), path(), value()) :: :ok | {:error, term()}

Save a parameter value.

Called after each successful BB.Parameter.set/3 operation. The implementation should persist the value durably.