Mojentic.Agents.SimpleRecursiveAgent.EventEmitter (Mojentic v1.2.0)

Copy Markdown View Source

A GenServer-based event emitter that allows subscribing to and emitting events.

Subscribers receive events asynchronously via spawned tasks.

Summary

Functions

Returns a specification to start this module under a supervisor.

Emits an event to all subscribers asynchronously.

Starts the EventEmitter GenServer.

Subscribes to an event type.

Unsubscribes from events using the reference returned by subscribe.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

emit(pid, event)

@spec emit(
  pid(),
  struct()
) :: :ok

Emits an event to all subscribers asynchronously.

Parameters

  • pid - The EventEmitter process
  • event - The event struct to emit

Examples

EventEmitter.emit(emitter, %GoalSubmittedEvent{state: state})

start_link(opts \\ [])

Starts the EventEmitter GenServer.

subscribe(pid, event_type, callback)

@spec subscribe(pid(), module(), function()) :: reference()

Subscribes to an event type.

Returns a reference that can be used to unsubscribe.

Parameters

  • pid - The EventEmitter process
  • event_type - The event module to subscribe to
  • callback - Function to call when event is emitted

Examples

ref = EventEmitter.subscribe(emitter, GoalSubmittedEvent, fn event ->
  IO.inspect(event)
end)

EventEmitter.unsubscribe(emitter, ref)

unsubscribe(pid, ref)

@spec unsubscribe(pid(), reference()) :: :ok

Unsubscribes from events using the reference returned by subscribe.