Raxol.Core.Runtime.EventSource behaviour (Raxol v0.5.0)

View Source

Behaviour for implementing custom event sources for subscriptions.

Event sources are processes that can generate events over time and send them to subscribers. This behaviour defines the contract that custom event sources must implement.

Example

defmodule MyEventSource do
  use Raxol.Core.Runtime.EventSource

  @impl true
  def init(args, context) do
    # Set up initial state
    {:ok, %{args: args, context: context}}
  end

  @impl true
  def handle_info(:tick, state) do
    # Send an event to the subscriber
    send_event(state.context, {:my_event, :data})
    {:noreply, state}
  end
end

Summary

Callbacks

Called when the event source receives a message. Should handle the message and optionally send events to the subscriber.

Called when the event source is started. Should return the initial state.

Called when the event source is stopping. Can be used to clean up resources.

Callbacks

handle_info(msg, state)

@callback handle_info(msg :: term(), state :: term()) ::
  {:noreply, new_state :: term()}
  | {:stop, reason :: term(), new_state :: term()}

Called when the event source receives a message. Should handle the message and optionally send events to the subscriber.

Parameters

  • msg - The received message
  • state - The current state

Returns

  • {:noreply, new_state} - Continue with new state
  • {:stop, reason, new_state} - Stop the event source

init(args, context)

@callback init(args :: term(), context :: map()) ::
  {:ok, state :: term()} | {:error, reason :: term()}

Called when the event source is started. Should return the initial state.

Parameters

  • args - The arguments passed to Subscription.custom/2
  • context - The runtime context containing the subscriber pid

Returns

  • {:ok, state} - Successfully initialized with state
  • {:error, reason} - Failed to initialize

terminate(reason, state)

(optional)
@callback terminate(reason :: term(), state :: term()) :: :ok

Called when the event source is stopping. Can be used to clean up resources.

Parameters

  • reason - The reason for stopping
  • state - The current state

Returns

  • :ok