Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/fable/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,31 @@ defmodule Fable.Events do
Keyword.t()
) ::
transation_fun
def emit(config, %{__meta__: %Ecto.Schema.Metadata{}} = aggregate, fun, changes \\ %{}, opts)
when is_function(fun, 3) do
def emit(config, %{__meta__: %Ecto.Schema.Metadata{}} = aggregate, fun, changes \\ %{}, opts) do
check_schema_fields(aggregate)

fn ->
aggregate = lock(aggregate, config.repo) || aggregate
events = fun.(aggregate, config.repo, changes)
result_of_applied_events = handle_events(config, aggregate, events, opts)
rollback_on_error(config.repo, result_of_applied_events)

case call_fun(fun, aggregate, config, changes) do
{:error, reason} ->
config.repo.rollback(reason)

events ->
result_of_applied_events = handle_events(config, aggregate, events, opts)
rollback_on_error(config.repo, result_of_applied_events)
end
end
end

defp call_fun(fun, aggregate, config, _changes) when is_function(fun, 2) do
fun.(aggregate, config.repo)
end

defp call_fun(fun, aggregate, config, changes) when is_function(fun, 3) do
fun.(aggregate, config.repo, changes)
end

@doc false
@spec emit(
Fable.Config.t(),
Expand All @@ -261,8 +274,7 @@ defmodule Fable.Events do
Keyword.t()
) ::
Ecto.Multi.t()
def emit(%{repo: repo} = config, %Ecto.Multi{} = multi, aggregate, name, fun, opts)
when is_function(fun, 3) do
def emit(%{repo: repo} = config, %Ecto.Multi{} = multi, aggregate, name, fun, opts) do
Ecto.Multi.run(multi, name, fn ^repo, changes ->
emit(config, aggregate, fun, changes, opts)
|> repo.transaction()
Expand Down