Filament.Observable behaviour (filament v0.2.1)

Copy Markdown

Behaviour for observable GenServer processes.

A GenServer that uses Filament.Observable.GenServer automatically satisfies this behaviour. Implement the optional callbacks to customise subscription acceptance and teardown.

Summary

Callbacks

Called when a new subscriber requests a subscription.

Called when a subscriber unsubscribes or its process terminates.

Callbacks

handle_subscribe(subscriber, state)

(optional)
@callback handle_subscribe(
  subscriber :: term(),
  state :: term()
) ::
  {:ok, initial_value :: term(), new_state :: term()}
  | {:error, reason :: term(), new_state :: term()}

Called when a new subscriber requests a subscription.

Return {:ok, initial_value, new_state} to accept. initial_value is the raw server state sent back to the subscriber.

Return {:error, reason, new_state} to reject.

handle_unsubscribe(subscriber, state)

(optional)
@callback handle_unsubscribe(subscriber :: term(), state :: term()) ::
  {:ok, new_state :: term()}

Called when a subscriber unsubscribes or its process terminates.