Extract logic into Task

This commit is contained in:
2024-10-10 22:49:28 +02:00
parent bdacbd792f
commit e235475118
2 changed files with 21 additions and 9 deletions

View File

@@ -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

View File

@@ -19,18 +19,21 @@ defmodule MihainatorWeb.UploadLive do
@impl Phoenix.LiveView @impl Phoenix.LiveView
def handle_event("save", _params, socket) do def handle_event("save", _params, socket) do
uploaded_files =
consume_uploaded_entries(socket, :history, fn %{path: path}, _entry -> consume_uploaded_entries(socket, :history, fn %{path: path}, _entry ->
line_count = Task.async(fn -> Mihainator.CSVParser.start(path) end)
File.stream!(path)
|> Enum.count()
IO.puts(line_count) {:ok, nil}
{:ok, line_count}
end) end)
{:noreply, update(socket, :uploaded_files, &(&1 ++ uploaded_files))} {:noreply, socket}
end
@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 end
defp error_to_string(:too_large), do: "Too large" defp error_to_string(:too_large), do: "Too large"