Last active
March 22, 2017 14:20
-
-
Save arjan/8a63899a6d3ba0573b697f59dfbcbe4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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