Skip to content

Instantly share code, notes, and snippets.

@arjan
Last active March 22, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arjan/8a63899a6d3ba0573b697f59dfbcbe4c to your computer and use it in GitHub Desktop.
Save arjan/8a63899a6d3ba0573b697f59dfbcbe4c to your computer and use it in GitHub Desktop.
defmodule Example.Utils.Multi do
@doc """
Intertwine multi calls with callbacks to a function to report progress information.
"""
def add_progress_calls(multi, fun) do
operations = multi.operations |> Enum.with_index()
size = Enum.count(operations)
new_operations =
Enum.reduce(operations, [], fn({op, index}, all) ->
percentage = (size - index) / size
progress = {["progress", index], {:run, fn(_) -> {:ok, fun.(percentage)} end}}
[progress, op | all]
end)
|> Enum.reverse()
%Ecto.Multi{operations: new_operations, names: MapSet.new(new_operations |> Enum.into(%{} |> Map.keys()))}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment