@@ -100,11 +100,8 @@ defmodule LiveDebugger.GenServers.EtsTableServer do
100100 @ impl true
101101 def handle_call ( { :watch , pid } , { watcher , _ } , state ) do
102102 case Map . get ( state , pid ) do
103- % TableInfo { watchers: watchers } = table_info ->
104- updated_watchers = MapSet . put ( watchers , watcher )
105-
106- updated_state =
107- Map . put ( state , pid , % { table_info | watchers: updated_watchers } )
103+ % TableInfo { } ->
104+ updated_state = update_watchers ( state , pid , & MapSet . put ( & 1 , watcher ) )
108105
109106 Process . monitor ( watcher )
110107
@@ -144,27 +141,32 @@ defmodule LiveDebugger.GenServers.EtsTableServer do
144141 defp maybe_delete_ets_table ( state , pid ) do
145142 with { % TableInfo { alive?: false } = table_info , updated_state } <- Map . pop ( state , pid ) ,
146143 true <- Enum . empty? ( table_info . watchers ) do
144+ PubSubUtils . process_status_topic ( )
145+ |> PubSubUtils . broadcast ( { :process_status , { :not_watched , pid } } )
146+
147147 :ets . delete ( table_info . table )
148- IO . inspect ( pid , label: "DELETING TABLE" )
149148 updated_state
150149 else
151150 _ ->
152151 state
153152 end
154153 end
155154
156- @ spec remove_watcher ( state ( ) , pid ( ) ) :: { state ( ) , [ pid ( ) ] }
155+ @ spec remove_watcher ( state ( ) , pid ( ) ) :: { updated_state :: state ( ) , touched_pids :: [ pid ( ) ] }
157156 defp remove_watcher ( state , watcher ) when is_pid ( watcher ) do
158- Enum . reduce ( state , { state , [ ] } , fn { pid , % { watchers: watchers } = table_info } ,
159- { state_acc , pids } ->
157+ Enum . reduce ( state , { state , [ ] } , fn { pid , % { watchers: watchers } } , { state_acc , pids } ->
160158 if Enum . member? ( watchers , watcher ) do
161- updated_state =
162- Map . put ( state_acc , pid , % { table_info | watchers: MapSet . delete ( watchers , watcher ) } )
159+ updated_state = update_watchers ( state_acc , pid , & MapSet . delete ( & 1 , watcher ) )
163160
164161 { updated_state , [ pid | pids ] }
165162 else
166163 { state_acc , pids }
167164 end
168165 end )
169166 end
167+
168+ defp update_watchers ( state , pid , update_fn ) when is_map_key ( state , pid ) do
169+ table_info = Map . get ( state , pid )
170+ Map . put ( state , pid , % { table_info | watchers: update_fn . ( table_info . watchers ) } )
171+ end
170172end
0 commit comments