diff --git a/lib/mihainator/csv_parser.ex b/lib/mihainator/csv_parser.ex new file mode 100644 index 0000000..a9a32d8 --- /dev/null +++ b/lib/mihainator/csv_parser.ex @@ -0,0 +1,9 @@ +defmodule Mihainator.CSVParser do + @moduledoc false + + def start(file) do + File.stream!(file) + |> Stream.map(&String.split(&1, ",")) + |> Enum.count() + end +end diff --git a/lib/mihainator_web/live/upload_live.ex b/lib/mihainator_web/live/upload_live.ex index 2622a0a..dd4aea4 100644 --- a/lib/mihainator_web/live/upload_live.ex +++ b/lib/mihainator_web/live/upload_live.ex @@ -19,18 +19,21 @@ defmodule MihainatorWeb.UploadLive do @impl Phoenix.LiveView def handle_event("save", _params, socket) do - uploaded_files = - consume_uploaded_entries(socket, :history, fn %{path: path}, _entry -> - line_count = - File.stream!(path) - |> Enum.count() + consume_uploaded_entries(socket, :history, fn %{path: path}, _entry -> + Task.async(fn -> Mihainator.CSVParser.start(path) end) - IO.puts(line_count) + {:ok, nil} + end) - {:ok, line_count} - end) + {:noreply, socket} + end - {:noreply, update(socket, :uploaded_files, &(&1 ++ uploaded_files))} + @impl Phoenix.LiveView + @spec handle_info({reference(), any()}, any()) :: {:noreply, any()} + def handle_info({ref, _result}, socket) do + Process.demonitor(ref, [:flush]) + + {:noreply, socket} end defp error_to_string(:too_large), do: "Too large"