Skip to content

Commit

Permalink
Kill pending compilation tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelga committed Sep 29, 2023
1 parent 7648ec3 commit 8d21f38
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ defmodule NextLS.Runtime do
end

def handle_call({:compile, opts}, from, %{node: node} = state) do
stop_pending_compilations(state)

task =
Task.Supervisor.async_nolink(state.task_supervisor, fn ->
if opts[:force] do
Expand All @@ -224,14 +226,15 @@ defmodule NextLS.Runtime do
end
end)

{:noreply, %{state | compiler_refs: Map.put(state.compiler_refs, task.ref, from)}}
{:noreply, %{state | compiler_refs: Map.put(state.compiler_refs, task.ref, {task.pid, from})}}
end

@impl GenServer
def handle_info({ref, errors}, %{compiler_refs: compiler_refs} = state) when is_map_key(compiler_refs, ref) do
Process.demonitor(ref, [:flush])

GenServer.reply(compiler_refs[ref], errors)
orig = elem(compiler_refs[ref], 1)
GenServer.reply(orig, errors)

{:noreply, %{state | compiler_refs: Map.delete(compiler_refs, ref)}}
end
Expand Down Expand Up @@ -274,6 +277,12 @@ defmodule NextLS.Runtime do
{:noreply, state}
end

defp stop_pending_compilations(%{compiler_refs: %{}}), do: :ok
defp stop_pending_compilations(%{compiler_refs: refs}) do
for {_ref, {task_pid, _from}} <- refs, do: Process.exit(task_pid, :kill)
:ok
end

defp connect(_node, _port, 0) do
false
end
Expand Down

0 comments on commit 8d21f38

Please sign in to comment.