Skip to content

Commit

Permalink
Reindex all renamed files after renaming done
Browse files Browse the repository at this point in the history
  • Loading branch information
scottming committed Jun 15, 2024
1 parent 13accf2 commit d72a46f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
8 changes: 0 additions & 8 deletions apps/remote_control/lib/lexical/remote_control/api/proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ defmodule Lexical.RemoteControl.Api.Proxy do
RemoteControl.Dispatch.broadcast(message)
end

def broadcast(file_changed() = message) do
RemoteControl.Dispatch.broadcast(message)
end

def broadcast(file_opened() = message) do
RemoteControl.Dispatch.broadcast(message)
end

def broadcast(message) do
mfa = to_mfa(RemoteControl.Dispatch.broadcast(message))
:gen_statem.call(__MODULE__, buffer(contents: mfa))
Expand Down
35 changes: 21 additions & 14 deletions apps/remote_control/lib/lexical/remote_control/commands/rename.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ defmodule Lexical.RemoteControl.Commands.Rename do
# Therefore, we need this module to tell us if lexical is currently in the process of renaming.

alias Lexical.RemoteControl.Api.Messages
alias Lexical.RemoteControl.Commands.Reindex
require Logger
import Messages

defmodule State do
@type uri_to_expected_operation :: %{
Lexical.uri() => Messages.file_changed() | Messages.file_saved()
}

@type t :: %__MODULE__{
uri_to_expected_operation: %{
Lexical.uri() => Messages.file_changed() | Messages.file_saved()
},
uri_to_expected_operation: uri_to_expected_operation(),
raw_uri_to_expected_operation: uri_to_expected_operation(),
on_update_progess: fun(),
on_complete: fun()
}
defstruct uri_to_expected_operation: %{}, on_update_progess: nil, on_complete: nil
defstruct uri_to_expected_operation: %{},
raw_uri_to_expected_operation: %{},
on_update_progess: nil,
on_complete: nil

def new(uri_to_expected_operation, on_update_progess, on_complete) do
%__MODULE__{
uri_to_expected_operation: uri_to_expected_operation,
raw_uri_to_expected_operation: uri_to_expected_operation,
on_update_progess: on_update_progess,
on_complete: on_complete
}
Expand All @@ -45,6 +53,7 @@ defmodule Lexical.RemoteControl.Commands.Rename do

if Enum.empty?(new_uri_with_expected_operation) do
state.on_complete.()
reindex_all_renamed_files(state)
end

%__MODULE__{state | uri_to_expected_operation: new_uri_with_expected_operation}
Expand All @@ -64,6 +73,12 @@ defmodule Lexical.RemoteControl.Commands.Rename do
uri_to_operation
end
end

defp reindex_all_renamed_files(%__MODULE__{} = state) do
state.raw_uri_to_expected_operation
|> Map.keys()
|> Enum.each(&Reindex.uri/1)
end
end

alias Lexical.RemoteControl.Api.Proxy
Expand Down Expand Up @@ -99,20 +114,12 @@ defmodule Lexical.RemoteControl.Commands.Rename do
# Instead, it should call this function to synchronously update the status,
# thus preventing failures due to latency issues.
def update_progress(message) do
if in_progress?() do
GenServer.cast(__MODULE__, {:update_progress, message})
else
:ok
end
end

def in_progress? do
pid = Process.whereis(__MODULE__)

if pid && Process.alive?(pid) do
GenServer.call(__MODULE__, :in_progress?)
GenServer.cast(__MODULE__, {:update_progress, message})
else
false
{:error, :not_in_rename_progress}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
on_complete
)

assert Rename.in_progress?()
assert_called(Proxy.start_buffering())
end

Expand Down Expand Up @@ -86,11 +85,11 @@ defmodule Lexical.RemoteControl.Commands.RenameTest do
Rename.update_progress(file_changed(uri: uri1))
assert_receive {:update_progress, 1, ""}
refute_receive :complete_progress
assert Rename.in_progress?()
end

test "it should return `:ok` when updating the progress if the process is not alive" do
assert :ok = Rename.update_progress(file_changed(uri: "file://file.ex"))
test "it should return :error when updating the progress if the process is not alive" do
assert {:error, :not_in_rename_progress} =
Rename.update_progress(file_changed(uri: "file://file.ex"))
end

defp update_progress(pid, delta, message) do
Expand Down

0 comments on commit d72a46f

Please sign in to comment.