Stop screen pid when screen process changes

This commit is contained in:
Simponic 2023-01-20 23:14:37 -07:00
parent 06f1ca7603
commit 5dbe76f6c2
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
2 changed files with 19 additions and 15 deletions

View File

@ -21,13 +21,17 @@ defmodule Chessh.SSH.Client do
screen_state_initials: [] screen_state_initials: []
end end
@impl true def link_menu_screen(player_session) do
def init([%State{player_session: player_session} = state]) do
send( send(
self(), self(),
{:set_screen_process, Chessh.SSH.Client.MainMenu, {:set_screen_process, Chessh.SSH.Client.MainMenu,
%Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}} %Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}
) )
end
@impl true
def init([%State{player_session: player_session} = state]) do
link_menu_screen(player_session)
{:ok, state} {:ok, state}
end end
@ -38,14 +42,9 @@ defmodule Chessh.SSH.Client do
%State{ %State{
width: width, width: width,
height: height, height: height,
screen_state_initials: screen_state_initials, screen_state_initials: screen_state_initials
screen_pid: screen_pid
} = state } = state
) do ) do
if screen_pid do
Process.unlink(screen_pid)
end
{:ok, new_screen_pid} = {:ok, new_screen_pid} =
GenServer.start_link(module, [%{screen_state_initial | client_pid: self()}]) GenServer.start_link(module, [%{screen_state_initial | client_pid: self()}])
@ -95,7 +94,8 @@ defmodule Chessh.SSH.Client do
%State{ %State{
width: width, width: width,
height: height, height: height,
screen_pid: screen_pid screen_pid: screen_pid,
player_session: player_session
} = state } = state
) do ) do
case keymap(data) do case keymap(data) do
@ -103,7 +103,10 @@ defmodule Chessh.SSH.Client do
{:stop, :normal, state} {:stop, :normal, state}
:previous_screen -> :previous_screen ->
{:noreply, go_back_one_screen(state)} GenServer.stop(screen_pid)
link_menu_screen(player_session)
{:noreply, state}
action -> action ->
send(screen_pid, {:input, width, height, action}) send(screen_pid, {:input, width, height, action})
@ -185,6 +188,7 @@ defmodule Chessh.SSH.Client do
defp go_back_one_screen( defp go_back_one_screen(
%State{ %State{
screen_pid: screen_pid,
screen_state_initials: [_ | rest_initial] screen_state_initials: [_ | rest_initial]
} = state, } = state,
previous_state previous_state
@ -197,10 +201,10 @@ defmodule Chessh.SSH.Client do
if(is_nil(previous_state), do: prev_state_initial, else: previous_state)} if(is_nil(previous_state), do: prev_state_initial, else: previous_state)}
) )
if screen_pid do
GenServer.stop(screen_pid)
end
%State{state | screen_state_initials: rest_initial} %State{state | screen_state_initials: rest_initial}
end end
defp go_back_one_screen(%State{} = state) do
go_back_one_screen(state, nil)
end
end end

View File

@ -6,7 +6,7 @@ defmodule Chessh.SSH.Client.SelectJoinableGame do
use Chessh.SSH.Client.SelectPaginatePoller use Chessh.SSH.Client.SelectPaginatePoller
def refresh_options_ms(), do: 4000 def refresh_options_ms(), do: 4000
def max_displayed_options(), do: 1 def max_displayed_options(), do: 10
def title(), do: ["-- Joinable Games --"] def title(), do: ["-- Joinable Games --"]
def dynamic_options(), do: true def dynamic_options(), do: true