Current section

Files

Jump to
hemdal lib hemdal event mock.ex
Raw

lib/hemdal/event/mock.ex

defmodule Hemdal.Event.Mock do
@moduledoc """
Consume the events generated by the system and send all of these events
to the process which run `start_link/0`. The even is `Hemdal.Event.t()`.
"""
use GenStage
@event_manager Hemdal.Event
@doc """
It is starting a consumer, linked to the caller process, and sending
all of the events to the caller process.
"""
@spec start_link() :: {:ok, pid()}
def start_link do
{:ok, _pid} = GenStage.start_link(__MODULE__, [self()])
end
@doc """
Stop the consumer, while it's linked to the caller process it's a good
idea to stop it.
"""
@spec stop(GenServer.server()) :: :ok
defdelegate stop(pid), to: GenStage
@impl GenStage
@doc false
def init([pid]) do
{:consumer, pid, subscribe_to: [@event_manager]}
end
@impl true
@doc false
def handle_events(events, from, pid) do
Enum.each(events, &send(pid, {:event, from, &1}))
{:noreply, [], pid}
end
end