Current section

Files

Jump to
eventstore lib event_store writer.ex
Raw

lib/event_store/writer.ex

defmodule EventStore.Writer do
@moduledoc false
use EventStore.Registration
alias EventStore.RecordedEvent
alias EventStore.Storage
@doc """
Append the given list of recorded events to the stream
Returns `:ok` on success, or `{:error, reason}` on failure
"""
@spec append_to_stream(list(RecordedEvent.t), String.t) :: :ok | {:error, reason :: any()}
def append_to_stream(events, stream_uuid)
def append_to_stream([], _stream_uuid), do: :ok
def append_to_stream(events, stream_uuid) do
case Storage.append_to_stream(events) do
{:ok, assigned_event_ids} ->
events
|> assign_event_ids(assigned_event_ids)
|> publish_events(stream_uuid)
:ok
{:error, _reason} = reply -> reply
end
end
defp assign_event_ids(events, ids) do
events
|> Enum.zip(ids)
|> Enum.map(fn {event, id} ->
%RecordedEvent{event | event_id: id}
end)
end
defp publish_events(events, stream_uuid), do: @registry.publish_events(stream_uuid, events)
end