Parcourir la source

Switch to MapSet for tracking pids, fixes #3

Tim Buchwaldt il y a 7 ans
Parent
commit
6d54121c87
1 fichiers modifiés avec 6 ajouts et 6 suppressions
  1. 6 6
      lib/gen_reset/gen_reset_tracker.ex

+ 6 - 6
lib/gen_reset/gen_reset_tracker.ex

@@ -1,7 +1,7 @@
 defmodule GenReset.Tracker do
   use GenServer
 
-  @initial_state []
+  @initial_state MapSet.new()
 
   def start_link(_args) do
     GenServer.start_link(__MODULE__, nil, name: __MODULE__)
@@ -33,11 +33,11 @@ defmodule GenReset.Tracker do
 
   def handle_call({:add, pid}, _from, state) do
     Process.monitor(pid)
-    {:reply, :ok, state ++ [pid]}
+    {:reply, :ok, MapSet.put(state, pid)}
   end
 
   def handle_call({:remove, pid}, _from, state) do
-    {:reply, :ok, state -- [pid]}
+    {:reply, :ok, MapSet.delete(state, pid)}
   end
 
   def handle_call(:reset, _from, state) do
@@ -46,11 +46,11 @@ defmodule GenReset.Tracker do
     end
   end
 
-  def handle_call(:pids, _from, state), do: {:reply, state, state}
+  def handle_call(:pids, _from, state), do: {:reply, Enum.into(state, []), state}
 
-  def handle_call(:internal_reset, _from, _state), do: {:reply, :ok, []}
+  def handle_call(:internal_reset, _from, _state), do: {:reply, :ok, @initial_state}
 
   def handle_info({:DOWN, _ref, :process, object, _reason}, state) do
-    {:noreply, state -- [object]}
+    {:noreply, MapSet.delete(state, object)}
   end
 end