Raxol.Core.Runtime.EventSource behaviour (Raxol v0.3.0)
View SourceBehaviour 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
@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 messagestate
- The current state
Returns
{:noreply, new_state}
- Continue with new state{:stop, reason, new_state}
- Stop the event source
@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 toSubscription.custom/2
context
- The runtime context containing the subscriber pid
Returns
{:ok, state}
- Successfully initialized with state{:error, reason}
- Failed to initialize
Called when the event source is stopping. Can be used to clean up resources.
Parameters
reason
- The reason for stoppingstate
- The current state
Returns
:ok