Extract logic into Task
This commit is contained in:
9
lib/mihainator/csv_parser.ex
Normal file
9
lib/mihainator/csv_parser.ex
Normal 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
|
||||||
@@ -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"
|
||||||
|
|||||||
Reference in New Issue
Block a user